Skip to content

Commit 7f6a62c

Browse files
authored
fix(Text input): Added aria-expanded (#9705)
* fix(Text input): Added aria-expanded * add interface * updates from review
1 parent 4d4d623 commit 7f6a62c

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

packages/react-core/src/components/TextInput/TextInput.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,24 @@ export enum TextInputReadOnlyVariant {
2626
plain = 'plain'
2727
}
2828

29+
export interface TextInputExpandedObj {
30+
/** Flag to apply expanded styling. */
31+
isExpanded: boolean;
32+
/** Id of the element that the text input is controlling expansion of. */
33+
ariaControls: string;
34+
}
35+
2936
export interface TextInputProps
3037
extends Omit<React.HTMLProps<HTMLInputElement>, 'onChange' | 'onFocus' | 'onBlur' | 'disabled' | 'ref'>,
3138
OUIAProps {
3239
/** Additional classes added to the text input. */
3340
className?: string;
3441
/** Flag to show if the text input is disabled. */
3542
isDisabled?: boolean;
36-
/** Flag to apply expanded styling */
43+
/** @deprecated Flag to apply expanded styling. expandedProps should now be used instead. */
3744
isExpanded?: boolean;
45+
/** 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. */
46+
expandedProps?: TextInputExpandedObj;
3847
/** Sets the input as readonly and determines the readonly styling. */
3948
readOnlyVariant?: 'plain' | 'default';
4049
/** Flag indicating whether the input is required */
@@ -182,6 +191,7 @@ export class TextInputBase extends React.Component<TextInputProps, TextInputStat
182191
isLeftTruncated,
183192
isStartTruncated,
184193
isExpanded,
194+
expandedProps,
185195
readOnly,
186196
readOnlyVariant,
187197
isRequired,
@@ -193,6 +203,9 @@ export class TextInputBase extends React.Component<TextInputProps, TextInputStat
193203
} = this.props;
194204

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

197210
return (
198211
<span
@@ -201,7 +214,7 @@ export class TextInputBase extends React.Component<TextInputProps, TextInputStat
201214
readOnlyVariant && styles.modifiers.readonly,
202215
readOnlyVariant === 'plain' && styles.modifiers.plain,
203216
isDisabled && styles.modifiers.disabled,
204-
isExpanded && styles.modifiers.expanded,
217+
(isExpanded || expandedProps?.isExpanded) && styles.modifiers.expanded,
205218
customIcon && styles.modifiers.icon,
206219
hasStatusIcon && styles.modifiers[validated as 'success' | 'warning' | 'error'],
207220
className
@@ -215,6 +228,7 @@ export class TextInputBase extends React.Component<TextInputProps, TextInputStat
215228
type={type}
216229
value={this.sanitizeInputValue(value)}
217230
aria-invalid={props['aria-invalid'] ? props['aria-invalid'] : validated === ValidatedOptions.error}
231+
{...ariaExpandedProps}
218232
required={isRequired}
219233
disabled={isDisabled}
220234
readOnly={!!readOnlyVariant || readOnly}

packages/react-core/src/components/TextInput/__tests__/TextInput.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,13 @@ describe('TextInput', () => {
124124
render(<TextInput isLeftTruncated aria-label="start truncated text input" />);
125125
expect(trimLeftFn).toHaveBeenCalled();
126126
});
127+
128+
test('has aria-expanded set to true when ariaProps.isExpanded is true', () => {
129+
render(<TextInput expandedProps={{isExpanded: true, ariaControls: 'test'}} aria-label="isExpanded"/>);
130+
131+
const input = screen.getByLabelText('isExpanded');
132+
expect(input).toHaveAttribute('aria-expanded', 'true');
133+
expect(input).toHaveAttribute('role', 'combobox');
134+
expect(input).toHaveAttribute('aria-controls', 'test');
135+
});
127136
});

packages/react-core/src/components/TextInput/examples/TextInput.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: Text input
33
section: components
44
subsection: forms
55
cssPrefix: pf-v5-c-form-control
6-
propComponents: ['TextInput']
6+
propComponents: ['TextInput', 'TextInputExpandedObj']
77
---
88

99
import CalendarIcon from '@patternfly/react-icons/dist/esm/icons/calendar-icon';

0 commit comments

Comments
 (0)