An open source design system that's fully customizable and agent ready
TypeScript
fix(selector): make hasSearch input the combobox with aria-activedescendant (comboboxes-4) (#3402)
Cindy Zhangac17c7fc249b
diff --git a/.changeset/selector-hassearch-combobox.md b/.changeset/selector-hassearch-combobox.mdnew file mode 100644index 0000000000..6e0fadf163--- /dev/null+++ b/.changeset/selector-hassearch-combobox.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Selector/MultiSelector (`hasSearch`): the popup's search input is now the combobox — it carries `role="combobox"`, `aria-expanded`, `aria-autocomplete="list"`, `aria-controls`, and `aria-activedescendant`, so screen readers announce the highlighted option as ArrowUp/Down move it. Previously the search input was a bare `searchbox` while `aria-activedescendant` stayed on the (now-unfocused) trigger, leaving highlight changes silent. In `hasSearch` mode the trigger is a plain button that opens the listbox rather than a second combobox (#3343).+@cixzhangdiff --git a/packages/core/src/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsxindex 121da3a4f6..bc7c25d0b3 100644--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx@@ -496,8 +496,10 @@ describe('MultiSelector', () => {/>,);- await user.click(screen.getByRole('combobox'));- expect(screen.getByRole('searchbox', h)).toBeInTheDocument();+ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ const searchInput = screen.getByRole('combobox', h);+ expect(searchInput).toBeInTheDocument();+ expect(searchInput).toHaveAttribute('aria-autocomplete', 'list');});it('filters options when searching', async () => {@@ -512,8 +514,8 @@ describe('MultiSelector', () => {/>,);- await user.click(screen.getByRole('combobox'));- const searchInput = screen.getByRole('searchbox', h);+ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ const searchInput = screen.getByRole('combobox', h);await user.type(searchInput, 'app');const options = screen.getAllByRole('option', h);@@ -532,8 +534,8 @@ describe('MultiSelector', () => {/>,);- await user.click(screen.getByRole('combobox'));- const searchInput = screen.getByRole('searchbox', h);+ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ const searchInput = screen.getByRole('combobox', h);await user.type(searchInput, 'xyz');expect(screen.getByText('No results found')).toBeInTheDocument();diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsxindex 6d7ab122ef..ae698c9f51 100644--- a/packages/core/src/MultiSelector/MultiSelector.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.tsx@@ -906,17 +906,30 @@ export function MultiSelector<T extends MultiSelectorOptionType>({<inputref={searchRef}id={searchId}- role="searchbox"+ // When hasSearch is set, focus moves into this input on open, so it —+ // not the trigger — must be the combobox reporting the highlighted+ // option via aria-activedescendant (comboboxes-4).+ role="combobox"+ aria-expanded={popover.isOpen}aria-controls={listboxId}+ aria-autocomplete="list"+ aria-activedescendant={+ popover.isOpen && highlightedIndex >= 0+ ? getItemId(highlightedIndex)+ : undefined+ }aria-label="Search options"type="text"value={searchQuery}onChange={e => setSearchQuery(e.target.value)}onKeyDown={e => {- // Let ArrowDown/Up/Escape/Tab propagate to parent handler+ // Arrow keys navigate options; Enter toggles; Escape/Tab close.+ // Space and Home/End are left to the input (type a space / move+ // the caret), not forwarded to option navigation.if (e.key === 'ArrowDown' ||e.key === 'ArrowUp' ||+ e.key === 'Enter' ||e.key === 'Escape' ||e.key === 'Tab') {@@ -935,6 +948,9 @@ export function MultiSelector<T extends MultiSelectorOptionType>({searchQuery,searchPlaceholder,onKeyDown,+ popover.isOpen,+ highlightedIndex,+ getItemId,]);// Render an individual item (index-based)@@ -1062,9 +1078,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({if (isDivider(option)) {flushPending();- elements.push(- <Divider key={`divider-${i}`} xstyle={styles.divider} />,- );+ elements.push(<Divider key={`divider-${i}`} xstyle={styles.divider} />);} else if (isSection(option)) {flushPending();const count = option.options.length;@@ -1144,12 +1158,15 @@ export function MultiSelector<T extends MultiSelectorOptionType>({ref={triggerRef}id={triggerId}type="button"- role="combobox"+ // In hasSearch mode the popup's search input is the combobox (it owns+ // focus + aria-activedescendant, comboboxes-4), so the trigger is a+ // plain button that opens the listbox — not a second combobox.+ role={hasSearch ? undefined : 'combobox'}aria-haspopup="listbox"aria-expanded={popover.isOpen}aria-controls={listboxId}aria-activedescendant={- popover.isOpen && highlightedIndex >= 0+ !hasSearch && popover.isOpen && highlightedIndex >= 0? getItemId(highlightedIndex): undefined}diff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsxindex 7fbdd0b1e3..9aff593e7b 100644--- a/packages/core/src/Selector/Selector.test.tsx+++ b/packages/core/src/Selector/Selector.test.tsx@@ -83,7 +83,12 @@ function mockSelectorRects() {'innerHeight',);HTMLElement.prototype.getBoundingClientRect = function () {- if (this.getAttribute('role') === 'combobox') {+ // The trigger is role="combobox" by default, or a plain button with+ // aria-haspopup="listbox" in hasSearch mode — match either.+ if (+ this.getAttribute('role') === 'combobox' ||+ this.getAttribute('aria-haspopup') === 'listbox'+ ) {return rect({top: 160, bottom: 190, height: 30});}if (this.getAttribute('role') === 'listbox') {@@ -108,9 +113,7 @@ function mockSelectorRects() {describe('Selector', () => {it('renders with placeholder when no value', () => {- render(- <Selector label="Fruit" options={OPTIONS} placeholder="Pick one" />,- );+ render(<Selector label="Fruit" options={OPTIONS} placeholder="Pick one" />);expect(screen.getByRole('combobox')).toHaveTextContent('Pick one');});@@ -355,8 +358,33 @@ describe('Selector', () => {hasSearch/>,);- await user.click(screen.getByRole('combobox'));- expect(screen.getByRole('searchbox', h)).toBeInTheDocument();+ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ expect(screen.getByRole('combobox', h)).toBeInTheDocument();+ });++ it('wires the search input as the combobox with activedescendant (comboboxes-4)', async () => {+ const user = userEvent.setup();+ render(+ <Selector+ label="Fruit"+ options={OPTIONS}+ value="Apple"+ onChange={() => {}}+ hasSearch+ />,+ );+ const triggerBtn = screen.getByRole('button', {name: 'Fruit'});+ // In hasSearch mode the trigger is a plain button, not a combobox.+ expect(triggerBtn).not.toHaveAttribute('role', 'combobox');+ await user.click(triggerBtn);+ const search = screen.getByRole('combobox', h);+ expect(search).toHaveAttribute('aria-autocomplete', 'list');+ expect(search).toHaveAttribute('aria-expanded', 'true');+ expect(search).toHaveAttribute('aria-controls');+ // ArrowDown moves the highlight; the search input reports it via+ // aria-activedescendant (previously silent on the trigger).+ await user.keyboard('{ArrowDown}');+ expect(search).toHaveAttribute('aria-activedescendant');});it('does not render search input when hasSearch is false', async () => {@@ -369,6 +397,8 @@ describe('Selector', () => {onChange={() => {}}/>,);+ // hasSearch is false, so the trigger itself is the combobox and there is+ // no separate search input inside the popup.await user.click(screen.getByRole('combobox'));expect(screen.queryByRole('searchbox', h)).not.toBeInTheDocument();});@@ -384,8 +414,8 @@ describe('Selector', () => {hasSearch/>,);- await user.click(screen.getByRole('combobox'));- await user.type(screen.getByRole('searchbox', h), 'ban');+ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ await user.type(screen.getByRole('combobox', h), 'ban');const options = screen.getAllByRole('option', h);expect(options).toHaveLength(1);expect(options[0]).toHaveTextContent('Banana');@@ -402,8 +432,8 @@ describe('Selector', () => {hasSearch/>,);- await user.click(screen.getByRole('combobox'));- await user.type(screen.getByRole('searchbox', h), 'xyz');+ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ await user.type(screen.getByRole('combobox', h), 'xyz');expect(screen.queryAllByRole('option', h)).toHaveLength(0);expect(screen.getByText('No results found')).toBeInTheDocument();});@@ -420,8 +450,8 @@ describe('Selector', () => {hasSearch/>,);- await user.click(screen.getByRole('combobox'));- await user.type(screen.getByRole('searchbox', h), 'ban');+ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ await user.type(screen.getByRole('combobox', h), 'ban');await user.click(screen.getByRole('option', {name: /Banana/, ...h}));expect(onChange).toHaveBeenCalledWith('Banana');});@@ -441,7 +471,9 @@ describe('Selector', () => {</>,);- const trigger = screen.getByRole('combobox');+ // In hasSearch mode the trigger is a plain button (the popup's search+ // input is the combobox); it still owns aria-expanded.+ const trigger = screen.getByRole('button', {name: 'Fruit'});await user.click(trigger);expect(trigger).toHaveAttribute('aria-expanded', 'true');@@ -461,7 +493,7 @@ describe('Selector', () => {searchPlaceholder="Find a fruit..."/>,);- await user.click(screen.getByRole('combobox'));+ await user.click(screen.getByRole('button', {name: 'Fruit'}));expect(screen.getByPlaceholderText('Find a fruit...'),).toBeInTheDocument();@@ -497,9 +529,7 @@ describe('Selector', () => {it('opens and selects an option with Enter (no mouse)', async () => {const user = userEvent.setup();const onChange = vi.fn();- render(- <Selector label="Fruit" options={OPTIONS} onChange={onChange} />,- );+ render(<Selector label="Fruit" options={OPTIONS} onChange={onChange} />);await user.tab();await user.keyboard('{Enter}'); // opendiff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsxindex a7b22e962c..3d3583286e 100644--- a/packages/core/src/Selector/Selector.tsx+++ b/packages/core/src/Selector/Selector.tsx@@ -474,34 +474,30 @@ type SelectorPropsNonClearable<changeAction?: (value: string) => void | Promise<void>;};-type SelectorPropsClearable<- T extends SelectorOptionType = SelectorOptionType,-> = SelectorPropsBase<T> & {- /**- * Whether to show a clear button when a value is selected.- * When clicked, resets the value to `null` and returns focus to the trigger.- *- * When enabled, `value` and `onChange` widen to include `null`.- */- hasClear: true;- value: string | null;- onChange?: (value: string | null) => void;- changeAction?: (value: string | null) => void | Promise<void>;-};--export type SelectorProps<- T extends SelectorOptionType = SelectorOptionType,-> = SelectorPropsNonClearable<T> | SelectorPropsClearable<T>;+type SelectorPropsClearable<T extends SelectorOptionType = SelectorOptionType> =+ SelectorPropsBase<T> & {+ /**+ * Whether to show a clear button when a value is selected.+ * When clicked, resets the value to `null` and returns focus to the trigger.+ *+ * When enabled, `value` and `onChange` widen to include `null`.+ */+ hasClear: true;+ value: string | null;+ onChange?: (value: string | null) => void;+ changeAction?: (value: string | null) => void | Promise<void>;+ };++export type SelectorProps<T extends SelectorOptionType = SelectorOptionType> =+ | SelectorPropsNonClearable<T>+ | SelectorPropsClearable<T>;/*** Default option renderer*/function DefaultOption({option}: {option: SelectorOptionData}) {return (- <SelectorOption- icon={option.icon}- label={option.label ?? option.value}- />+ <SelectorOption icon={option.icon} label={option.label ?? option.value} />);}@@ -717,16 +713,30 @@ export function Selector<T extends SelectorOptionType>(<inputref={searchRef}id={searchId}- role="searchbox"+ // When hasSearch is set, focus moves into this input on open, so it —+ // not the trigger — must be the combobox that reports the highlighted+ // option via aria-activedescendant (comboboxes-4). A bare searchbox+ // left the highlight silent to screen readers.+ role="combobox"+ aria-expanded={popover.isOpen}aria-controls={listboxId}+ aria-autocomplete="list"+ aria-activedescendant={+ popover.isOpen && highlightedIndex >= 0+ ? getItemId(highlightedIndex)+ : undefined+ }aria-label="Search options"type="text"value={searchQuery}onChange={e => setSearchQuery(e.target.value)}onKeyDown={e => {+ // Arrow keys navigate options; Enter selects; Escape/Tab close.+ // Home/End are left to the input for caret movement.if (e.key === 'ArrowDown' ||e.key === 'ArrowUp' ||+ e.key === 'Enter' ||e.key === 'Escape' ||e.key === 'Tab') {@@ -745,6 +755,9 @@ export function Selector<T extends SelectorOptionType>(searchQuery,searchPlaceholder,onKeyDown,+ popover.isOpen,+ highlightedIndex,+ getItemId,]);// Render an individual item@@ -812,9 +825,7 @@ export function Selector<T extends SelectorOptionType>(const option = options[i];if (isDivider(option)) {- elements.push(- <Divider key={`divider-${i}`} xstyle={styles.divider} />,- );+ elements.push(<Divider key={`divider-${i}`} xstyle={styles.divider} />);} else if (isSection(option)) {const sectionItems: ReactNode[] = [];for (const opt of option.options) {@@ -892,13 +903,16 @@ export function Selector<T extends SelectorOptionType>(ref={triggerRef}id={triggerId}type="button"- role="combobox"+ // In hasSearch mode the popup's search input is the combobox (it owns+ // focus + aria-activedescendant, comboboxes-4), so the trigger is a+ // plain button that opens the listbox — not a second combobox.+ role={hasSearch ? undefined : 'combobox'}{...rest}aria-haspopup="listbox"aria-expanded={popover.isOpen}aria-controls={listboxId}aria-activedescendant={- popover.isOpen && highlightedIndex >= 0+ !hasSearch && popover.isOpen && highlightedIndex >= 0? getItemId(highlightedIndex): undefined}