Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-yaks-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

SelectPanel: Fixes a bug in `SelectPanel` when the list of items was not memoized.
37 changes: 34 additions & 3 deletions packages/react/src/SelectPanel/SelectPanel.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import {render as HTMLRender} from '@testing-library/react'
import {render as HTMLRender, fireEvent, getByRole, getByText} from '@testing-library/react'
import axe from 'axe-core'

import React from 'react'
import theme from '../theme'
import {SelectPanel} from '../SelectPanel'
import {behavesAsComponent, checkExports} from '../utils/testing'
import {BaseStyles, SSRProvider, ThemeProvider} from '..'
import type {ItemInput} from '../deprecated/ActionList/List'

const items = [{text: 'Foo'}, {text: 'Bar'}, {text: 'Baz'}, {text: 'Bon'}] as ItemInput[]

function SimpleSelectPanel(): JSX.Element {
const items = [
{text: 'Foo', id: 'foo'},
{text: 'Bar', id: 'bar'},
{text: 'Baz', id: 'baz'},
{text: 'Bon', id: 'bon'},
] as ItemInput[]

const [selected, setSelected] = React.useState<ItemInput[]>([])
const [, setFilter] = React.useState('')
const [open, setOpen] = React.useState(false)
Expand All @@ -27,6 +33,7 @@ function SimpleSelectPanel(): JSX.Element {
onFilterChange={setFilter}
open={open}
onOpenChange={setOpen}
overlayProps={{width: 'medium', height: 'medium'}}
/>
<div id="portal-root"></div>
</BaseStyles>
Expand All @@ -36,10 +43,18 @@ function SimpleSelectPanel(): JSX.Element {
}

describe('SelectPanel', () => {
const originalScrollTo = Element.prototype.scrollTo
beforeAll(() => {
Element.prototype.scrollTo = () => {}
})
afterEach(() => {
jest.clearAllMocks()
})

afterAll(() => {
Element.prototype.scrollTo = originalScrollTo
})

behavesAsComponent({
Component: SelectPanel,
options: {skipAs: true, skipSx: true},
Expand All @@ -56,4 +71,20 @@ describe('SelectPanel', () => {
const results = await axe.run(container)
expect(results).toHaveNoViolations()
})

it('should render selected items', () => {
const item = HTMLRender(<SimpleSelectPanel />)
const {container} = item
const button = getByRole(container, 'button')
expect(button).toBeTruthy()

fireEvent.click(button)
const selector = getByText(container, 'Foo')
expect(selector).toBeVisible()

fireEvent.click(selector)

const selected = getByRole(container, 'option', {selected: true})
expect(selected).toBeTruthy()
})
})
28 changes: 24 additions & 4 deletions packages/react/src/SelectPanel/SelectPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,24 @@ export function SelectPanel({
}
}, [placeholder, renderAnchor, selected])

const testIsItemSelected = React.useCallback((selectedItem: ItemInput, item: ItemInput) => {
const itemType = typeof selectedItem
if (itemType === 'object') {
if (selectedItem.hasOwnProperty('id')) {
return selectedItem.id === item.id
}
Comment on lines +132 to +134
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also try testing for key since that's React's way of differentiating component changes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that, but I actually think it's better to add the key when rendering over the list instead of trying to add it preemptively. Not super opinionated though if we want to use the key here!

}

return selectedItem === item
}, [])

const itemsToRender = useMemo(() => {
return items.map(item => {
const isItemSelected = isMultiSelectVariant(selected) ? selected.includes(item) : selected === item
const isItemSelected = isMultiSelectVariant(selected)
? selected.some(selectedItem => {
return testIsItemSelected(selectedItem, item)
})
: selected === item

return {
...item,
Expand All @@ -142,8 +157,13 @@ export function SelectPanel({
}

if (isMultiSelectVariant(selected)) {
const otherSelectedItems = selected.filter(selectedItem => selectedItem !== item)
const newSelectedItems = selected.includes(item) ? otherSelectedItems : [...otherSelectedItems, item]
const wasPreviouslySelected = selected.some(selectedItem => {
return testIsItemSelected(selectedItem, item)
})
const otherSelectedItems = selected.filter(selectedItem => {
return !testIsItemSelected(selectedItem, item)
})
const newSelectedItems = wasPreviouslySelected ? otherSelectedItems : [...otherSelectedItems, item]

const multiSelectOnChange = onSelectedChange as SelectPanelMultiSelection['onSelectedChange']
multiSelectOnChange(newSelectedItems)
Expand All @@ -157,7 +177,7 @@ export function SelectPanel({
},
} as ItemProps
})
}, [onClose, onSelectedChange, items, selected])
}, [onClose, onSelectedChange, items, selected, testIsItemSelected])

const inputRef = React.useRef<HTMLInputElement>(null)
const focusTrapSettings = {
Expand Down