An open source design system that's fully customizable and agent ready
TypeScript
fix(MultiSelector): move focus to the trigger only when the clear button is activated by keyboard (#7506)
홍규진3736d4338d93
diff --git a/.changeset/multiselector-clear-returns-focus.md b/.changeset/multiselector-clear-returns-focus.mdnew file mode 100644index 0000000000..03c045f451--- /dev/null+++ b/.changeset/multiselector-clear-returns-focus.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `MultiSelector`: activating the clear button with the keyboard now returns focus to the trigger, as `hasClear` documents. Previously the button unmounted once the selection was empty 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/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsxindex 246bb2e628..8d713dd644 100644--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx@@ -25,7 +25,7 @@ import {type MultiSelectorHandle,type MultiSelectorChange,} from './MultiSelector';-import {useRef} from 'react';+import {useRef, useState} from 'react';import {Icon} from '../Icon';import {InternationalizationProvider} from '../i18n';import {__resetLiveRegionsForTest} from '../hooks/useAnnounce';@@ -1595,6 +1595,55 @@ describe('MultiSelector', () => {expect(clear).not.toHaveAttribute('tabIndex', '-1');});+ it('returns focus to the trigger when the clear button is activated by keyboard', async () => {+ function Controlled() {+ const [value, setValue] = useState(['Apple', 'Banana']);+ return (+ <MultiSelector+ label="Fruit"+ options={defaultOptions}+ value={value}+ onChange={setValue}+ hasClear+ />+ );+ }+ const user = userEvent.setup();+ render(<Controlled />);+ screen.getByRole('button', {name: 'Clear all Fruit'}).focus();+ await user.keyboard('{Enter}');+ expect(+ screen.queryByRole('button', {name: 'Clear all 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(['Apple', 'Banana']);+ return (+ <MultiSelector+ label="Fruit"+ options={defaultOptions}+ value={value}+ onChange={setValue}+ hasClear+ hasSearch+ />+ );+ }+ const user = userEvent.setup();+ const {container} = render(<Controlled />);+ await user.click(container.querySelector('[aria-haspopup]') as Element);+ const search = screen.getByRole('combobox', h);+ search.focus();+ await user.click(screen.getByRole('button', {name: 'Clear all Fruit'}));+ expect(+ screen.queryByRole('button', {name: 'Clear all Fruit'}),+ ).not.toBeInTheDocument();+ expect(search).toHaveFocus();+ });+it('scrolls the highlighted option into view during arrow navigation', async () => {const scrollIntoView = vi.fn();Object.defineProperty(HTMLElement.prototype, 'scrollIntoView', {diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsxindex 65ee97254d..0d92344e7f 100644--- a/packages/core/src/MultiSelector/MultiSelector.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.tsx@@ -1569,6 +1569,14 @@ export function MultiSelector<T extends MultiSelectorOptionType>({(e: React.MouseEvent) => {e.stopPropagation(); // Don't open dropdownclearValues();+ // A keyboard activation (detail 0) leaves focus on the clear button,+ // which unmounts once the value is empty and would drop focus to <body>;+ // hand it to the trigger. A pointer press never focuses the button, so+ // there is no focus to repair, and moving it would pull focus out of an+ // open list's search input.+ if (e.detail === 0) {+ triggerRef.current?.focus();+ }},[clearValues],);