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

fix(multi-select): Update selected state when pill is removed #3076

Merged
merged 1 commit into from
Dec 17, 2024
Merged
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
4 changes: 2 additions & 2 deletions modules/preview-react/multi-select/lib/MultiSelectedList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export interface MultiSelectedListProps
extends MultiSelectedItemProps,
React.HTMLAttributes<HTMLDivElement> {}

export const MultiSelectedList = createSubcomponent()({
export const MultiSelectedList = createSubcomponent('div')({
modelHook: useMultiSelectModel,
})<MultiSelectedListProps>(({'aria-labelledby': ariaLabelledBy, removeLabel}, Element, model) => {
return model.selected.state.items.length ? (
<>
<div data-part="separator" />
<ListBox
model={model.selected}
as="div"
as={Element}
role="listbox"
aria-orientation="horizontal"
aria-labelledby={ariaLabelledBy}
Expand Down
11 changes: 1 addition & 10 deletions modules/preview-react/multi-select/lib/useMultiSelectModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,12 @@ export const useMultiSelectModel = createModelHook({
orientation: 'horizontal',
onRemove({id}) {
model.events.select({id});
setSelectedItems(cachedSelected.filter(item => item.id !== id));
Copy link
Member Author

Choose a reason for hiding this comment

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

Updates the selected items when the pill is removed. Without this, the pill that was removed will be temporarily added again while the menu is open and will disappear for good after the menu is closed again.

},
shouldVirtualize: false,
items: model.state.visibility === 'hidden' ? cachedSelected : selectedItems,
});

const state = {
...model.state,
};

const events = {
...model.events,
};

return {
selected: {
...selected,
Expand All @@ -94,7 +87,5 @@ export const useMultiSelectModel = createModelHook({
},
},
...model,
state,
events,
};
});
Loading