An open source design system that's fully customizable and agent ready
TypeScript
fix(Selector): return focus to the trigger when the clear button is activated (#7455)
홍규진77d1674595cd
diff --git a/.changeset/selector-clear-returns-focus.md b/.changeset/selector-clear-returns-focus.mdnew file mode 100644index 0000000000..44c47c06c7--- /dev/null+++ b/.changeset/selector-clear-returns-focus.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `Selector`: activating the clear button with the keyboard now returns focus to the trigger, as `hasClear` documents. Previously the button unmounted once the value was cleared and focus fell to `<body>`, so a keyboard user had to Tab back in from the top of the page. Pointer clears are unchanged.++@kyu-rongdiff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsxindex f3d12bc767..592a512f9e 100644--- a/packages/core/src/Selector/Selector.test.tsx+++ b/packages/core/src/Selector/Selector.test.tsx@@ -822,6 +822,58 @@ describe('Selector', () => {expect(onChange).toHaveBeenCalledWith(null);});+ it('returns focus to the trigger when the clear button is activated by keyboard', async () => {+ function Controlled() {+ const [value, setValue] = useState<string | null>('Banana');+ return (+ <Selector+ label="Fruit"+ options={OPTIONS}+ value={value}+ onChange={setValue}+ hasClear+ />+ );+ }+ const user = userEvent.setup();+ render(<Controlled />);+ screen.getByRole('button', {name: 'Clear Fruit'}).focus();+ await user.keyboard('{Enter}');+ expect(+ screen.queryByRole('button', {name: 'Clear Fruit'}),+ ).not.toBeInTheDocument();+ expect(screen.getByRole('combobox')).toHaveFocus();+ });++ it('leaves focus in the search input when the clear button is clicked with the list open', async () => {+ function Controlled() {+ const [value, setValue] = useState<string | null>('Banana');+ return (+ <Selector+ label="Fruit"+ options={OPTIONS}+ value={value}+ onChange={setValue}+ hasClear+ hasSearch+ />+ );+ }+ const user = userEvent.setup();+ render(<Controlled />);+ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ const search = screen.getByRole('combobox', {+ name: 'Search options',+ hidden: true,+ });+ search.focus();+ await user.click(screen.getByRole('button', {name: 'Clear Fruit'}));+ expect(+ screen.queryByRole('button', {name: 'Clear Fruit'}),+ ).not.toBeInTheDocument();+ expect(search).toHaveFocus();+ });+it('clears the value via Delete on the focused trigger', async () => {const user = userEvent.setup();const onChange = vi.fn();diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsxindex df4497d9c9..5e12ce9f10 100644--- a/packages/core/src/Selector/Selector.tsx+++ b/packages/core/src/Selector/Selector.tsx@@ -1381,6 +1381,14 @@ export function Selector<T extends SelectorOptionType>((e: React.MouseEvent) => {e.stopPropagation(); // Don't open dropdownclearValue();+ // A keyboard activation (detail 0) leaves focus on the clear button,+ // which unmounts once the value is null and would drop focus to <body>;+ // hand it to the trigger as `hasClear` documents. A pointer press never+ // focuses the button, so there is no focus to repair, and moving it+ // would pull it out of an open list's search input.+ if (e.detail === 0) {+ triggerRef.current?.focus();+ }},[clearValue],);