Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Selector): a searchable trigger's name carries the selected value (#10170)

Cindy Zhangb3bbc3856919
diff --git a/.changeset/selector-search-trigger-value.md b/.changeset/selector-search-trigger-value.md
new file mode 100644
index 0000000000..c8dffaaeb5
--- /dev/null
+++ b/.changeset/selector-search-trigger-value.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A searchable `Selector`'s closed trigger now names the selected option after its label, so a screen reader announces the selection (for example "Team, Banana, button, collapsed").
+
+@cixzhang
diff --git a/docs/specs/components/Selector/Selector.spec.md b/docs/specs/components/Selector/Selector.spec.md
index 4271e5c78a..c337dd5a36 100644
--- a/docs/specs/components/Selector/Selector.spec.md
+++ b/docs/specs/components/Selector/Selector.spec.md
@@ -210,7 +210,10 @@ These requirements describe shipped behavior on current `main`.
touch presentation exposes a modal BottomSheet dialog containing the listbox
and the trigger uses `aria-haspopup="dialog"`. In every presentation the
listbox's accessible name is the field's label (`label`), not the trigger's
- value.
+ value. With `hasSearch`, the editable trigger is a button, and the search
+ input owns the combobox. The closed trigger's accessible name is its label
+ followed by the selected value's text. With nothing selected, that text is
+ the placeholder. A caller `aria-label` takes the label's place in that name, and a caller `aria-labelledby` outranks it, as on the combobox shape.
- **AR2 — Focus follows the active interaction model.** The anchored presentation
keeps the combobox relationship at the trigger/search control. The modal touch
presentation moves focus into its search control or listbox. On close, focus
@@ -348,19 +351,19 @@ new theming target.
## Verification map
-| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |
-| ---------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- |
-| FR1, AR2, AR3 | `Selector.test.tsx` selection, focus, and keyboard suites, including "leaves focus on the field a press moved it to" and "returns focus to the trigger when the press lands on nothing focusable" | closed/open, search/non-search, disabled option | Removing selection wiring, focus movement, or keyboard behavior fails the named interaction tests | `audit:Selector/behavior` |
-| FR2 | placement and selected-item geometry tests | default, explicit, RTL, transformed entry | Using the wrong positioning model fails the expected position-area or margin | `audit:Selector/behavior` |
-| FR3, AV1 | Focused `Selector.test.tsx` computed-display checks plus guarded Chromium Storybook matrix and stable visual stories | popover/bottom sheet, narrow/wide, start/end, LTR/RTL, selected/unselected, check/radio | Empty output reserves width, visible output collapses, logical placement flips incorrectly, or row content overflows | `audit:Selector/design-rendered` |
-| FR4, AR1, AR2 | presentation tests plus `aria-haspopup` source review | pointer, compact coarse pointer, search/non-search | Wrong Popover/dialog roles or focus destinations fail tests; `aria-haspopup` values require source/a11y review | `audit:Selector/accessibility` |
-| AR1, AV2 | `Selector.listbox.a11y.test.tsx` and `Listbox.a11y.chromium.spec.ts` against the shared Listbox contract; `SelectorListboxName.a11y.chromium.spec.ts` and `Selector.test.tsx` "names the listbox with the field label, not the trigger that shows the value" for the listbox name | popover/sheet, flat/grouped, selected/unselected, disabled, search, loading, custom RTL | A new or wider loss of role, listbox/option name, state, or ownership gates | `audit:Selector/accessibility` |
-| FR5, FR6, AR1, AR2 (anchored), AR4 | `Selector.combobox.a11y.test.tsx` and `Selector.combobox.a11y.chromium.spec.ts` against the shared Combobox contract | closed, open with active option, read-only, busy, bottom-sheet open, search input with active option | The trigger or search input loses its role/name/state or popup kind, its open popup or active option relationship breaks, or read-only exposes a selection surface; the bottom-sheet trigger's `aria-controls` naming the inner listbox remains exact known debt | `audit:Selector/accessibility` |
-| FR5 | loading, empty-state, and live-region tests | empty options, unmatched search, loading | Empty/no-results output appears or is announced while loading | `audit:Selector/behavior` |
-| FR6, AR4 | read-only interaction, form, ARIA, and theme-state tests | search/non-search, clearable, open→read-only, disabled precedence | A value changes, popup or edit affordance remains, form value disappears, or read-only semantics are absent | `audit:Selector/accessibility` |
-| FR7 | `Selector.test.tsx` press model suite | finger release over another option, finger/mouse release outside, listbox marker | The press option is selected, the listbox takes focus, a finger release outside closes the list, or the stray click selects | `audit:Selector/behavior` |
-| AR5 | `Selector.activeOption.test.tsx`: the ring after keyboard input, no ring after pointer input, the ring again when keyboard input follows a pointer highlight; the active option's indicator measured against the listbox in light and dark | keyboard-active option, pointer-active option | Removing the ring, or drawing it after pointer input, fails; an indicator under 3:1 against the listbox in either mode fails (the fill alone measures 1.09:1 light and 1.15:1 dark) | `audit:Selector/accessibility` |
-| source-build contract | `Selector.source-build.test.mjs` | package source compiled by consumer Babel | Moving evaluated StyleX values outside the supported source form fails compilation | `audit:Selector/code-health` |
+| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |
+| ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- |
+| FR1, AR2, AR3 | `Selector.test.tsx` selection, focus, and keyboard suites, including "leaves focus on the field a press moved it to" and "returns focus to the trigger when the press lands on nothing focusable" | closed/open, search/non-search, disabled option | Removing selection wiring, focus movement, or keyboard behavior fails the named interaction tests | `audit:Selector/behavior` |
+| FR2 | placement and selected-item geometry tests | default, explicit, RTL, transformed entry | Using the wrong positioning model fails the expected position-area or margin | `audit:Selector/behavior` |
+| FR3, AV1 | Focused `Selector.test.tsx` computed-display checks plus guarded Chromium Storybook matrix and stable visual stories | popover/bottom sheet, narrow/wide, start/end, LTR/RTL, selected/unselected, check/radio | Empty output reserves width, visible output collapses, logical placement flips incorrectly, or row content overflows | `audit:Selector/design-rendered` |
+| FR4, AR1, AR2 | presentation tests plus `aria-haspopup` source review | pointer, compact coarse pointer, search/non-search | Wrong Popover/dialog roles or focus destinations fail tests; `aria-haspopup` values require source/a11y review | `audit:Selector/accessibility` |
+| AR1, AV2 | `Selector.listbox.a11y.test.tsx` and `Listbox.a11y.chromium.spec.ts` against the shared Listbox contract; `SelectorListboxName.a11y.chromium.spec.ts` and `Selector.test.tsx` "names the listbox with the field label, not the trigger that shows the value" for the listbox name; `Selector.test.tsx` "Selector with hasSearch: the closed trigger names the selection" for the button trigger's name; `SelectorListboxName.a11y.chromium.spec.ts` "the closed searchable trigger is named by the label and the selected value" for the accessibility tree | popover/sheet, flat/grouped, selected/unselected, disabled, search, loading, custom RTL | A new or wider loss of role, listbox/option name, state, or ownership gates | `audit:Selector/accessibility` |
+| FR5, FR6, AR1, AR2 (anchored), AR4 | `Selector.combobox.a11y.test.tsx` and `Selector.combobox.a11y.chromium.spec.ts` against the shared Combobox contract | closed, open with active option, read-only, busy, bottom-sheet open, search input with active option | The trigger or search input loses its role/name/state or popup kind, its open popup or active option relationship breaks, or read-only exposes a selection surface; the bottom-sheet trigger's `aria-controls` naming the inner listbox remains exact known debt | `audit:Selector/accessibility` |
+| FR5 | loading, empty-state, and live-region tests | empty options, unmatched search, loading | Empty/no-results output appears or is announced while loading | `audit:Selector/behavior` |
+| FR6, AR4 | read-only interaction, form, ARIA, and theme-state tests | search/non-search, clearable, open→read-only, disabled precedence | A value changes, popup or edit affordance remains, form value disappears, or read-only semantics are absent | `audit:Selector/accessibility` |
+| FR7 | `Selector.test.tsx` press model suite | finger release over another option, finger/mouse release outside, listbox marker | The press option is selected, the listbox takes focus, a finger release outside closes the list, or the stray click selects | `audit:Selector/behavior` |
+| AR5 | `Selector.activeOption.test.tsx`: the ring after keyboard input, no ring after pointer input, the ring again when keyboard input follows a pointer highlight; the active option's indicator measured against the listbox in light and dark | keyboard-active option, pointer-active option | Removing the ring, or drawing it after pointer input, fails; an indicator under 3:1 against the listbox in either mode fails (the fill alone measures 1.09:1 light and 1.15:1 dark) | `audit:Selector/accessibility` |
+| source-build contract | `Selector.source-build.test.mjs` | package source compiled by consumer Babel | Moving evaluated StyleX values outside the supported source form fails compilation | `audit:Selector/code-health` |
## Decision log
diff --git a/packages/core/src/Field/__tests__/SearchKeyboard.interaction.browser.spec.ts b/packages/core/src/Field/__tests__/SearchKeyboard.interaction.browser.spec.ts
index 701ed17474..50098025c3 100644
--- a/packages/core/src/Field/__tests__/SearchKeyboard.interaction.browser.spec.ts
+++ b/packages/core/src/Field/__tests__/SearchKeyboard.interaction.browser.spec.ts
@@ -31,7 +31,8 @@ async function expectSearchKeyboard(input: Locator) {
async function openSearchRow(page: Page, story: string, label: string) {
await openStory(page, storybook(), story);
- const trigger = page.getByRole('button', {name: label, exact: true});
+ // A searchable trigger's name is its label followed by its value text.
+ const trigger = page.getByRole('button', {name: new RegExp(`^${label}\\b`)});
await expect(trigger).toBeVisible();
await trigger.tap();
return page.getByRole('combobox');
diff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsx
index 2aea79d351..39db61be01 100644
--- a/packages/core/src/Selector/Selector.test.tsx
+++ b/packages/core/src/Selector/Selector.test.tsx
@@ -137,6 +137,10 @@ const h = {hidden: true} as const;
const OPTIONS = ['Apple', 'Banana', 'Cherry'];
+// A searchable trigger's name is the label followed by the value text, so the
+// "Fruit" field's trigger is found by the start of its name.
+const SEARCH_TRIGGER_NAME = /^Fruit\b/;
+
// Mirrors useTypeahead's default resetMs — how long the typed buffer survives.
const TYPEAHEAD_RESET_MS = 750;
@@ -792,7 +796,7 @@ describe('Selector', () => {
);
// In hasSearch mode the trigger is a plain button, not a combobox.
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
const popover = screen
.getByRole('listbox', {hidden: true})
.closest('[popover]') as HTMLElement;
@@ -962,7 +966,7 @@ describe('Selector', () => {
}
const user = userEvent.setup();
render(<Controlled />);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
const search = screen.getByRole('combobox', {
name: 'Search options',
hidden: true,
@@ -1073,7 +1077,7 @@ describe('Selector', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
expect(screen.getByRole('combobox', h)).toBeInTheDocument();
});
@@ -1089,7 +1093,7 @@ describe('Selector', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
const search = screen.getByRole('combobox', h);
expect(search).toHaveAttribute('enterkeyhint', 'search');
expect(search).toHaveAttribute('autocorrect', 'off');
@@ -1115,7 +1119,9 @@ describe('Selector', () => {
hasSearch
/>,
);
- const triggerBtn = screen.getByRole('button', {name: 'Fruit'});
+ const triggerBtn = screen.getByRole('button', {
+ name: SEARCH_TRIGGER_NAME,
+ });
// In hasSearch mode the trigger is a plain button, not a combobox.
expect(triggerBtn).not.toHaveAttribute('role', 'combobox');
await user.click(triggerBtn);
@@ -1134,7 +1140,7 @@ describe('Selector', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
const search = screen.getByRole('combobox', h);
// Filter to Apple and Banana so "last" means last *visible* option.
await user.type(search, 'a');
@@ -1160,7 +1166,7 @@ describe('Selector', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
const search = screen.getByRole<HTMLInputElement>('combobox', h);
await user.type(search, 'an');
expect(search.selectionStart).toBe(2);
@@ -1202,7 +1208,7 @@ describe('Selector', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
await user.type(screen.getByRole('combobox', h), 'ban');
const options = screen.getAllByRole('option', h);
expect(options).toHaveLength(1);
@@ -1220,7 +1226,7 @@ describe('Selector', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
await user.type(screen.getByRole('combobox', h), 'xyz');
expect(screen.queryAllByRole('option', h)).toHaveLength(0);
// Scope to the listbox: the polite live region also announces "No results
@@ -1241,7 +1247,7 @@ describe('Selector', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
await user.type(screen.getByRole('combobox', h), 'xyz');
// role="listbox" only permits option/group children — the visual
@@ -1264,7 +1270,7 @@ describe('Selector', () => {
emptySearchText="Nothing like that here"
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
await user.type(screen.getByRole('combobox', h), 'xyz');
const listbox = screen.getByRole('listbox', h);
@@ -1287,7 +1293,7 @@ describe('Selector', () => {
emptySearchText="Nothing like that here"
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
const listbox = screen.getByRole('listbox', h);
expect(within(listbox).getByText('No options')).toBeInTheDocument();
@@ -1308,7 +1314,7 @@ describe('Selector', () => {
emptySearchText="Nothing like that here"
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
await enterQuery(user, screen.getByRole('combobox', h), 'xyz');
// The panel message is role="presentation", so the live region is the
@@ -1339,7 +1345,7 @@ describe('Selector', () => {
}
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
await enterQuery(user, screen.getByRole('combobox', h), 'xyz');
await waitFor(() =>
@@ -1390,7 +1396,7 @@ describe('Selector', () => {
emptySearchText={<Message />}
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
await enterQuery(user, screen.getByRole('combobox', h), 'xyz');
await waitFor(() =>
@@ -1494,7 +1500,7 @@ describe('Selector', () => {
emptySearchText="Nothing like that here"
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
await user.type(screen.getByRole('combobox', h), 'xyz');
// useAnnounce writes on the next animation frame, so an immediate read
@@ -1531,7 +1537,7 @@ describe('Selector', () => {
// Typing on the closed trigger opens the popup and seeds the query. That
// path bypasses the search input's own change handler, so it has to
// announce for itself.
- const trigger = screen.getByRole('button', {name: 'Fruit'});
+ const trigger = screen.getByRole('button', {name: SEARCH_TRIGGER_NAME});
trigger.focus();
fireEvent.keyDown(trigger, {key: 'z'});
@@ -1611,7 +1617,7 @@ describe('Selector', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
await user.type(screen.getByRole('combobox', h), 'ban');
await user.click(screen.getByRole('option', {name: /Banana/, ...h}));
expect(onChange).toHaveBeenCalledWith('Banana');
@@ -1634,7 +1640,7 @@ describe('Selector', () => {
// 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'});
+ const trigger = screen.getByRole('button', {name: SEARCH_TRIGGER_NAME});
await user.click(trigger);
expect(trigger).toHaveAttribute('aria-expanded', 'true');
@@ -1654,7 +1660,7 @@ describe('Selector', () => {
searchPlaceholder="Find a fruit..."
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
expect(
screen.getByPlaceholderText('Find a fruit...'),
).toBeInTheDocument();
@@ -1671,7 +1677,7 @@ describe('Selector', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
const search = screen.getByRole('combobox', h);
// Filter to Banana and highlight it so an unguarded Enter would commit a
// selection.
@@ -1714,7 +1720,9 @@ describe('Selector', () => {
/>
</InternationalizationProvider>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(
+ screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}),
+ );
// "a" matches Apple and Banana.
await user.type(screen.getByRole('combobox', h), 'a');
await waitFor(() => {
@@ -1743,7 +1751,9 @@ describe('Selector', () => {
/>
</InternationalizationProvider>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(
+ screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}),
+ );
// "ban" matches only Banana. Exact, so "1 résultats" would fail.
await enterQuery(user, screen.getByRole('combobox', h), 'ban');
await waitFor(() => {
@@ -1768,7 +1778,9 @@ describe('Selector', () => {
/>
</InternationalizationProvider>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(
+ screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}),
+ );
await enterQuery(user, screen.getByRole('combobox', h), 'xyz');
await waitFor(() => {
expect(politeRegion()?.textContent).toBe('Aucun résultat');
@@ -1789,7 +1801,9 @@ describe('Selector', () => {
// Popover closed: nothing announced.
expect(politeRegion()?.textContent ?? '').toBe('');
// Open with an empty query: still nothing announced.
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(
+ screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}),
+ );
expect(politeRegion()?.textContent ?? '').toBe('');
});
});
@@ -1824,7 +1838,9 @@ describe('Selector', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(
+ screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}),
+ );
// "orange" only matches within the Citrus group.
await user.type(screen.getByRole('combobox', h), 'orange');
@@ -1846,7 +1862,9 @@ describe('Selector', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(
+ screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}),
+ );
// "berry" only matches items inside the Berries group.
await user.type(screen.getByRole('combobox', h), 'berry');
@@ -1869,7 +1887,9 @@ describe('Selector', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(
+ screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}),
+ );
const search = screen.getByRole('combobox', h);
// "berry" leaves Strawberry, Blueberry (in that document order).
await user.type(search, 'berry');
@@ -1894,7 +1914,9 @@ describe('Selector', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(
+ screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}),
+ );
await user.type(screen.getByRole('combobox', h), 'zzz');
expect(screen.queryAllByRole('option', h)).toHaveLength(0);
expect(
@@ -3061,7 +3083,7 @@ describe('Selector', () => {
const {rerender} = render(selector(false));
const editableTrigger = screen.getByRole(
hasSearch ? 'button' : 'combobox',
- {name: 'Fruit'},
+ {name: hasSearch ? SEARCH_TRIGGER_NAME : 'Fruit'},
);
await user.click(editableTrigger);
if (hasSearch) {
@@ -3096,7 +3118,7 @@ describe('Selector', () => {
const {rerender} = render(selector(false));
const editableTrigger = screen.getByRole(
hasSearch ? 'button' : 'combobox',
- {name: 'Fruit'},
+ {name: hasSearch ? SEARCH_TRIGGER_NAME : 'Fruit'},
);
await user.click(editableTrigger);
const dialog = await screen.findByRole('dialog', {name: 'Fruit'});
@@ -3152,7 +3174,9 @@ describe('Selector', () => {
);
const {container, rerender} = render(selector(false));
const getTrigger = () =>
- screen.getByRole(hasSearch ? 'button' : 'combobox', {name: 'Fruit'});
+ screen.getByRole(hasSearch ? 'button' : 'combobox', {
+ name: hasSearch ? SEARCH_TRIGGER_NAME : 'Fruit',
+ });
const snapshot = () => {
const trigger = getTrigger();
const root = container.querySelector('.astryx-selector');
@@ -3451,7 +3475,7 @@ describe('Selector empty-state theme target', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
await user.type(screen.getByRole('combobox', h), 'xyz');
const empty = container.querySelector('.astryx-selector-empty-state');
@@ -3769,7 +3793,7 @@ describe('Selector search focus ring', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
await waitForSearchFocus();
expect(field()).not.toHaveAttribute('data-keyboard-focus');
});
@@ -3785,7 +3809,7 @@ describe('Selector search focus ring', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
await waitForSearchFocus();
await user.keyboard('an');
// Typing does not retroactively make a pointer focus a keyboard one; the
@@ -3821,7 +3845,7 @@ describe('Selector search focus ring', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
await waitForSearchFocus();
const search = screen.getByRole('combobox', {hidden: true});
await user.type(search, 'a');
@@ -3852,7 +3876,7 @@ describe('Selector search affordances', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
const search = screen.getByRole('combobox', {hidden: true});
// The row is the outer gutter; the input sits inside the rounded field.
@@ -3897,7 +3921,7 @@ describe('Selector search affordances', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
const search = screen.getByRole('combobox', {hidden: true});
// The magnifier leads the search row, as a sibling of the <input>.
const container = search.parentElement;
@@ -3919,7 +3943,7 @@ describe('Selector search affordances', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
const search = screen.getByRole('combobox', {hidden: true});
await user.type(search, 'ap');
expect(search).toHaveValue('ap');
@@ -3950,7 +3974,7 @@ describe('Selector search affordances', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
const search = screen.getByRole<HTMLInputElement>('combobox', {
hidden: true,
});
@@ -3991,7 +4015,7 @@ describe('Selector search affordances', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
expect(
screen.queryByRole('button', {
name: 'Clear Search options',
@@ -4011,7 +4035,7 @@ describe('Selector search affordances', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
// Exactly one combobox — the input. The magnifier and clear button are not
// part of the combobox contract.
const comboboxes = screen.getAllByRole('combobox', {hidden: true});
@@ -4031,8 +4055,8 @@ describe('Selector search affordances', () => {
hasSearch
/>,
);
- await user.click(screen.getByRole('button', {name: 'Fruit'}));
- const trigger = screen.getByRole('button', {name: 'Fruit'});
+ await user.click(screen.getByRole('button', {name: SEARCH_TRIGGER_NAME}));
+ const trigger = screen.getByRole('button', {name: SEARCH_TRIGGER_NAME});
const search = screen.getByRole('combobox', {hidden: true});
await user.type(search, 'ap');
expect(search).toHaveFocus();
@@ -4989,7 +5013,7 @@ describe('Selector disabled selection transitions', () => {
}
const {rerender} = render(<ControlledSelector isDisabled={false} />);
const trigger = screen.getByRole(hasSearch ? 'button' : 'combobox', {
- name: 'Fruit',
+ name: hasSearch ? SEARCH_TRIGGER_NAME : 'Fruit',
});
const form = screen.getByRole<HTMLFormElement>('form', {
name: 'Fruit form',
@@ -5475,3 +5499,94 @@ describe('Selector caller aria-labelledby', () => {
expect(trigger).not.toHaveAttribute('aria-labelledby');
});
});
+
+// A searchable Selector's closed trigger is a button, so a screen reader
+// reads its name, not a value. The name carries the selection.
+describe('Selector with hasSearch: the closed trigger names the selection', () => {
+ it('names the trigger with the label and the selected option', () => {
+ render(
+ <Selector
+ label="Team"
+ options={OPTIONS}
+ value="Banana"
+ onChange={() => {}}
+ hasSearch
+ />,
+ );
+ expect(screen.getByRole('button', {name: 'Team Banana'})).toHaveAttribute(
+ 'aria-expanded',
+ 'false',
+ );
+ });
+
+ it('names the trigger with the label and the placeholder when nothing is selected', () => {
+ render(
+ <Selector
+ label="Team"
+ options={OPTIONS}
+ placeholder="Pick one"
+ hasSearch
+ />,
+ );
+ expect(
+ screen.getByRole('button', {name: 'Team Pick one'}),
+ ).toBeInTheDocument();
+ });
+
+ it('names the trigger with the label alone when the value text is empty', () => {
+ render(
+ <Selector label="Team" options={OPTIONS} placeholder="" hasSearch />,
+ );
+ expect(screen.getByRole('button', {name: 'Team'})).toBeInTheDocument();
+ });
+
+ it('keeps a caller aria-label as the first part of the name', () => {
+ render(
+ <Selector
+ label="Team"
+ aria-label="Pick a team"
+ options={OPTIONS}
+ value="Banana"
+ onChange={() => {}}
+ hasSearch
+ />,
+ );
+ expect(
+ screen.getByRole('button', {name: 'Pick a team Banana'}),
+ ).toBeInTheDocument();
+ });
+
+ it('keeps a caller aria-labelledby ahead of a caller aria-label, as the combobox shape does', () => {
+ render(
+ <>
+ <span id="lunch">Lunch</span>
+ <Selector
+ label="Team"
+ aria-label="Pick a team"
+ aria-labelledby="lunch"
+ options={OPTIONS}
+ value="Banana"
+ onChange={() => {}}
+ hasSearch
+ />
+ </>,
+ );
+ expect(
+ screen.getByRole('button', {name: 'Team Lunch Banana'}),
+ ).toBeInTheDocument();
+ });
+
+ it('keeps the combobox shape named by its label, with the value as its value', () => {
+ render(
+ <Selector
+ label="Team"
+ options={OPTIONS}
+ value="Banana"
+ onChange={() => {}}
+ />,
+ );
+ expect(screen.getByRole('combobox', {name: 'Team'})).toHaveTextContent(
+ 'Banana',
+ );
+ });
+});
diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsx
index 458adc9eca..2a3deb12f5 100644
--- a/packages/core/src/Selector/Selector.tsx
+++ b/packages/core/src/Selector/Selector.tsx
@@ -1197,6 +1197,7 @@ export function Selector<T extends SelectorOptionType>(
const inputLabelId = useId();
const readOnlyDescriptionId = useId();
const searchId = useId();
+ const valueTextId = useId();
// Measure from the same outer control that usePopover anchors to; using the
// shorter inner button makes every size's selected row land too low.
const anchorRef = useRef<HTMLDivElement>(null);
@@ -1983,6 +1984,7 @@ export function Selector<T extends SelectorOptionType>(
selectedItem && renderValue ? (
<SelectorRowLayoutContext value={rowLayout}>
<span
+ id={valueTextId}
{...stylex.props(
styles.triggerValue,
inputGroup && styles.triggerValueInGroup,
@@ -1995,12 +1997,28 @@ export function Selector<T extends SelectorOptionType>(
{!startIcon &&
selectedItem?.icon != null &&
renderIconSlot(selectedItem.icon, {size: 'sm', color: 'secondary'})}
- <span {...stylex.props(styles.triggerLabel)}>
+ <span id={valueTextId} {...stylex.props(styles.triggerLabel)}>
{selectedItem?.label ?? placeholder}
</span>
</>
);
+ // A screen reader announces no value for a button. So the searchable
+ // trigger's name holds the value text after the label. A standalone label
+ // needs an id for this reference. A caller aria-label stays the name's first
+ // part: the trigger refers to itself. A caller aria-labelledby outranks it,
+ // as it does on the combobox shape.
+ const callerAriaLabel = (rest as {'aria-label'?: string})['aria-label'];
+ const triggerNameLabelId =
+ callerAriaLabel != null && ariaLabelledByProp == null
+ ? triggerId
+ : (triggerLabelledBy ?? (inputGroup ? undefined : fieldLabelId));
+ const isValueInTriggerName =
+ hasSearch &&
+ !isEffectivelyReadOnly &&
+ triggerNameLabelId != null &&
+ (selectedItem != null || placeholder != null);
+
const panelContent = hasSearch ? (
<div {...stylex.props(styles.searchPanel)}>
{renderSearch()}
@@ -2104,7 +2122,9 @@ export function Selector<T extends SelectorOptionType>(
'aria-describedby':
[ariaDescribedByProp, ariaDescribedBy].filter(Boolean).join(' ') ||
undefined,
- 'aria-labelledby': triggerLabelledBy,
+ 'aria-labelledby': isValueInTriggerName
+ ? joinAriaIDs(triggerNameLabelId, valueTextId)
+ : triggerLabelledBy,
'aria-required': isEffectivelyRequired ? 'true' : undefined,
'aria-invalid': status?.type === 'error' ? 'true' : undefined,
'aria-busy': isBusy || undefined,
@@ -2325,7 +2345,11 @@ export function Selector<T extends SelectorOptionType>(
isLabelHidden={isLabelHidden}
description={description}
inputID={triggerId}
- labelID={ariaLabelledByProp != null ? fieldLabelId : undefined}
+ labelID={
+ ariaLabelledByProp != null || isValueInTriggerName
+ ? fieldLabelId
+ : undefined
+ }
descriptionID={description ? descriptionId : undefined}
isOptional={isOptional}
isRequired={isRequired}
diff --git a/packages/core/src/Selector/__tests__/SelectorListboxName.a11y.chromium.spec.ts b/packages/core/src/Selector/__tests__/SelectorListboxName.a11y.chromium.spec.ts
index c5d7c8b546..c2c1a36016 100644
--- a/packages/core/src/Selector/__tests__/SelectorListboxName.a11y.chromium.spec.ts
+++ b/packages/core/src/Selector/__tests__/SelectorListboxName.a11y.chromium.spec.ts
@@ -7,7 +7,8 @@
* value selected
* @output Chromium's own accessibility tree names the open listbox by the
* field's label, never by the trigger's value, in the popover and the
- * bottom sheet.
+ * bottom sheet, and names the closed searchable trigger by the label and
+ * the selected value.
* @position Real-browser check: a modal sheet and the trigger reference
* behave differently in jsdom.
*/
@@ -56,6 +57,25 @@ async function listboxNames(
.map(node => node.name?.value ?? '');
}
+test('the closed searchable trigger is named by the label and the selected value', async ({
+ page,
+}) => {
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=core-selector--searchable&viewMode=story&args=presentation:popover;value:Banana`,
+ {waitUntil: 'load'},
+ );
+ const trigger = page.locator('button[aria-haspopup]').first();
+ await expect(trigger).toContainText('Banana');
+ const cdp = await page.context().newCDPSession(page);
+ const {nodes} = (await cdp.send('Accessibility.getFullAXTree')) as {
+ nodes: {role?: {value?: string}; name?: {value?: string}}[];
+ };
+ const names = nodes
+ .filter(node => node.role?.value === 'button')
+ .map(node => node.name?.value ?? '');
+ expect(names).toContain('Fruit Banana');
+});
+
test('the popover listbox is named by the field label, not the selected value', async ({
page,
}) => {