An open source design system that's fully customizable and agent ready
TypeScript
fix(MultiSelector): keep a caller aria-labelledby on the trigger (#8711)
홍규진e200d6f279f7
diff --git a/.changeset/multi-selector-caller-labelledby.md b/.changeset/multi-selector-caller-labelledby.mdnew file mode 100644index 0000000000..db87bf149d--- /dev/null+++ b/.changeset/multi-selector-caller-labelledby.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] MultiSelector: a caller's `aria-labelledby` now reaches the built-in trigger. Before, MultiSelector 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="Fruits" aria-labelledby="lunch-heading"` names the trigger "Fruits Lunch", and inside an InputGroup the group label comes first. With `renderTrigger`, the caller's control still receives the caller's `aria-labelledby` as given. Triggers without a caller `aria-labelledby` are unchanged. No API changes.++@kyu-rongdiff --git a/packages/core/src/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsxindex fb6cfc0362..3f27bdf9e4 100644--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx@@ -4603,3 +4603,100 @@ describe('MultiSelector in an InputGroup', () => {expect(groupClasses().some(c => classes.has(c))).toBe(false);});});++describe('MultiSelector 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>+ <MultiSelector+ label="Fruits"+ options={FRUITS}+ value={[]}+ onChange={() => {}}+ aria-labelledby="lunch-heading"+ />+ </>,+ );+ const trigger = screen.getByRole('combobox', {name: 'Fruits 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">+ <MultiSelector+ label="Fruits"+ options={FRUITS}+ value={[]}+ onChange={() => {}}+ aria-labelledby="lunch-heading"+ />+ </InputGroup>+ </>,+ );+ expect(+ screen.getByRole('combobox', {name: 'Order Fruits Lunch'}),+ ).toBeInTheDocument();+ });++ it('leaves a standalone trigger named by its label element without a caller aria-labelledby', () => {+ render(+ <MultiSelector+ label="Fruits"+ options={FRUITS}+ value={[]}+ onChange={() => {}}+ />,+ );+ const trigger = screen.getByRole('combobox', {name: 'Fruits'});+ expect(trigger).not.toHaveAttribute('aria-labelledby');+ });++ it('still hands a caller aria-labelledby to a renderTrigger control unchanged', () => {+ render(+ <>+ <span id="lunch-heading">Lunch</span>+ <MultiSelector+ label="Fruits"+ options={FRUITS}+ value={[]}+ onChange={() => {}}+ aria-labelledby="lunch-heading"+ renderTrigger={props => (+ <button type="button" {...props}>+ Pick fruits+ </button>+ )}+ />+ </>,+ );+ const opener = screen.getByRole('button', {name: 'Lunch'});+ expect(opener).toHaveAttribute('aria-labelledby', 'lunch-heading');+ });++ it("leaves a renderTrigger control's own aria-labelledby in place without a caller value", () => {+ render(+ <>+ <span id="row-title">Lunch fruits</span>+ <MultiSelector+ label="Fruits"+ options={FRUITS}+ value={[]}+ onChange={() => {}}+ renderTrigger={props => (+ <button type="button" aria-labelledby="row-title" {...props} />+ )}+ />+ </>,+ );+ const opener = screen.getByRole('button', {name: 'Lunch fruits'});+ expect(opener).toHaveAttribute('aria-labelledby', 'row-title');+ });+});diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsxindex 105dc0a57d..192d85440e 100644--- a/packages/core/src/MultiSelector/MultiSelector.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.tsx@@ -75,6 +75,7 @@ import {} from '../Selector/utils';import {useMultiCombobox} from './hooks';import {getInputARIA, isImeKeyEvent, mergeProps} from '../utils';+import {joinAriaIDs} from '../utils/inputAria';import {devWarn, warnOnce} from '../utils/devWarning';import {getInteractionModality,@@ -1136,6 +1137,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({onFocus,onKeyDown: onKeyDownProp,'aria-describedby': ariaDescribedByProp,+ 'aria-labelledby': ariaLabelledByProp,...restProps}: MultiSelectorProps<T>) {const inputGroup = useInputGroup();@@ -1183,6 +1185,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({const listboxId = useId();const descriptionId = useId();const statusMessageId = useId();+ const fieldLabelId = useId();const inputLabelId = useId();const readOnlyDescriptionId = useId();const searchId = useId();@@ -1245,6 +1248,12 @@ export function MultiSelector<T extends MultiSelectorOptionType>({],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(@@ -2449,7 +2458,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({'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,@@ -2485,6 +2494,11 @@ export function MultiSelector<T extends MultiSelectorOptionType>({// in this mode; the selector's own wiring below wins over them....rest,'aria-describedby': ariaDescribedByProp,+ // Only a caller value is handed back, so a control that names itself+ // with its own aria-labelledby before spreading keeps it.+ ...(ariaLabelledByProp != null+ ? {'aria-labelledby': ariaLabelledByProp}+ : null),ref: mergeRefs<HTMLElement>(el => {popover.triggerRef(el);triggerRef.current = el;@@ -2741,6 +2755,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({isLabelHidden={isLabelHidden}description={description}inputID={triggerId}+ labelID={ariaLabelledByProp != null ? fieldLabelId : undefined}descriptionID={description ? descriptionId : undefined}isOptional={isOptional}isRequired={isRequired}