An open source design system that's fully customizable and agent ready
TypeScript
fix(Selector): keep a caller aria-labelledby on the trigger (#8710)
홍규진619e4a52982a
diff --git a/.changeset/selector-caller-labelledby.md b/.changeset/selector-caller-labelledby.mdnew file mode 100644index 0000000000..b2ef9d5ac4--- /dev/null+++ b/.changeset/selector-caller-labelledby.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Selector: a caller's `aria-labelledby` now reaches the trigger. Before, Selector replaced it with its own value, so the trigger lost the extra name (standalone it had no `aria-labelledby` at all). Now the caller's ids follow the field's own label: `label="Fruit" aria-labelledby="lunch-heading"` names the trigger "Fruit Lunch", and inside an InputGroup the group label comes first. Triggers without a caller `aria-labelledby` are unchanged. No API changes.++@kyu-rongdiff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsxindex 274a9bba70..7878ec14ad 100644--- a/packages/core/src/Selector/Selector.test.tsx+++ b/packages/core/src/Selector/Selector.test.tsx@@ -4979,3 +4979,50 @@ describe('Selector and option actions (not adopted)', () => {}});});++describe('Selector caller aria-labelledby', () => {+ const FRUITS = ['Apple', 'Banana', 'Cherry'];++ it('names the trigger by its label and a caller aria-labelledby', () => {+ render(+ <>+ <span id="lunch-heading">Lunch</span>+ <Selector+ label="Fruit"+ options={FRUITS}+ onChange={() => {}}+ aria-labelledby="lunch-heading"+ />+ </>,+ );+ const trigger = screen.getByRole('combobox', {name: 'Fruit Lunch'});+ expect(trigger.getAttribute('aria-labelledby')?.split(' ')).toContain(+ 'lunch-heading',+ );+ });++ it('names a grouped trigger by the group, its label, and a caller aria-labelledby', () => {+ render(+ <>+ <span id="lunch-heading">Lunch</span>+ <InputGroup label="Order">+ <Selector+ label="Fruit"+ options={FRUITS}+ onChange={() => {}}+ aria-labelledby="lunch-heading"+ />+ </InputGroup>+ </>,+ );+ expect(+ screen.getByRole('combobox', {name: 'Order Fruit Lunch'}),+ ).toBeInTheDocument();+ });++ it('leaves a standalone trigger named by its label element without a caller aria-labelledby', () => {+ render(<Selector label="Fruit" options={FRUITS} onChange={() => {}} />);+ const trigger = screen.getByRole('combobox', {name: 'Fruit'});+ expect(trigger).not.toHaveAttribute('aria-labelledby');+ });+});diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsxindex b48b4016f9..77adf71c6e 100644--- a/packages/core/src/Selector/Selector.tsx+++ b/packages/core/src/Selector/Selector.tsx@@ -80,6 +80,7 @@ import {useResolvedRequired} from '../hooks/useResolvedRequired';import {SelectorOption} from './SelectorOption';import {SelectorRowLayoutContext} from './SelectorRowLayoutContext';import {getInputARIA, isImeKeyEvent, mergeProps} from '../utils';+import {joinAriaIDs} from '../utils/inputAria';import {warnOnce} from '../utils/devWarning';import {useSize} from '../SizeContext/SizeContext';import {@@ -990,6 +991,7 @@ export function Selector<T extends SelectorOptionType>(onFocus,onKeyDown: onKeyDownProp,'aria-describedby': ariaDescribedByProp,+ 'aria-labelledby': ariaLabelledByProp,hasClear: hasClearProp,id,...restProps@@ -1028,6 +1030,7 @@ export function Selector<T extends SelectorOptionType>(const emptyStateRef = useRef<HTMLDivElement>(null);const descriptionId = useId();const statusMessageId = useId();+ const fieldLabelId = useId();const inputLabelId = useId();const readOnlyDescriptionId = useId();const searchId = useId();@@ -1090,6 +1093,12 @@ export function Selector<T extends SelectorOptionType>(],inputGroup,);+ // A caller aria-labelledby names the trigger together with the field's own+ // label (the Field label standalone, the group and input labels in a group).+ const triggerLabelledBy =+ ariaLabelledByProp != null+ ? joinAriaIDs(ariaLabelledBy ?? fieldLabelId, ariaLabelledByProp)+ : ariaLabelledBy;// Flatten options for keyboard navigationconst selectableItems = useMemo(@@ -1918,7 +1927,7 @@ export function Selector<T extends SelectorOptionType>('aria-describedby':[ariaDescribedByProp, ariaDescribedBy].filter(Boolean).join(' ') ||undefined,- 'aria-labelledby': ariaLabelledBy,+ 'aria-labelledby': triggerLabelledBy,'aria-required': isEffectivelyRequired ? 'true' : undefined,'aria-invalid': status?.type === 'error' ? 'true' : undefined,'aria-busy': isBusy || undefined,@@ -2133,6 +2142,7 @@ export function Selector<T extends SelectorOptionType>(isLabelHidden={isLabelHidden}description={description}inputID={triggerId}+ labelID={ariaLabelledByProp != null ? fieldLabelId : undefined}descriptionID={description ? descriptionId : undefined}isOptional={isOptional}isRequired={isRequired}