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

15257 deleted options bug #15669

Merged
Merged
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
27 changes: 25 additions & 2 deletions packages/react/src/components/MultiSelect/MultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ interface SortItemsOptions<ItemType>
selectedItems: ItemType[];
}

interface selectedItemType {
text: string;
}

interface MultiSelectSortingProps<ItemType> {
/**
* Provide a compare function that is used to determine the ordering of
Expand Down Expand Up @@ -133,6 +137,11 @@ export interface MultiSelectProps<ItemType>
> {
className?: string;

/**
* Specify the text that should be read for screen readers that describes that all items are deleted
*/
clearAnnouncement?: string;

/**
* Specify the text that should be read for screen readers that describes total items selected
*/
Expand Down Expand Up @@ -320,6 +329,7 @@ const MultiSelect = React.forwardRef(
sortItems = defaultSortItems as MultiSelectProps<ItemType>['sortItems'],
compareItems = defaultCompareItems,
clearSelectionText = 'To clear selection, press Delete or Backspace',
clearAnnouncement = 'all items have been cleared',
clearSelectionDescription = 'Total items selected: ',
light,
invalid,
Expand Down Expand Up @@ -349,6 +359,7 @@ const MultiSelect = React.forwardRef(
const [isOpen, setIsOpen] = useState(open || false);
const [prevOpenProp, setPrevOpenProp] = useState(open);
const [topItems, setTopItems] = useState([]);
const [itemsCleared, setItemsCleared] = useState(false);
const {
selectedItems: controlledSelectedItems,
onItemChange,
Expand Down Expand Up @@ -405,12 +416,17 @@ const MultiSelect = React.forwardRef(
e.stopPropagation();
}

if (!isOpen && match(e, keys.Delete) && selectedItems.length > 0) {
setItemsCleared(true);
}

if (
(match(e, keys.Space) ||
match(e, keys.ArrowDown) ||
match(e, keys.Enter)) &&
!isOpen
) {
setItemsCleared(false);
setIsOpenWrapper(true);
}
}
Expand Down Expand Up @@ -588,14 +604,18 @@ const MultiSelect = React.forwardRef(
});
}

const itemsSelectedText =
selectedItems.length > 0 &&
selectedItems.map((item) => (item as selectedItemType).text);

return (
<div className={wrapperClasses}>
<label className={titleClasses} {...getLabelProps()}>
{titleText && titleText}
{selectedItems.length > 0 && (
<span className={`${prefix}--visually-hidden`}>
{clearSelectionDescription} {selectedItems.length},
{clearSelectionText}
{clearSelectionDescription} {selectedItems.length}{' '}
{itemsSelectedText},{clearSelectionText}
</span>
)}
</label>
Expand Down Expand Up @@ -701,6 +721,9 @@ const MultiSelect = React.forwardRef(
}
)}
</ListBox.Menu>
{itemsCleared && (
<span aria-live="assertive" aria-label={clearAnnouncement} />
)}
</ListBox>
{!inline && !invalid && !warn && helperText && (
<div id={helperId} className={helperClasses}>
Expand Down
Loading