Skip to content

Commit

Permalink
added: [M3-6989] - aria-describedby to TextField with helper text (#…
Browse files Browse the repository at this point in the history
…11351)

* Add aria-describedby to TextField with helper text

* Always some ID

* cleanup

* add coverage

* Added changeset: aria-describedby to TextField with helper text

* cleanup

* feedback @hkhalil-akamai
  • Loading branch information
abailly-akamai authored Dec 6, 2024
1 parent 82c8990 commit 04f241d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/manager/.changeset/pr-11351-added-1733194164261.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Added
---

aria-describedby to TextField with helper text ([#11351](https://github.com/linode/manager/pull/11351))
32 changes: 32 additions & 0 deletions packages/ui/src/components/TextField/TextField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,36 @@ describe('TextField', () => {
fireEvent.change(input, { target: { value: '1' } });
expect(input?.getAttribute('value')).toBe('2');
});

it('renders a helper text with an input id', () => {
const { getByText } = renderWithTheme(
<TextField helperText="Helper text" inputId="input-id" label="" />
);

expect(getByText('Helper text')).toBeInTheDocument();
const helperText = getByText('Helper text');
expect(helperText.getAttribute('id')).toBe('input-id-helper-text');
});

it('renders a helper text with a label', () => {
const { getByText } = renderWithTheme(
<TextField helperText="Helper text" label="Label" />
);

const helperText = getByText('Helper text');

expect(helperText).toBeInTheDocument();
expect(helperText.getAttribute('id')).toBe('label-helper-text');
});

it('renders a helper text with a fallback id', () => {
const { getByText } = renderWithTheme(
<TextField helperText="Helper text" label="" />
);

const helperText = getByText('Helper text');

// ':rg:' being the default react generated id
expect(helperText.getAttribute('id')).toBe(':rg:-helper-text');
});
});
19 changes: 16 additions & 3 deletions packages/ui/src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ interface InputToolTipProps {
tooltipWidth?: number;
}

interface TextFieldPropsOverrides extends StandardTextFieldProps {
interface TextFieldPropsOverrides
extends Omit<StandardTextFieldProps, 'label'> {
// We override this prop to make it required
label: string;
}
Expand Down Expand Up @@ -166,6 +167,7 @@ export const TextField = (props: TextFieldProps) => {

const [_value, setValue] = React.useState<Value>(value);
const theme = useTheme();
const fallbackId = React.useId();

React.useEffect(() => {
setValue(value);
Expand Down Expand Up @@ -249,7 +251,14 @@ export const TextField = (props: TextFieldProps) => {
}

const validInputId =
inputId || (label ? convertToKebabCase(`${label}`) : undefined);
inputId ||
(label
? convertToKebabCase(label)
: // label could still be an empty string
fallbackId);

const helperTextId = `${validInputId}-helper-text`;
const errorTextId = `${validInputId}-error-text`;

const labelSuffixText = required
? '(required)'
Expand Down Expand Up @@ -316,6 +325,7 @@ export const TextField = (props: TextFieldProps) => {
marginTop: 0,
}}
data-qa-textfield-helper-text
id={helperTextId}
>
{helperText}
</FormHelperText>
Expand Down Expand Up @@ -363,6 +373,9 @@ export const TextField = (props: TextFieldProps) => {
...SelectProps,
}}
inputProps={{
'aria-describedby': helperText ? helperTextId : undefined,
'aria-errormessage': errorText ? errorTextId : undefined,
'aria-invalid': !!error || !!errorText,
'data-testid': 'textfield-input',
id: validInputId,
...inputProps,
Expand Down Expand Up @@ -436,7 +449,7 @@ export const TextField = (props: TextFieldProps) => {
</FormHelperText>
)}
{helperText && helperTextPosition === 'bottom' && (
<FormHelperText data-qa-textfield-helper-text>
<FormHelperText data-qa-textfield-helper-text id={helperTextId}>
{helperText}
</FormHelperText>
)}
Expand Down

0 comments on commit 04f241d

Please sign in to comment.