Skip to content

Commit

Permalink
Adding popLast
Browse files Browse the repository at this point in the history
  • Loading branch information
OsamaAbdellateef committed Jan 9, 2025
1 parent 80ca4e8 commit ccdc508
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions packages/components/src/Multiselect/Multiselect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,20 @@ export const Multiselect = forwardRef<HTMLInputElement, MultiselectProps<any>>(
inputValue: '',
});

const clearInput = () => {
setFieldState({
inputValue: '',
selectedKey: null,
});
};

const onRemove = useCallback(
(keys: Set<Key>) => {
// TODO: clarify why this is an array
const key = keys.values().next().value;
if (key) {
selectedItems.remove(key);
setFieldState({
inputValue: '',
selectedKey: null,
});
clearInput();
onItemCleared?.(key);
}
},
Expand All @@ -200,10 +204,7 @@ export const Multiselect = forwardRef<HTMLInputElement, MultiselectProps<any>>(

if (!selectedKeys.includes(id)) {
selectedItems.append(item);
setFieldState({
inputValue: '',
selectedKey: null,
});
clearInput();
onItemInserted?.(id);
}
};
Expand All @@ -217,7 +218,20 @@ export const Multiselect = forwardRef<HTMLInputElement, MultiselectProps<any>>(
accessibleList.setFilterText(value);
};

const popLast = useCallback(() => {}, []);
const popLast = useCallback(() => {
if (selectedItems.items.length === 0) {
return;
}

// Getting the last selected item
const endKey = selectedItems.items.at(-1);

if (endKey) {
selectedItems.remove(endKey.id);
onItemCleared?.(endKey.id);
}
clearInput();
}, [selectedItems, onItemCleared]);

return (
// container
Expand Down

0 comments on commit ccdc508

Please sign in to comment.