Skip to content

Commit 388ec12

Browse files
authored
[v4] fix(InputGroup): don't add aria-describedby automatically (#9671)
* fix(InputGroup): don't add aria-describedby automatically For fixing #9667 * test(NumberInput): drop aria-describedby workaround Test was setting the `aria-describedby=''` attribute to a NumericInput inside an InputGroup, most probably to avoid the autogenerated value (see #9667). However, after cleaning up the InputGroup component, `aria-describedby=''` is not a workaround anymore for unset the attribute, breaking the snapshot associated to the test.
1 parent f5feb0b commit 388ec12

File tree

3 files changed

+8
-54
lines changed

3 files changed

+8
-54
lines changed

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import * as React from 'react';
22
import styles from '@patternfly/react-styles/css/components/InputGroup/input-group';
33
import { css } from '@patternfly/react-styles';
4-
import { FormSelect } from '../FormSelect';
5-
import { TextArea } from '../TextArea';
6-
import { TextInput } from '../TextInput';
74

85
export interface InputGroupProps extends React.HTMLProps<HTMLDivElement> {
96
/** Additional classes added to the input group. */
@@ -20,25 +17,12 @@ export const InputGroup: React.FunctionComponent<InputGroupProps> = ({
2017
innerRef,
2118
...props
2219
}: InputGroupProps) => {
23-
const formCtrls = [FormSelect, TextArea, TextInput].map(comp => comp.displayName);
24-
const idItem = React.Children.toArray(children).find(
25-
(child: any) => !formCtrls.includes(child.type.displayName) && child.props.id
26-
) as React.ReactElement<{ id: string }>;
27-
2820
const ref = React.useRef(null);
2921
const inputGroupRef = innerRef || ref;
3022

3123
return (
3224
<div ref={inputGroupRef} className={css(styles.inputGroup, className)} {...props}>
33-
{idItem
34-
? React.Children.map(children, (child: any) =>
35-
!formCtrls.includes(child.type.displayName) || child.props['aria-describedby']
36-
? child
37-
: React.cloneElement(child, {
38-
'aria-describedby': child.props['aria-describedby'] === '' ? undefined : idItem.props.id
39-
})
40-
)
41-
: children}
25+
{children}
4226
</div>
4327
);
4428
};

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

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,18 @@ import { Button } from '../../Button';
77
import { TextInput } from '../../TextInput';
88

99
describe('InputGroup', () => {
10-
test('add aria-describedby to form-control if one of the non form-controls has id', () => {
11-
// In this test, TextInput is a form-control component and Button is not.
12-
// If Button has an id props, this should be used in aria-describedby.
10+
// Regression test for https://github.com/patternfly/patternfly-react/issues/9667
11+
test('wont add aria-describedby automatically to form-control', () => {
1312
render(
1413
<InputGroup>
15-
<TextInput value="some data" aria-label="some text" />
16-
<Button variant="primary" id="button-id">
17-
hello
14+
<TextInput aria-label="User password" />
15+
<Button variant="primary" id="show-password-toggler">
16+
Show
1817
</Button>
1918
</InputGroup>
2019
);
21-
expect(screen.getByLabelText('some text')).toHaveAttribute('aria-describedby', 'button-id');
22-
});
2320

24-
test('wont add aria-describedby to form-control if describedby is empty string', () => {
25-
// In this test, TextInput is a form-control component and Button is not.
26-
// If Button has an id props, this should be used in aria-describedby, but this
27-
// example has an empty aria-describedby to prevent that from happening.
28-
render(
29-
<InputGroup>
30-
<TextInput value="some data" aria-describedby="" aria-label="some text" />
31-
<Button id="button-id">
32-
hello
33-
</Button>
34-
</InputGroup>
35-
);
36-
expect(screen.getByLabelText('some text')).not.toHaveAttribute('aria-describedby');
37-
});
38-
39-
test('wont override aria-describedby in form-control if describedby has value', () => {
40-
// In this test, TextInput is a form-control component and Button is not.
41-
// If Button has an id props, this should be used in aria-describedby, but this
42-
// example has a predefined aria-describedby to prevent that from happening
43-
render(
44-
<InputGroup>
45-
<TextInput value="some data" aria-describedby="myself" aria-label="some text" />
46-
<Button id="button-id">
47-
hello
48-
</Button>
49-
</InputGroup>
50-
);
51-
expect(screen.getByLabelText('some text')).toHaveAttribute('aria-describedby', 'myself');
21+
const formControl = screen.getByLabelText("User password");
22+
expect(formControl).not.toHaveAttribute("aria-describedby");
5223
});
5324
});

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ describe('numberInput', () => {
6969
<NumberInput
7070
value={5}
7171
onMinus={jest.fn()}
72-
inputProps={{ 'aria-describedby': '' }}
7372
minusBtnProps={{ id: 'minus-id' }}
7473
onPlus={jest.fn()}
7574
plusBtnProps={{ id: 'plus-id' }}

0 commit comments

Comments
 (0)