Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non-memoized item lists #4324

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
---

Fixes a bug in `SelectPanel` when the list of items was not memoized.
36 changes: 33 additions & 3 deletions packages/react/src/SelectPanel/SelectPanel.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {render as HTMLRender} from '@testing-library/react'
import {render as HTMLRender, fireEvent, getByRole, getByText} from '@testing-library/react'
import {axe} from 'jest-axe'
import React from 'react'
import theme from '../theme'
Expand All @@ -7,9 +7,14 @@ 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 +32,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 +42,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 +70,20 @@ describe('SelectPanel', () => {
const results = await axe(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 @@ -124,9 +124,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 @@ -140,8 +155,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 @@ -155,7 +175,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
Loading