An open source design system that's fully customizable and agent ready
TypeScript
fix(inputgroup): name the group via aria-labelledby, drop orphaned htmlFor (forms-14) (#3455)
Cindy Zhangae2d146d7cf5
diff --git a/.changeset/inputgroup-group-label.md b/.changeset/inputgroup-group-label.mdnew file mode 100644index 0000000000..582080718e--- /dev/null+++ b/.changeset/inputgroup-group-label.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] InputGroup: the group is now named by the field label via `aria-labelledby` instead of a duplicated `aria-label`, and the label no longer carries an orphaned `htmlFor` (its `inputId` was never handed to a child). Uses the `Field` `isGroupLabel`/`labelID` support (#3343).+@cixzhangdiff --git a/packages/core/src/InputGroup/InputGroup.test.tsx b/packages/core/src/InputGroup/InputGroup.test.tsxindex 52529ab99c..da852acec3 100644--- a/packages/core/src/InputGroup/InputGroup.test.tsx+++ b/packages/core/src/InputGroup/InputGroup.test.tsx@@ -16,33 +16,31 @@ import {InputGroupText} from './InputGroupText';import {TextInput} from '../TextInput';describe('InputGroup', () => {- it('renders a group with aria-label', () => {+ it('names the group via the label element (forms-14)', () => {render(<InputGroup label="Price"><InputGroupText>$</InputGroupText>- <TextInput- label="Amount"- isLabelHidden- value=""- onChange={() => {}}- />+ <TextInput label="Amount" isLabelHidden value="" onChange={() => {}} /></InputGroup>,);- const group = screen.getByRole('group');+ // The group is named by the field label via aria-labelledby (not a+ // duplicated aria-label). The label is rendered as a <span> (not a literal+ // <label>, which can't name a group) and carries no orphaned htmlFor.+ const group = screen.getByRole('group', {name: 'Price'});expect(group).toBeInTheDocument();- expect(group).toHaveAttribute('aria-label', 'Price');+ expect(group).not.toHaveAttribute('aria-label');+ const label = screen.getByText('Price');+ expect(label.tagName).toBe('SPAN');+ expect(label.closest('label')).toBeNull();+ expect(label).not.toHaveAttribute('for');+ expect(group.getAttribute('aria-labelledby')).toBe(label.id);});it('renders the visible label', () => {render(<InputGroup label="Price">- <TextInput- label="Amount"- isLabelHidden- value=""- onChange={() => {}}- />+ <TextInput label="Amount" isLabelHidden value="" onChange={() => {}} /></InputGroup>,);@@ -53,12 +51,7 @@ describe('InputGroup', () => {render(<InputGroup label="Price"><InputGroupText>$</InputGroupText>- <TextInput- label="Amount"- isLabelHidden- value=""- onChange={() => {}}- />+ <TextInput label="Amount" isLabelHidden value="" onChange={() => {}} /></InputGroup>,);@@ -68,12 +61,7 @@ describe('InputGroup', () => {it('renders the input', () => {render(<InputGroup label="Price">- <TextInput- label="Amount"- isLabelHidden- value=""- onChange={() => {}}- />+ <TextInput label="Amount" isLabelHidden value="" onChange={() => {}} /></InputGroup>,);@@ -97,12 +85,7 @@ describe('InputGroup', () => {it('applies data-testid', () => {render(<InputGroup label="Price" data-testid="price-group">- <TextInput- label="Amount"- isLabelHidden- value=""- onChange={() => {}}- />+ <TextInput label="Amount" isLabelHidden value="" onChange={() => {}} /></InputGroup>,);@@ -112,12 +95,7 @@ describe('InputGroup', () => {it('renders description text', () => {render(<InputGroup label="Price" description="Enter the price in USD">- <TextInput- label="Amount"- isLabelHidden- value=""- onChange={() => {}}- />+ <TextInput label="Amount" isLabelHidden value="" onChange={() => {}} /></InputGroup>,);@@ -129,12 +107,7 @@ describe('InputGroup', () => {<InputGrouplabel="Price"status={{type: 'error', message: 'Price is required'}}>- <TextInput- label="Amount"- isLabelHidden- value=""- onChange={() => {}}- />+ <TextInput label="Amount" isLabelHidden value="" onChange={() => {}} /></InputGroup>,);@@ -144,12 +117,7 @@ describe('InputGroup', () => {it('renders with hidden label', () => {render(<InputGroup label="Price" isLabelHidden>- <TextInput- label="Amount"- isLabelHidden- value=""- onChange={() => {}}- />+ <TextInput label="Amount" isLabelHidden value="" onChange={() => {}} /></InputGroup>,);@@ -160,24 +128,14 @@ describe('InputGroup', () => {it('renders with different sizes', () => {const {rerender} = render(<InputGroup label="Price" size="sm">- <TextInput- label="Amount"- isLabelHidden- value=""- onChange={() => {}}- />+ <TextInput label="Amount" isLabelHidden value="" onChange={() => {}} /></InputGroup>,);expect(screen.getByRole('textbox')).toBeInTheDocument();rerender(<InputGroup label="Price" size="lg">- <TextInput- label="Amount"- isLabelHidden- value=""- onChange={() => {}}- />+ <TextInput label="Amount" isLabelHidden value="" onChange={() => {}} /></InputGroup>,);expect(screen.getByRole('textbox')).toBeInTheDocument();diff --git a/packages/core/src/InputGroup/InputGroup.tsx b/packages/core/src/InputGroup/InputGroup.tsxindex 76cecc8970..db2e2f9e18 100644--- a/packages/core/src/InputGroup/InputGroup.tsx+++ b/packages/core/src/InputGroup/InputGroup.tsx@@ -156,6 +156,7 @@ export function InputGroup({}: InputGroupProps) {const size = useSize(sizeProp, 'md');const inputId = useId();+ const labelElementId = useId();const statusMessageId = useId();const contextValue = useMemo(() => ({isInGroup: true as const}), []);@@ -168,6 +169,8 @@ export function InputGroup({isLabelHidden={isLabelHidden}description={description}inputID={inputId}+ labelElementID={labelElementId}+ isGroupLabelisOptional={isOptional}isRequired={isRequired}isDisabled={isDisabled}@@ -185,7 +188,7 @@ export function InputGroup({<divref={ref}role="group"- aria-label={label}+ aria-labelledby={labelElementId}data-testid={testId}{...rest}{...mergeProps(