Skip to content

Commit

Permalink
fix(dropdown): fixed downshift warning going from controlled to uncon…
Browse files Browse the repository at this point in the history
…trolled
  • Loading branch information
Powerplex committed Mar 26, 2024
1 parent df9b557 commit 614430d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/components/dropdown/src/Dropdown.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export const Statuses: StoryFn = () => {
<div className="flex flex-col gap-lg pb-[300px]">
{statuses.map(status => {
return (
<Dropdown state={status}>
<Dropdown state={status} key={status}>
<Dropdown.Trigger aria-label="Book">
<Dropdown.Value placeholder="Pick a book" />
</Dropdown.Trigger>
Expand Down
18 changes: 13 additions & 5 deletions packages/components/dropdown/src/useDropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export const useDropdown = ({

const downshiftMultipleSelection = useMultipleSelection<DropdownItem>({
selectedItems:
value != null
value != null && multiple
? items.filter(item =>
multiple ? (value as string[]).includes(item.value) : value === item.value
)
: undefined,
initialSelectedItems:
defaultValue != null
defaultValue != null && multiple
? items.filter(item =>
multiple ? (defaultValue as string[]).includes(item.value) : defaultValue === item.value
)
Expand Down Expand Up @@ -104,9 +104,11 @@ export const useDropdown = ({
initialIsOpen: defaultOpen ?? false,
stateReducer,
// Controlled mode (single selection)
selectedItem: value != null ? itemsMap.get(value as string) : undefined,
selectedItem: value != null && !multiple ? itemsMap.get(value as string) || null : undefined,
initialSelectedItem:
defaultValue != null || value ? itemsMap.get(defaultValue as string) : undefined,
(defaultValue != null || value != null) && !multiple
? itemsMap.get(defaultValue as string) || null
: undefined,
onSelectedItemChange: ({ selectedItem }) => {
if (selectedItem?.value != null && !multiple) {
onValueChange?.(selectedItem?.value as OnChangeValueType)
Expand All @@ -115,7 +117,13 @@ export const useDropdown = ({
/**
* 1. Downshift default behaviour is to scroll into view the highlighted item when the dropdown opens. This behaviour is not stable and scrolls the dropdown to the bottom of the screen.
*/
scrollIntoView: () => undefined /* 1 */,
scrollIntoView: node => {
if (node) {
node.scrollIntoView({ block: 'nearest' })
}

return undefined
},
})

return {
Expand Down

0 comments on commit 614430d

Please sign in to comment.