Skip to content

Commit

Permalink
feat(18222): add transformSearchResults prop to MultiselectChipWithSe…
Browse files Browse the repository at this point in the history
…arch
  • Loading branch information
albaranau committed Jun 4, 2024
1 parent 4b64575 commit 0d3e9ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function MultiselectChipWithSearch<I extends SelectableItem>({
withResetButton = true,
type = 'classic',
filter = defaultFilterByTitle,
transformSearchResults,
itemToString = defaultItemToString,
className,
}: {
Expand All @@ -69,20 +70,26 @@ export function MultiselectChipWithSearch<I extends SelectableItem>({
error?: boolean;
type?: 'classic' | 'inline';
filter?: (item: I, search: string) => boolean;
/**
* Apply any modifications (sorting etc) to the filtered items here
*/
transformSearchResults?: (items: I[], search: string) => I[];
itemToString?: (item: I | null) => string;
className?: string;
}) {
const [inputValue, setInputValue] = useState('');
const filteredItems = useMemo(
() =>
getFilteredList({
list: items,
exclude: selectedItems,
search: inputValue,
filter,
}),
[items, selectedItems, filter, inputValue],
);
const resultItems = useMemo(() => {
const filtered = getFilteredList({
list: items,
exclude: selectedItems,
search: inputValue,
filter,
});
if (transformSearchResults) {
transformSearchResults(filtered, inputValue);
}
return filtered;
}, [items, selectedItems, filter, inputValue]);

const { getDropdownProps, removeSelectedItem, reset } = useMultipleSelection({
selectedItems: selectedItems,
Expand Down Expand Up @@ -111,7 +118,7 @@ export function MultiselectChipWithSearch<I extends SelectableItem>({

const { isOpen, getToggleButtonProps, getLabelProps, getMenuProps, getInputProps, highlightedIndex, getItemProps } =
useCombobox({
items: filteredItems,
items: resultItems,
itemToString: itemToString,
defaultHighlightedIndex: 0, // after selection, highlight the first item.
selectedItem: null,
Expand Down Expand Up @@ -207,8 +214,8 @@ export function MultiselectChipWithSearch<I extends SelectableItem>({
</div>
<ul {...getMenuProps()} className={cn({ [style.menu]: true })}>
{isOpen &&
(filteredItems.length ? (
filteredItems.map((item, index) => (
(resultItems.length ? (
resultItems.map((item, index) => (
<SelectItem
key={`${item.value}${index}`}
item={item}
Expand Down

0 comments on commit 0d3e9ba

Please sign in to comment.