Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Text input): Added aria-expanded #9705

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/react-core/src/components/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,24 @@ export enum TextInputReadOnlyVariant {
plain = 'plain'
}

export interface TextInputExpandedObj {
/** Flag to apply expanded styling. */
isExpanded: boolean;
/** Id of the element that the text input is controlling expansion of. */
ariaControls: string;
}

export interface TextInputProps
extends Omit<React.HTMLProps<HTMLInputElement>, 'onChange' | 'onFocus' | 'onBlur' | 'disabled' | 'ref'>,
OUIAProps {
/** Additional classes added to the text input. */
className?: string;
/** Flag to show if the text input is disabled. */
isDisabled?: boolean;
/** Flag to apply expanded styling */
/** @deprecated Flag to apply expanded styling. expandedProps should now be used instead. */
isExpanded?: boolean;
/** Prop to apply expanded styling to the text input and link it to the element it is controlling. This should be used when the input controls a menu and that menu is expandable. */
expandedProps?: TextInputExpandedObj;
/** Sets the input as readonly and determines the readonly styling. */
readOnlyVariant?: 'plain' | 'default';
/** Flag indicating whether the input is required */
Expand Down Expand Up @@ -182,6 +191,7 @@ export class TextInputBase extends React.Component<TextInputProps, TextInputStat
isLeftTruncated,
isStartTruncated,
isExpanded,
expandedProps,
readOnly,
readOnlyVariant,
isRequired,
Expand All @@ -193,6 +203,9 @@ export class TextInputBase extends React.Component<TextInputProps, TextInputStat
} = this.props;

const hasStatusIcon = ['success', 'error', 'warning'].includes(validated);
const ariaExpandedProps = expandedProps
? { 'aria-expanded': expandedProps?.isExpanded, 'aria-controls': expandedProps?.ariaControls, role: 'combobox' }
: {};

return (
<span
Expand All @@ -201,7 +214,7 @@ export class TextInputBase extends React.Component<TextInputProps, TextInputStat
readOnlyVariant && styles.modifiers.readonly,
readOnlyVariant === 'plain' && styles.modifiers.plain,
isDisabled && styles.modifiers.disabled,
isExpanded && styles.modifiers.expanded,
(isExpanded || expandedProps?.isExpanded) && styles.modifiers.expanded,
customIcon && styles.modifiers.icon,
hasStatusIcon && styles.modifiers[validated as 'success' | 'warning' | 'error'],
className
Expand All @@ -215,6 +228,7 @@ export class TextInputBase extends React.Component<TextInputProps, TextInputStat
type={type}
value={this.sanitizeInputValue(value)}
aria-invalid={props['aria-invalid'] ? props['aria-invalid'] : validated === ValidatedOptions.error}
{...ariaExpandedProps}
required={isRequired}
disabled={isDisabled}
readOnly={!!readOnlyVariant || readOnly}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,13 @@ describe('TextInput', () => {
render(<TextInput isLeftTruncated aria-label="start truncated text input" />);
expect(trimLeftFn).toHaveBeenCalled();
});

test('has aria-expanded set to true when ariaProps.isExpanded is true', () => {
render(<TextInput expandedProps={{isExpanded: true, ariaControls: 'test'}} aria-label="isExpanded"/>);

const input = screen.getByLabelText('isExpanded');
expect(input).toHaveAttribute('aria-expanded', 'true');
expect(input).toHaveAttribute('role', 'combobox');
expect(input).toHaveAttribute('aria-controls', 'test');
});
Comment on lines +128 to +135
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might lean towards having this split up to 3 separate tests rather than checking that all 3 attributes are present.

For the role test, we'd also be able to do const input = screen.getByRole('combobox') as well, then just check that it's visible/in the document

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know that it is worth it to re-render the same thing to check for different attributes?

});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ id: Text input
section: components
subsection: forms
cssPrefix: pf-v5-c-form-control
propComponents: ['TextInput']
propComponents: ['TextInput', 'TextInputExpandedObj']
---

import CalendarIcon from '@patternfly/react-icons/dist/esm/icons/calendar-icon';
Expand Down
Loading