Skip to content

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

TypeScript

fix(NumberInput): keep a caller aria-labelledby on the input (#8708)

홍규진f30564c287f4
diff --git a/.changeset/number-input-caller-labelledby.md b/.changeset/number-input-caller-labelledby.md
new file mode 100644
index 0000000000..fd76a60ab2
--- /dev/null
+++ b/.changeset/number-input-caller-labelledby.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] NumberInput: a caller's `aria-labelledby` now reaches the input. Before, NumberInput replaced it with its own value, so the input lost the extra name (standalone it had no `aria-labelledby` at all). Now the caller's ids follow the field's own label: `label="Amount" aria-labelledby="shipping-heading"` names the input "Amount Shipping", and inside an InputGroup the group label comes first. Inputs without a caller `aria-labelledby` are unchanged. No API changes.
+
+@kyu-rong
diff --git a/packages/core/src/NumberInput/NumberInput.test.tsx b/packages/core/src/NumberInput/NumberInput.test.tsx
index 1f28890ff0..64dd2b6127 100644
--- a/packages/core/src/NumberInput/NumberInput.test.tsx
+++ b/packages/core/src/NumberInput/NumberInput.test.tsx
@@ -1080,6 +1080,49 @@ describe('NumberInput', () => {
}
});
+ it('names the input by its label and a caller aria-labelledby', () => {
+ render(
+ <>
+ <span id="shipping-heading">Shipping</span>
+ <NumberInput
+ label="Amount"
+ value={null}
+ onChange={() => {}}
+ aria-labelledby="shipping-heading"
+ />
+ </>,
+ );
+ const input = screen.getByRole('spinbutton', {name: 'Amount Shipping'});
+ expect(input.getAttribute('aria-labelledby')?.split(' ')).toContain(
+ 'shipping-heading',
+ );
+ });
+
+ it('names a grouped input by the group, its label, and a caller aria-labelledby', () => {
+ render(
+ <>
+ <span id="shipping-heading">Shipping</span>
+ <InputGroup label="Price">
+ <NumberInput
+ label="Amount"
+ value={null}
+ onChange={() => {}}
+ aria-labelledby="shipping-heading"
+ />
+ </InputGroup>
+ </>,
+ );
+ expect(
+ screen.getByRole('spinbutton', {name: 'Price Amount Shipping'}),
+ ).toBeInTheDocument();
+ });
+
+ it('leaves a standalone input named by its label element without a caller aria-labelledby', () => {
+ render(<NumberInput label="Amount" value={null} onChange={() => {}} />);
+ const input = screen.getByRole('spinbutton', {name: 'Amount'});
+ expect(input).not.toHaveAttribute('aria-labelledby');
+ });
+
it('does not render status message when not provided', () => {
render(
<NumberInput
diff --git a/packages/core/src/NumberInput/NumberInput.tsx b/packages/core/src/NumberInput/NumberInput.tsx
index def41b5db1..ee285dab0c 100644
--- a/packages/core/src/NumberInput/NumberInput.tsx
+++ b/packages/core/src/NumberInput/NumberInput.tsx
@@ -52,6 +52,7 @@ import {Icon, renderIconSlot, type IconType} from '../Icon';
import {VisuallyHidden} from '../VisuallyHidden';
import {useTooltip} from '../Tooltip';
import {getInputARIA} from '../utils';
+import {joinAriaIDs} from '../utils/inputAria';
import {interactionOverlayStyles} from '../utils/interactionOverlay.stylex';
import {usePressFeedback} from '../hooks/usePressFeedback';
import {inputDisabledFocusStyles} from '../Field/inputDisabledFocus';
@@ -577,6 +578,7 @@ export function NumberInput({
ref,
id: idProp,
'aria-describedby': ariaDescribedByProp,
+ 'aria-labelledby': ariaLabelledByProp,
...restProps
}: NumberInputProps) {
const {rest, container} = useFieldInputStyling('NumberInput', restProps, {
@@ -591,6 +593,7 @@ export function NumberInput({
const size = useSize(sizeProp, 'md');
const generatedID = useId();
const id = idProp ?? generatedID;
+ const fieldLabelID = useId();
const inputLabelID = useId();
const descriptionID = useId();
const statusMessageID = useId();
@@ -651,6 +654,12 @@ export function NumberInput({
],
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 parseInput = useCallback(
(text: string) => parseNumberInput(text, {min, max, isIntegerOnly, locale}),
@@ -990,7 +999,7 @@ export function NumberInput({
aria-invalid={
status?.type === 'error' || !isInputValid ? 'true' : undefined
}
- aria-labelledby={ariaLabelledBy}
+ aria-labelledby={inputLabelledBy}
{...stylex.props(
styles.input,
isDisabled && styles.inputDisabled,
@@ -1106,6 +1115,7 @@ export function NumberInput({
isLabelHidden={isLabelHidden}
description={description}
inputID={id}
+ labelID={ariaLabelledByProp != null ? fieldLabelID : undefined}
descriptionID={description ? descriptionID : undefined}
isOptional={isOptional}
isRequired={isRequired}