An open source design system that's fully customizable and agent ready
TypeScript
fix(selector): keyboard clear via Delete/Backspace (comboboxes-2) (#3365)
Cindy Zhangbfcbf64dac1d
diff --git a/.changeset/selector-clear-keyboard.md b/.changeset/selector-clear-keyboard.mdnew file mode 100644index 0000000000..20f5f9daac--- /dev/null+++ b/.changeset/selector-clear-keyboard.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Selector/MultiSelector: `Delete` and `Backspace` now clear the value from the focused trigger when `hasClear` is set, so clearing a selection is no longer mouse-only. The clear button was already keyboard-reachable; this adds the keyboard shortcut path (#3343).+@cixzhangdiff --git a/packages/core/src/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsxindex 57d6c42823..d202302548 100644--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx@@ -783,6 +783,60 @@ describe('MultiSelector', () => {.scrollIntoView;}});++ it('clears all values via Delete on the focused trigger', async () => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ render(+ <MultiSelector+ label="Fruit"+ options={defaultOptions}+ value={['Apple', 'Banana']}+ onChange={onChange}+ hasClear+ />,+ );+ const trigger = screen.getByRole('combobox');+ trigger.focus();+ await user.keyboard('{Delete}');+ expect(onChange).toHaveBeenCalledWith([]);+ });++ it('clears all values via Backspace on the focused trigger', async () => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ render(+ <MultiSelector+ label="Fruit"+ options={defaultOptions}+ value={['Apple', 'Banana']}+ onChange={onChange}+ hasClear+ />,+ );+ const trigger = screen.getByRole('combobox');+ trigger.focus();+ await user.keyboard('{Backspace}');+ expect(onChange).toHaveBeenCalledWith([]);+ });++ it('does not clear via Delete when nothing is selected', async () => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ render(+ <MultiSelector+ label="Fruit"+ options={defaultOptions}+ value={[]}+ onChange={onChange}+ hasClear+ />,+ );+ const trigger = screen.getByRole('combobox');+ trigger.focus();+ await user.keyboard('{Delete}');+ expect(onChange).not.toHaveBeenCalled();+ });});describe('announcements', () => {diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsxindex b7180902f9..0f27fd5696 100644--- a/packages/core/src/MultiSelector/MultiSelector.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.tsx@@ -730,26 +730,34 @@ export function MultiSelector<T extends MultiSelectorOptionType>({}, []);// Handle toggle- // Handle clear button click+ // Clear all selected values. Shared by the clear button and the keyboard+ // Delete/Backspace path so clearing is reachable without a mouse.+ const clearValues = useCallback(() => {+ onChange([]);+ announceSelection([]);+ if (changeAction) {+ startTransition(async () => {+ setOptimisticValue([]);+ await changeAction([]);+ });+ }+ }, [+ onChange,+ changeAction,+ startTransition,+ setOptimisticValue,+ announceSelection,+ ]);++ // Whether there is at least one selected value (clearing is meaningful).+ const hasValue = optimisticValue.length > 0;+const handleClear = useCallback((e: React.MouseEvent) => {e.stopPropagation(); // Don't open dropdown- onChange([]);- announceSelection([]);- if (changeAction) {- startTransition(async () => {- setOptimisticValue([]);- await changeAction([]);- });- }+ clearValues();},- [- onChange,- changeAction,- startTransition,- setOptimisticValue,- announceSelection,- ],+ [clearValues],);const handleToggle = useCallback(@@ -876,6 +884,8 @@ export function MultiSelector<T extends MultiSelectorOptionType>({}, [popover, hasSearch, optimisticValue]),onClose: popover.hide,onToggle: handleNavigableToggle,+ onClear: hasClear ? clearValues : undefined,+ hasValue,listboxId,});diff --git a/packages/core/src/MultiSelector/hooks.ts b/packages/core/src/MultiSelector/hooks.tsindex 1db297e3e9..3dc14e402d 100644--- a/packages/core/src/MultiSelector/hooks.ts+++ b/packages/core/src/MultiSelector/hooks.ts@@ -23,6 +23,17 @@ interface UseMultiComboboxOptions {onOpen: () => void;onClose: () => void;onToggle: (itemValue: string) => void;+ /**+ * Clear all selected values. When provided, pressing Delete or Backspace on+ * the closed trigger clears the selection — a keyboard equivalent of the+ * clear button (comboboxes-2). No-op when the popup is open or search is on.+ */+ onClear?: () => void;+ /**+ * Whether at least one value is selected (i.e. there is something to clear).+ * The Delete/Backspace clear path is skipped when false.+ */+ hasValue?: boolean;listboxId: string;}@@ -49,6 +60,8 @@ export function useMultiCombobox({onOpen,onClose,onToggle,+ onClear,+ hasValue = false,listboxId,}: UseMultiComboboxOptions): UseMultiComboboxResult {const [highlightedIndex, setHighlightedIndex] = useState<number>(-1);@@ -179,6 +192,18 @@ export function useMultiCombobox({}break;+ case 'Delete':+ case 'Backspace':+ // Keyboard equivalent of the clear button (comboboxes-2): clear all+ // selected values from the closed trigger so clearing is not+ // mouse-only. Skipped in search mode (keys edit the query) and while+ // the popup is open (arrow navigation owns interaction).+ if (!hasSearch && !isOpen && onClear != null && hasValue) {+ e.preventDefault();+ onClear();+ }+ break;+default:// Typeahead only when search is not presentif (!hasSearch && e.key.length === 1 && !e.ctrlKey && !e.metaKey) {@@ -218,6 +243,8 @@ export function useMultiCombobox({getEnabledIndices,typeahead,hasSearch,+ onClear,+ hasValue,],);diff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsxindex 81a70fdaa9..329505f8f8 100644--- a/packages/core/src/Selector/Selector.test.tsx+++ b/packages/core/src/Selector/Selector.test.tsx@@ -332,6 +332,59 @@ describe('Selector', () => {expect(onChange).toHaveBeenCalledWith(null);});+ it('clears the value via Delete on the focused trigger', async () => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ render(+ <Selector+ label="Fruit"+ options={OPTIONS}+ value="Banana"+ onChange={onChange}+ hasClear+ />,+ );+ const trigger = screen.getByRole('combobox');+ trigger.focus();+ await user.keyboard('{Delete}');+ expect(onChange).toHaveBeenCalledWith(null);+ });++ it('clears the value via Backspace on the focused trigger', async () => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ render(+ <Selector+ label="Fruit"+ options={OPTIONS}+ value="Banana"+ onChange={onChange}+ hasClear+ />,+ );+ const trigger = screen.getByRole('combobox');+ trigger.focus();+ await user.keyboard('{Backspace}');+ expect(onChange).toHaveBeenCalledWith(null);+ });++ it('does not clear via Delete when hasClear is not set', async () => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ render(+ <Selector+ label="Fruit"+ options={OPTIONS}+ value="Banana"+ onChange={onChange}+ />,+ );+ const trigger = screen.getByRole('combobox');+ trigger.focus();+ await user.keyboard('{Delete}');+ expect(onChange).not.toHaveBeenCalled();+ });+it('shows placeholder after clearing', () => {render(<Selectordiff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsxindex 88fb364d0c..c9f1086666 100644--- a/packages/core/src/Selector/Selector.tsx+++ b/packages/core/src/Selector/Selector.tsx@@ -652,6 +652,18 @@ export function Selector<T extends SelectorOptionType>(? {marginBlockStart: `-${selectedItemOffset}px`}: undefined;+ // Clear the current value. Shared by the clear button and the keyboard+ // Delete/Backspace path so clearing is reachable without a mouse.+ const clearValue = useCallback(() => {+ onChange?.(null);+ if (changeAction) {+ startTransition(async () => {+ setOptimisticValue(undefined);+ await changeAction(null);+ });+ }+ }, [onChange, changeAction, startTransition, setOptimisticValue]);+// Selector behavior (keyboard nav, typeahead, selection)const {highlightedIndex,@@ -688,6 +700,7 @@ export function Selector<T extends SelectorOptionType>(},[onChange, changeAction, startTransition, setOptimisticValue],),+ onClear: hasClear ? clearValue : undefined,listboxId,});@@ -708,15 +721,9 @@ export function Selector<T extends SelectorOptionType>(const handleClear = useCallback((e: React.MouseEvent) => {e.stopPropagation(); // Don't open dropdown- onChange?.(null);- if (changeAction) {- startTransition(async () => {- setOptimisticValue(undefined);- await changeAction(null);- });- }+ clearValue();},- [onChange, changeAction, startTransition, setOptimisticValue],+ [clearValue],);// Render search inputdiff --git a/packages/core/src/Selector/hooks.ts b/packages/core/src/Selector/hooks.tsindex 45d2c800db..5cf720911f 100644--- a/packages/core/src/Selector/hooks.ts+++ b/packages/core/src/Selector/hooks.ts@@ -138,6 +138,13 @@ interface UseComboboxOptions {onOpen: () => void;onClose: () => void;onSelect?: (value: string) => void;+ /**+ * Clear the current value. When provided, pressing Delete or Backspace on the+ * closed trigger clears the selection — a keyboard equivalent of the clear+ * button, so clearing is not mouse-only. No-op when the popup is open (arrow+ * navigation owns those keys there) or when there is no value.+ */+ onClear?: () => void;listboxId: string;}@@ -163,6 +170,7 @@ export function useCombobox({onOpen,onClose,onSelect,+ onClear,listboxId,}: UseComboboxOptions): UseComboboxResult {const [highlightedIndex, setHighlightedIndex] = useState<number>(-1);@@ -310,6 +318,19 @@ export function useCombobox({}break;+ case 'Delete':+ case 'Backspace':+ // Keyboard equivalent of the clear button (comboboxes-2): clear the+ // value from the closed trigger so clearing is not mouse-only. When+ // hasSearch is active these keys must edit the search text instead,+ // and when the popup is open arrow navigation owns interaction, so+ // only handle the closed non-search case with a clearable value.+ if (!hasSearch && !isOpen && onClear != null && value != null) {+ e.preventDefault();+ onClear();+ }+ break;+default:if (!hasSearch && e.key.length === 1 && !e.ctrlKey && !e.metaKey) {const newTypeahead = typeahead + e.key.toLowerCase();@@ -349,6 +370,8 @@ export function useCombobox({getEnabledIndices,typeahead,hasSearch,+ onClear,+ value,],);