An open source design system that's fully customizable and agent ready
TypeScript
fix(Selector): derive the trigger id from a caller-supplied `id` so the label and listbox wiring resolves (#5561)
Cindy Zhang055e75f65f5a
diff --git a/.changeset/selector-caller-supplied-trigger-id.md b/.changeset/selector-caller-supplied-trigger-id.mdnew file mode 100644index 0000000000..768c284392--- /dev/null+++ b/.changeset/selector-caller-supplied-trigger-id.md@@ -0,0 +1,11 @@+---+'@astryxdesign/core': patch+---++[fix] Selector: a caller-supplied `id` now drives the trigger's whole identity, not just its `id` attribute++`Selector` generated its trigger id with `useId()` and set it on the trigger button before spreading `...rest`, so a caller's `id` — accepted through `BaseProps` — replaced it on the button while the generated value stayed behind as the target of the listbox's `aria-labelledby` and of the `Field` label's `htmlFor`. Passing `id` therefore left the listbox with no accessible name and the field label pointing at an element that does not exist, silently and with a clean typecheck, lint and build.++The internal identity is now derived from the caller's `id` when there is one, so the button, the listbox's `aria-labelledby` (both the plain and the `hasSearch` panel) and `Field`'s `inputID` all name the same element. The trigger's own `id` attribute is unchanged in every case; what changes is that references which used to dangle now resolve — including the field label, which consequently regains its native click-to-focus behaviour. With no `id` supplied the rendered output is identical to before.++@cixzhangdiff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsxindex 1e09eea49f..c9189a5b62 100644--- a/packages/core/src/Selector/Selector.test.tsx+++ b/packages/core/src/Selector/Selector.test.tsx@@ -2344,6 +2344,55 @@ describe('Selector', () => {});});+describe('Selector caller-supplied id', () => {+ it('names the listbox with the id the caller put on the trigger', () => {+ render(<Selector label="Fruit" options={OPTIONS} id="fruit-picker" />);++ const trigger = screen.getByRole('combobox');+ expect(trigger).toHaveAttribute('id', 'fruit-picker');++ const labelledBy = screen+ .getByRole('listbox', h)+ .getAttribute('aria-labelledby');+ expect(document.getElementById(labelledBy!)).toBe(trigger);+ });++ it('names the search-variant listbox with the caller id', () => {+ render(+ <Selector label="Fruit" options={OPTIONS} id="fruit-picker" hasSearch />,+ );++ const trigger = document.getElementById('fruit-picker');+ expect(trigger?.tagName).toBe('BUTTON');++ const labelledBy = screen+ .getByRole('listbox', h)+ .getAttribute('aria-labelledby');+ expect(document.getElementById(labelledBy!)).toBe(trigger);+ });++ it('points the field label at the caller id', () => {+ render(<Selector label="Fruit" options={OPTIONS} id="fruit-picker" />);++ const trigger = screen.getByRole('combobox', {name: 'Fruit'});+ expect(trigger).toHaveAttribute('id', 'fruit-picker');+ expect(screen.getByLabelText('Fruit')).toBe(trigger);+ });++ it('generates the trigger id when the caller supplies none', () => {+ render(<Selector label="Fruit" options={OPTIONS} />);++ const trigger = screen.getByRole('combobox', {name: 'Fruit'});+ expect(trigger.id).not.toBe('');+ expect(screen.getByLabelText('Fruit')).toBe(trigger);++ const labelledBy = screen+ .getByRole('listbox', h)+ .getAttribute('aria-labelledby');+ expect(document.getElementById(labelledBy!)).toBe(trigger);+ });+});+describe('Selector statusVariant forwarding', () => {it('defaults to attached (status renders with data-variant="attached")', () => {const {container} = render(diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsxindex c60813d0cd..21deaaf47e 100644--- a/packages/core/src/Selector/Selector.tsx+++ b/packages/core/src/Selector/Selector.tsx@@ -823,6 +823,7 @@ export function Selector<T extends SelectorOptionType>(className,style,hasClear: hasClearProp,+ id,...rest} = props as SelectorPropsClearable<T>;const isEffectivelyRequired = useResolvedRequired({isRequired, isOptional});@@ -841,7 +842,10 @@ export function Selector<T extends SelectorOptionType>(// Normalize null to undefined for internal use (null is the clear sentinel)const normalizedValue = value === null ? undefined : value;- const triggerId = useId();+ const generatedTriggerId = useId();+ // A caller's `id` lands on the trigger either way, so the internal identity+ // has to be that same value or the label and listbox point at nothing.+ const triggerId = id ?? generatedTriggerId;const listboxId = useId();const descriptionId = useId();const statusMessageId = useId();