An open source design system that's fully customizable and agent ready
TypeScript
fix(TextArea, Typeahead, Tokenizer): keep the field's label with a caller aria-labelledby (#8862)
Ojas Mutreja75001c5b4b63
diff --git a/.changeset/fields-keep-field-label-with-caller-labelledby.md b/.changeset/fields-keep-field-label-with-caller-labelledby.mdnew file mode 100644index 0000000000..5b25a28a13--- /dev/null+++ b/.changeset/fields-keep-field-label-with-caller-labelledby.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] TextArea, Typeahead and Tokenizer: a caller's `aria-labelledby` no longer replaces the field's own label. Before, it was the only name the control had, so `label="Notes" aria-labelledby="billing-heading"` named the textarea "Billing". Now the caller's ids follow the field's own label ("Notes Billing"), as TextInput, NumberInput, Selector and MultiSelector already do. A Typeahead inside an InputGroup, which ignored the caller's ids altogether, is now named by the group, its label, then the caller's ids. The visible label stays in the accessible name (WCAG 2.2 SC 2.5.3 Label in Name; `family:input-fields` FR10.1). Fields without a caller `aria-labelledby` are unchanged. No API changes.++@HelloOjasMutrejadiff --git a/packages/core/src/TextArea/TextArea.test.tsx b/packages/core/src/TextArea/TextArea.test.tsxindex eee5da18ac..31da107519 100644--- a/packages/core/src/TextArea/TextArea.test.tsx+++ b/packages/core/src/TextArea/TextArea.test.tsx@@ -1288,3 +1288,30 @@ describe('TextArea theme target names', () => {expect(root).not.toHaveClass('astryx-textarea');});});++describe('TextArea caller aria-labelledby', () => {+ it('names the textarea by its label and a caller aria-labelledby', () => {+ render(+ <>+ <span id="billing-heading">Billing</span>+ <TextArea+ label="Notes"+ value=""+ onChange={() => {}}+ aria-labelledby="billing-heading"+ />+ </>,+ );+ const textarea = screen.getByRole('textbox', {name: 'Notes Billing'});+ expect(textarea.getAttribute('aria-labelledby')?.split(' ')).toContain(+ 'billing-heading',+ );+ });++ it('leaves a textarea named by its label element without a caller aria-labelledby', () => {+ render(<TextArea label="Notes" value="" onChange={() => {}} />);+ expect(screen.getByRole('textbox', {name: 'Notes'})).not.toHaveAttribute(+ 'aria-labelledby',+ );+ });+});diff --git a/packages/core/src/TextArea/TextArea.tsx b/packages/core/src/TextArea/TextArea.tsxindex a9dd004c46..f74186ac66 100644--- a/packages/core/src/TextArea/TextArea.tsx+++ b/packages/core/src/TextArea/TextArea.tsx@@ -47,6 +47,7 @@ import {Icon, renderIconSlot, type IconType} from '../Icon';import {Spinner} from '../Spinner';import {useTooltip} from '../Tooltip';import {mergeProps} from '../utils';+import {joinAriaIDs} from '../utils/inputAria';import type {BaseProps} from '../BaseProps';import {type FieldInputBaseProps,@@ -417,6 +418,7 @@ export function TextArea({ref,id: idProp,'aria-describedby': ariaDescribedByProp,+ 'aria-labelledby': ariaLabelledByProp,...restProps}: TextAreaProps) {const {rest, container} = useFieldInputStyling('TextArea', restProps, {@@ -430,6 +432,7 @@ export function TextArea({const announce = useAnnounce();const generatedID = useId();const id = idProp ?? generatedID;+ const fieldLabelID = useId();const descriptionID = useId();const statusMessageID = useId();const counterID = useId();@@ -572,6 +575,7 @@ export function TextArea({isLabelHidden={isLabelHidden}description={description}inputID={id}+ labelID={ariaLabelledByProp != null ? fieldLabelID : undefined}descriptionID={description ? descriptionID : undefined}isOptional={isOptional}isRequired={isRequired}@@ -644,6 +648,13 @@ export function TextArea({spellCheck={hasSpellCheck}autoFocus={hasAutoFocus}data-autofocus={hasAutoFocus || undefined}+ // A caller aria-labelledby names the textarea together with the+ // field's own label, which it would otherwise replace.+ aria-labelledby={+ ariaLabelledByProp != null+ ? joinAriaIDs(fieldLabelID, ariaLabelledByProp)+ : undefined+ }aria-describedby={[ariaDescribedByProp, ariaDescribedBy].filter(Boolean).join(' ') ||undefineddiff --git a/packages/core/src/Tokenizer/Tokenizer.test.tsx b/packages/core/src/Tokenizer/Tokenizer.test.tsxindex 07a6542f84..2413e3a960 100644--- a/packages/core/src/Tokenizer/Tokenizer.test.tsx+++ b/packages/core/src/Tokenizer/Tokenizer.test.tsx@@ -2167,3 +2167,23 @@ describe('Tokenizer consumer key handler order', () => {expect(currentTarget).toBe(screen.getByRole('combobox'));});});++describe('Tokenizer caller aria-labelledby', () => {+ it('names the input by its label and a caller aria-labelledby', () => {+ render(+ <>+ <span id="billing-heading">Billing</span>+ <Tokenizer+ label="Members"+ searchSource={userSource}+ value={[]}+ onChange={() => {}}+ aria-labelledby="billing-heading"+ />+ </>,+ );+ expect(+ screen.getByRole('combobox', {name: 'Members Billing'}),+ ).toBeInTheDocument();+ });+});diff --git a/packages/core/src/Tokenizer/Tokenizer.tsx b/packages/core/src/Tokenizer/Tokenizer.tsxindex e31b63816d..005ae1422c 100644--- a/packages/core/src/Tokenizer/Tokenizer.tsx+++ b/packages/core/src/Tokenizer/Tokenizer.tsx@@ -85,6 +85,7 @@ import {} from '../theme/tokens.stylex';import type {SearchableItem, SearchSource} from '../Typeahead/types';import {composeEventHandlers, mergeProps} from '../utils';+import {joinAriaIDs} from '../utils/inputAria';import {themeProps} from '../utils/themeProps';import {useTranslator} from '../i18n';import type {TokenizerBottomSheetProps} from './TokenizerBottomSheet';@@ -754,6 +755,7 @@ export function Tokenizer<T extends SearchableItem>({experimental,id: idProp,'aria-describedby': ariaDescribedByProp,+ 'aria-labelledby': ariaLabelledByProp,...restProps}: TokenizerWithPresentationProps<T>): React.JSX.Element {const {rest, container} = useFieldInputStyling('Tokenizer', restProps, {@@ -768,8 +770,15 @@ export function Tokenizer<T extends SearchableItem>({const size = useSize(sizeProp, 'md');const generatedInputId = useId();const inputId = idProp ?? generatedInputId;+ const fieldLabelId = useId();const descriptionId = useId();const statusMessageId = useId();+ // A caller aria-labelledby names the input together with the field's own+ // label, which it would otherwise replace.+ const fieldLabelledBy =+ ariaLabelledByProp != null+ ? joinAriaIDs(fieldLabelId, ariaLabelledByProp)+ : undefined;const inputRef = useRef<HTMLInputElement>(null);const wrapperRef = useRef<HTMLDivElement>(null);@@ -1391,6 +1400,7 @@ export function Tokenizer<T extends SearchableItem>({isFocusableDisabled={showsDisabledMessage}hasAutoFocus={hasAutoFocus}inputId={inputId}+ ariaLabelledBy={fieldLabelledBy}ariaDescribedBy={[ariaDescribedByProp, ariaDescribedBy].filter(Boolean)@@ -1457,6 +1467,7 @@ export function Tokenizer<T extends SearchableItem>({ref={triggerMergedRef}type="button"id={inputId}+ aria-labelledby={fieldLabelledBy}aria-haspopup="dialog"aria-expanded={isSheetOpen}aria-disabled={isDisabled || undefined}@@ -1707,6 +1718,7 @@ export function Tokenizer<T extends SearchableItem>({isLabelHidden={isLabelHidden}description={description}inputID={inputId}+ labelID={ariaLabelledByProp != null ? fieldLabelId : undefined}descriptionID={description ? descriptionId : undefined}isOptional={isOptional}isRequired={isRequired}diff --git a/packages/core/src/Tokenizer/TokenizerBottomSheet.test.tsx b/packages/core/src/Tokenizer/TokenizerBottomSheet.test.tsxindex 5b129908bc..f1b8078945 100644--- a/packages/core/src/Tokenizer/TokenizerBottomSheet.test.tsx+++ b/packages/core/src/Tokenizer/TokenizerBottomSheet.test.tsx@@ -75,6 +75,7 @@ function Harness({maxEntries?: number;isDisabled?: boolean;disabledMessage?: string;+ 'aria-labelledby'?: string;debounceMs?: number;onFocus?: (event: React.FocusEvent) => void;onBlur?: (event: React.FocusEvent) => void;@@ -238,6 +239,16 @@ describe('Tokenizer experimental.presentation (FR16)', () => {});describe('Tokenizer sheet presentation (single-sheet touch flow)', () => {+ it('names the sheet trigger by its label, then a caller aria-labelledby', () => {+ render(+ <>+ <span id="billing-heading">Billing</span>+ <Harness aria-labelledby="billing-heading" />+ </>,+ );+ expect(screen.getByRole('button', {name: 'Tags Billing'})).toBeTruthy();+ });+it('keeps rows stable while open and applies selected-first order on reopen (FR19)', async () => {const spy = vi.fn();render(<Harness spy={spy} />);@@ -1787,7 +1798,7 @@ describe('Tokenizer sheet presentation (single-sheet touch flow)', () => {// changes until the sheet closes.compact = false;act(() => {- for (const cb of listeners) cb();+ for (const cb of listeners) {cb();}});expect(screen.getByRole('dialog', {name: 'Tags'})).toBe(dialog);expect(trigger).toBeInTheDocument();@@ -2531,7 +2542,7 @@ describe('Tokenizer sheet presentation (single-sheet touch flow)', () => {// the hand-off inside the field reports neither a blur nor a focus.compact = false;act(() => {- for (const cb of listeners) cb();+ for (const cb of listeners) {cb();}});expect(screen.getByRole('combobox')).toHaveFocus();expect(onFocus).toHaveBeenCalledTimes(1);@@ -2539,14 +2550,14 @@ describe('Tokenizer sheet presentation (single-sheet touch flow)', () => {// And back: the input focused, the screen becomes compact again.compact = true;act(() => {- for (const cb of listeners) cb();+ for (const cb of listeners) {cb();}});expect(openTrigger()).toHaveFocus();// Unfocused, a change moves nothing.openTrigger().blur();compact = false;act(() => {- for (const cb of listeners) cb();+ for (const cb of listeners) {cb();}});expect(document.body).toHaveFocus();});diff --git a/packages/core/src/Typeahead/Typeahead.test.tsx b/packages/core/src/Typeahead/Typeahead.test.tsxindex b39d5afea8..df7d7dc5f9 100644--- a/packages/core/src/Typeahead/Typeahead.test.tsx+++ b/packages/core/src/Typeahead/Typeahead.test.tsx@@ -2822,3 +2822,43 @@ describe('Typeahead consumer key handler target', () => {expect(onKeyDown).toHaveBeenCalledOnce();});});++describe('Typeahead caller aria-labelledby', () => {+ it('names the input by its label and a caller aria-labelledby', () => {+ render(+ <>+ <span id="billing-heading">Billing</span>+ <Typeahead+ label="Fruit"+ searchSource={fruitSource}+ value={null}+ onChange={() => {}}+ aria-labelledby="billing-heading"+ />+ </>,+ );+ expect(+ screen.getByRole('combobox', {name: 'Fruit Billing'}),+ ).toBeInTheDocument();+ });++ it('names a grouped input by the group, its label, and a caller aria-labelledby', () => {+ render(+ <>+ <span id="billing-heading">Billing</span>+ <InputGroup label="Contact">+ <Typeahead+ label="Fruit"+ searchSource={fruitSource}+ value={null}+ onChange={() => {}}+ aria-labelledby="billing-heading"+ />+ </InputGroup>+ </>,+ );+ expect(+ screen.getByRole('combobox', {name: 'Contact Fruit Billing'}),+ ).toBeInTheDocument();+ });+});diff --git a/packages/core/src/Typeahead/Typeahead.tsx b/packages/core/src/Typeahead/Typeahead.tsxindex d582f885f7..ada0c052e7 100644--- a/packages/core/src/Typeahead/Typeahead.tsx+++ b/packages/core/src/Typeahead/Typeahead.tsx@@ -63,6 +63,7 @@ import {isImeKeyEvent,mergeProps,} from '../utils';+import {joinAriaIDs} from '../utils/inputAria';import {TrailingKeyDownContext} from './trailingKeyDown';import {type FieldInputBaseProps,@@ -416,6 +417,7 @@ export function Typeahead<T extends SearchableItem>({controlStyle,id: idProp,'aria-describedby': ariaDescribedByProp,+ 'aria-labelledby': ariaLabelledByProp,...restProps}: TypeaheadProps<T>) {const {rest, container} = useFieldInputStyling('Typeahead', restProps, {@@ -428,6 +430,7 @@ export function Typeahead<T extends SearchableItem>({const generatedInputId = useId();const inputId = idProp ?? generatedInputId;const inputLabelId = useId();+ const fieldLabelId = useId();const descriptionId = useId();const statusMessageId = useId();const inputGroup = useInputGroup();@@ -609,6 +612,13 @@ export function Typeahead<T extends SearchableItem>({inputGroup,);+ // A caller aria-labelledby names the input together with the field's own+ // label (the Field label standalone, the group and input labels in a group).+ const inputLabelledBy =+ ariaLabelledByProp != null+ ? joinAriaIDs(ariaLabelledBy ?? fieldLabelId, ariaLabelledByProp)+ : ariaLabelledBy;+const sizeStyle = wrapperSizeStyles[size];const typeaheadContent = (@@ -684,7 +694,7 @@ export function Typeahead<T extends SearchableItem>({.filter(Boolean).join(' ') || undefined}- ariaLabelledBy={ariaLabelledBy}+ ariaLabelledBy={inputLabelledBy}onChangeQuery={onChangeQuery}onOpenChange={onOpenChange}debounceMs={debounceMs}@@ -735,6 +745,7 @@ export function Typeahead<T extends SearchableItem>({isLabelHidden={isLabelHidden}description={description}inputID={inputId}+ labelID={ariaLabelledByProp != null ? fieldLabelId : undefined}descriptionID={description ? descriptionId : undefined}isOptional={isOptional}isRequired={isRequired}