Skip to content

Commit

Permalink
fix(Text input): Added aria-expanded (#9705)
Browse files Browse the repository at this point in the history
* fix(Text input): Added aria-expanded

* add interface

* updates from review
  • Loading branch information
tlabaj authored Nov 2, 2023
1 parent 4d4d623 commit 7f6a62c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
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');
});
});
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

0 comments on commit 7f6a62c

Please sign in to comment.