Skip to content

Commit ccf2101

Browse files
Support single child in InputGroup (#714)
`InputGroup` crashed when a single child was passed because it called `children.reduce()` directly, assuming an array. Normalize `children` with `React.Children.toArray()` before resolving the validation state. Cover the `children` prop with visual tests for both a single node and multiple nodes, and make `InputGroupForTest` accept custom `children` while keeping the current composition as the default.
1 parent ef10288 commit ccf2101

6 files changed

Lines changed: 79 additions & 11 deletions

File tree

src/components/InputGroup/InputGroup.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const InputGroup = ({
3939
return null;
4040
}
4141

42-
const validationState = children.reduce(
42+
const validationState = React.Children.toArray(children).reduce(
4343
(state, child) => {
4444
if (state === 'invalid' || (state === 'warning' && child.props.validationState === 'valid')) {
4545
return state;

src/components/InputGroup/__tests__/InputGroup.spec.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
InputGroupWithCustomInputPropsForTest,
1010
InputGroupWithoutChildrenForTest,
1111
} from './InputGroup.story';
12+
import { childrenPropTest } from './_propTests/childrenPropTest';
1213
import { validationTextsPropTest } from './_propTests/validationTextsPropTest';
1314
import { validationStatePropTest } from './_propTests/validationStatePropTest';
1415

@@ -17,6 +18,7 @@ test.describe('InputGroup', () => {
1718
test.describe('visual', () => {
1819
[
1920
...propTests.defaultComponentPropTest,
21+
...childrenPropTest,
2022
...propTests.disabledPropTest,
2123
...propTests.isLabelVisiblePropTest,
2224
...propTests.layoutPropTest,
Loading
3.25 KB
Loading

src/components/InputGroup/__tests__/InputGroup.story.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,30 @@ const options = [
2626
},
2727
];
2828

29+
const defaultChildren = [
30+
<SelectField
31+
key="selectField"
32+
label="Select label"
33+
options={options}
34+
value={options[0].value}
35+
/>,
36+
<TextField
37+
key="textField"
38+
label="Text label"
39+
placeholder="Placeholder"
40+
/>,
41+
<Button
42+
key="button"
43+
label="Submit"
44+
/>,
45+
];
46+
2947
export const InputGroupForTest = ({
48+
children,
3049
...props
3150
}: InputGroupTestProps) => (
3251
<InputGroup label="Input group label" {...props}>
33-
<SelectField
34-
label="Select label"
35-
options={options}
36-
value={options[0].value}
37-
/>
38-
<TextField
39-
label="Text label"
40-
placeholder="Placeholder"
41-
/>
42-
<Button label="Submit" />
52+
{children ?? defaultChildren}
4353
</InputGroup>
4454
);
4555

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React from 'react';
2+
import type { PropTests } from '../../../../../tests/playwright/types';
3+
import { ButtonWithGlobalProps as Button } from '../../../Button/Button';
4+
import { SelectFieldWithGlobalProps as SelectField } from '../../../SelectField/SelectField';
5+
import { TextFieldWithGlobalProps as TextField } from '../../../TextField/TextField';
6+
7+
const options = [
8+
{
9+
label: 'Option 1',
10+
value: 'option1',
11+
},
12+
{
13+
label: 'Option 2',
14+
value: 'option2',
15+
},
16+
{
17+
label: 'Option 3',
18+
value: 'option3',
19+
},
20+
];
21+
22+
export const childrenPropTest: PropTests = [
23+
{
24+
name: 'children:node[single]',
25+
props: {
26+
children: (
27+
<TextField
28+
label="Text label"
29+
placeholder="Placeholder"
30+
/>
31+
),
32+
},
33+
},
34+
{
35+
name: 'children:node[multiple]',
36+
props: {
37+
children: [
38+
<SelectField
39+
key="selectField"
40+
label="Select label"
41+
options={options}
42+
value={options[0].value}
43+
/>,
44+
<TextField
45+
key="textField"
46+
label="Text label"
47+
placeholder="Placeholder"
48+
/>,
49+
<Button
50+
key="button"
51+
label="Submit"
52+
/>,
53+
],
54+
},
55+
},
56+
];

0 commit comments

Comments
 (0)