Skip to content

Commit

Permalink
feat: Make Input aria-label available when using FormField (#1615)
Browse files Browse the repository at this point in the history
  • Loading branch information
Al-Dani authored Oct 5, 2023
1 parent 1db9c25 commit b0e7f93
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/input/__tests__/input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import Input, { InputProps } from '../../../lib/components/input';
import FormField from '../../../lib/components/form-field';
import styles from '../../../lib/components/input/styles.css.js';
import createWrapper from '../../../lib/components/test-utils/dom';

Expand Down Expand Up @@ -324,6 +325,32 @@ describe('Input', () => {
expect(input).not.toHaveAttribute('aria-required');
});
});

test('aria-label from input takes precedence over aria-labelledBy from form field', () => {
render(
<FormField label="Form label">
<Input value="" ariaLabel="Input label" />
</FormField>
);

const element = createWrapper().find('input')!.getElement();

expect(element).toHaveAttribute('aria-label', 'Input label');
expect(element).not.toHaveAttribute('aria-labelledby');
});

test('aria-labelled by from input takes precedence over aria-label', () => {
render(
<FormField label="Form label">
<Input value="" ariaLabel="Input label" ariaLabelledby="test" />
</FormField>
);

const element = createWrapper().find('input')!.getElement();

expect(element).toHaveAttribute('aria-label', 'Input label');
expect(element).toHaveAttribute('aria-labelledby', 'test');
});
});

describe('Ref', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/input/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ function InternalInput(

const attributes: React.InputHTMLAttributes<HTMLInputElement> = {
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledby,
// aria-labelledby has precedence over aria-label in accessible name calculation.
// When aria-label is provided for Input, it should override aria-labelledBy from form-field context.
// If both aria-label and aria-labelledby come from Input props, aria-labelledby will be used in accessible name
'aria-labelledby': ariaLabel && !rest.ariaLabelledby ? undefined : ariaLabelledby,
'aria-describedby': ariaDescribedby,
name,
placeholder,
Expand Down

0 comments on commit b0e7f93

Please sign in to comment.