Skip to content

Commit b726700

Browse files
committed
don't crash with only one child
1 parent e649098 commit b726700

File tree

4 files changed

+15
-74
lines changed

4 files changed

+15
-74
lines changed

packages/main/src/components/Form/Form.stories.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ import { Form } from '@ui5/webcomponents-react/lib/Form';
7676
</Story>
7777
</Preview>
7878

79+
### Usage Notes
80+
81+
Please note that the `FormGroup` and `FormItem` components are only used for calculating the final layout of the `Form`,
82+
thus they don't accept any other props then the specified ones, especially no `className`, `style` or `ref`.
83+
7984
<Props components={{ Form, FormGroup, FormItem }} />

packages/main/src/components/Form/Form.test.tsx

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,6 @@ describe('Create a Form', () => {
5858
expect(wrapper.render()).toMatchSnapshot();
5959
});
6060

61-
test('should create a FormGroup and put ungrouped FormItems into it', () => {
62-
const ungroupedChildren = (
63-
<Form title={'Test form'}>
64-
<FormItem label={'item 1'}>
65-
<Input type={InputType.Text} />
66-
</FormItem>
67-
<FormItem label={'item 2'}>
68-
<Input type={InputType.Number} />
69-
</FormItem>
70-
</Form>
71-
);
72-
const wrapper = mount(ungroupedChildren);
73-
expect(wrapper.render()).toMatchSnapshot();
74-
});
75-
7661
test("should use a single FormGroup's title as a Form title if one is not set", () => {
7762
const ungroupedChildren = (
7863
<Form>

packages/main/src/components/Form/__snapshots__/Form.test.tsx.snap

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Create a Form should create a FormGroup and put ungrouped FormItems into it 1`] = `
4-
<div
5-
class="Form-form-0"
6-
style="grid-template-columns: repeat(24, 1fr); --ui5wcr_form_full_span: span 24; --ui5wcr_form_content_span: 8; --ui5wcr_form_label_span: 4;"
7-
>
8-
<ui5-title
9-
class="Form-formTitle-0"
10-
level="H3"
11-
>
12-
Test form
13-
</ui5-title>
14-
<ui5-title
15-
level="H5"
16-
style="padding-bottom: 0.75rem; grid-column: span 12;"
17-
/>
18-
<ui5-title
19-
level="H5"
20-
style="padding-bottom: 0.75rem; grid-column: span 12;"
21-
/>
22-
<ui5-label
23-
class="FormItem-label-0"
24-
style="grid-column-start: 1;"
25-
>
26-
item 1:
27-
</ui5-label>
28-
<div
29-
class="FormItem-content-0"
30-
>
31-
<ui5-input
32-
type="Text"
33-
value-state="None"
34-
/>
35-
</div>
36-
<ui5-label
37-
class="FormItem-label-0"
38-
style="grid-column-start: 13;"
39-
>
40-
item 2:
41-
</ui5-label>
42-
<div
43-
class="FormItem-content-0"
44-
>
45-
<ui5-input
46-
type="Number"
47-
value-state="None"
48-
/>
49-
</div>
50-
</div>
51-
`;
52-
533
exports[`Create a Form should use a single FormGroup's title as a Form title if one is not set 1`] = `
544
<div
555
class="Form-form-0"

packages/main/src/components/Form/index.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ const Form: FC<FormPropTypes> = forwardRef((props: FormPropTypes, ref: Ref<HTMLD
5858
for (let i = 0; i < firstColumnFormGroups.length; i++) {
5959
const formGroupColumnOne = firstColumnFormGroups[i];
6060
const formGroupColumnTwo = secondColumnFormGroups[i];
61+
let childrenOfColumnOneGroup = [];
62+
let childrenOfColumnTwoGroup = [];
6163

6264
if (formGroupColumnOne) {
6365
formGroups.push(
@@ -69,6 +71,10 @@ const Form: FC<FormPropTypes> = forwardRef((props: FormPropTypes, ref: Ref<HTMLD
6971
{formGroupColumnOne?.props?.title ?? ''}
7072
</Title>
7173
);
74+
childrenOfColumnOneGroup =
75+
formGroupColumnOne.type.displayName === 'FormItem'
76+
? [formGroupColumnOne]
77+
: Children.toArray(formGroupColumnOne?.props?.children);
7278
totalRowCount++;
7379
}
7480

@@ -82,17 +88,12 @@ const Form: FC<FormPropTypes> = forwardRef((props: FormPropTypes, ref: Ref<HTMLD
8288
{formGroupColumnTwo?.props?.title ?? ''}
8389
</Title>
8490
);
91+
childrenOfColumnTwoGroup =
92+
formGroupColumnTwo.type.displayName === 'FormItem'
93+
? [formGroupColumnTwo]
94+
: Children.toArray(formGroupColumnTwo?.props?.children);
8595
}
8696

87-
const childrenOfColumnOneGroup =
88-
formGroupColumnOne.type.displayName === 'FormItem'
89-
? [formGroupColumnOne]
90-
: Children.toArray(formGroupColumnOne?.props?.children);
91-
const childrenOfColumnTwoGroup =
92-
formGroupColumnTwo.type.displayName === 'FormItem'
93-
? [formGroupColumnTwo]
94-
: Children.toArray(formGroupColumnTwo?.props?.children);
95-
9697
const maxChildCount = Math.max(childrenOfColumnOneGroup.length, childrenOfColumnTwoGroup.length);
9798

9899
for (let childIndex = 0; childIndex < maxChildCount; childIndex++) {

0 commit comments

Comments
 (0)