Skip to content

Commit a458c7a

Browse files
authored
fix(VerticalLayout): remove div element causing flex issues (#101)
* fix(VerticalLayout): remove div element causing flex issues * fix(VerticalLayout): remove unused group label conditional * fix(LayoutRenderers): add tests to assert no unexpected layout element
1 parent 77af92c commit a458c7a

4 files changed

Lines changed: 32 additions & 19 deletions

File tree

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import { test } from "vitest"
1+
import { test, expect } from "vitest"
22
import { screen } from "@testing-library/react"
33
import { render } from "../common/test-render"
44

55
import {
66
numericMagnitudeSchema,
77
numericHorizontalUISchema,
88
} from "../testSchemas/numericSchema"
9+
import { HORIZONTAL_LAYOUT_FORM_TEST_ID } from "./HorizontalLayout"
910

1011
test("Horizontal layout renders", async () => {
1112
render({
1213
schema: numericMagnitudeSchema,
1314
uischema: numericHorizontalUISchema,
1415
})
1516
await screen.findByRole("spinbutton")
17+
// since we are wrapped in a form no wrapper element should be introduced
18+
expect(
19+
screen.queryByTestId(HORIZONTAL_LAYOUT_FORM_TEST_ID),
20+
).not.toBeInTheDocument()
1621
})

src/layouts/HorizontalLayout.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { HorizontalLayoutUISchema } from "../ui-schema"
55
import { Form, Row } from "antd"
66
import { withJsonFormsLayoutProps } from "@jsonforms/react"
77

8+
export const HORIZONTAL_LAYOUT_FORM_TEST_ID = "horizontal-layout-form"
9+
810
export function HorizontalLayout({
911
uischema,
1012
schema,
@@ -25,7 +27,11 @@ export function HorizontalLayout({
2527
}
2628
const form = Form.useFormInstance()
2729
return (
28-
<Form component={form ? false : "form"} form={form}>
30+
<Form
31+
data-testid={HORIZONTAL_LAYOUT_FORM_TEST_ID}
32+
component={form ? false : "form"}
33+
form={form}
34+
>
2935
{!isEmpty(groupLayout.label) && groupLayout.label}
3036
<Row justify="space-between" gutter={12} align="middle">
3137
<AntDLayout
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
import { test } from "vitest"
1+
import { test, expect } from "vitest"
22
import { screen } from "@testing-library/react"
33
import { render } from "../common/test-render"
44

55
import {
66
numericMagnitudeSchema,
77
numericVerticalUISchema,
88
} from "../testSchemas/numericSchema"
9+
import { VERTICAL_LAYOUT_FORM_TEST_ID } from "./VerticalLayout"
910

1011
test("Vertical layout renders", async () => {
1112
render({
1213
schema: numericMagnitudeSchema,
1314
uischema: numericVerticalUISchema,
1415
})
1516
await screen.findByRole("spinbutton")
17+
18+
// since we are wrapped in a form no wrapper element should be introduced
19+
expect(
20+
screen.queryByTestId(VERTICAL_LAYOUT_FORM_TEST_ID),
21+
).not.toBeInTheDocument()
1622
})

src/layouts/VerticalLayout.tsx

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { LayoutProps, GroupLayout } from "@jsonforms/core"
1+
import { LayoutProps } from "@jsonforms/core"
22
import { AntDLayout, AntDLayoutProps } from "./LayoutRenderer"
3-
import { Form, FormInstance, FormProps } from "antd"
3+
import { Form } from "antd"
44
import { VerticalLayoutUISchema } from "../ui-schema"
55
import { withJsonFormsLayoutProps } from "@jsonforms/react"
66

7+
export const VERTICAL_LAYOUT_FORM_TEST_ID = "vertical-layout-form"
8+
79
export function VerticalLayout({
810
uischema,
911
schema,
@@ -13,31 +15,25 @@ export function VerticalLayout({
1315
renderers,
1416
cells,
1517
}: LayoutProps) {
16-
const verticalLayout = uischema as VerticalLayoutUISchema<unknown>
17-
const groupLayout = uischema as GroupLayout
18+
const { elements } = uischema as VerticalLayoutUISchema<unknown>
1819
const childProps: AntDLayoutProps = {
19-
elements: verticalLayout.elements,
20+
elements,
2021
schema,
2122
path,
2223
enabled,
2324
visible,
2425
}
2526
const form = Form.useFormInstance()
2627
return (
27-
<Form {...getFormLayoutOptions(form)} form={form}>
28-
{!groupLayout.label && (
29-
<div>{groupLayout.label}</div> // this was SubtitleSemiBold
30-
)}
28+
<Form
29+
data-testid={VERTICAL_LAYOUT_FORM_TEST_ID}
30+
component={form ? false : "form"}
31+
scrollToFirstError
32+
form={form}
33+
>
3134
<AntDLayout {...childProps} renderers={renderers} cells={cells} />
3235
</Form>
3336
)
3437
}
3538

36-
function getFormLayoutOptions(form: FormInstance): Partial<FormProps> {
37-
return {
38-
scrollToFirstError: true,
39-
component: form ? "div" : "form",
40-
}
41-
}
42-
4339
export const VerticalLayoutRenderer = withJsonFormsLayoutProps(VerticalLayout)

0 commit comments

Comments
 (0)