Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: HorizontalLayout #38

Merged
merged 11 commits into from
Mar 14, 2024
12 changes: 6 additions & 6 deletions src/controls/NumericControls/NumericControl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
numericWeightSchema,
numericSheepSchema,
numericBeansSchema,
numericUISchema,
numericVerticalUISchema,
numericUISchemaWithRule,
numericPriceSchema,
numericUSDUISchema,
Expand Down Expand Up @@ -90,7 +90,7 @@ describe("NumericControl", () => {
render({
data: data,
schema: numericTheNumberSchema, // this has a default of 42.42
uischema: numericUISchema,
uischema: numericVerticalUISchema,
})
screen.getByText("The Number")
expect(screen.getByRole("spinbutton")).toHaveValue(`${dataVal}`)
Expand All @@ -100,7 +100,7 @@ describe("NumericControl", () => {
it("renders default value when no data is provided", () => {
render({
schema: numericTheNumberSchema,
uischema: numericUISchema,
uischema: numericVerticalUISchema,
})
expect(screen.getByRole("spinbutton")).toHaveValue("42.42")
})
Expand All @@ -109,7 +109,7 @@ describe("NumericControl", () => {
let data: JSONSchema
render({
schema: numericMagnitudeSchema,
uischema: numericUISchema,
uischema: numericVerticalUISchema,
onChange: (state: { data: JSONSchema }) => {
data = state.data
},
Expand All @@ -126,7 +126,7 @@ describe("NumericControl", () => {
it("shows error message onBlur when field is required and empty", async () => {
render({
schema: numericTheNumberSchema,
uischema: numericUISchema,
uischema: numericVerticalUISchema,
})
const input = screen.getByRole("spinbutton")
await userEvent.clear(input)
Expand All @@ -147,7 +147,7 @@ describe("NumericControl", () => {
async (schema: JSONSchema) => {
render({
schema: schema,
uischema: numericUISchema,
uischema: numericVerticalUISchema,
})
const input = screen.getByRole("spinbutton")
await userEvent.type(input, "123.45") // try to input a float
Expand Down
16 changes: 16 additions & 0 deletions src/layouts/HorizontalLayoutRenderer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { test } from "vitest"
import { screen } from "@testing-library/react"
import { render } from "../common/test-render"

import {
numericMagnitudeSchema,
numericHorizontalUISchema,
} from "../testSchemas/numericSchema/numericSchema"

test("Horizontal layout renders", async () => {
render({
schema: numericMagnitudeSchema,
uischema: numericHorizontalUISchema,
})
await screen.findByRole("spinbutton")
})
39 changes: 39 additions & 0 deletions src/layouts/HorizontalLayoutRenderer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { LayoutProps, GroupLayout } from "@jsonforms/core"
import isEmpty from "lodash.isempty"
import { AntDLayoutRenderer, AntDLayoutRendererProps } from "./LayoutRenderer"
import { HorizontalLayout } from "../ui-schema"
import { Form, Row } from "antd"

export function HorizontalLayoutRenderer({
NathanFarmer marked this conversation as resolved.
Show resolved Hide resolved
uischema,
schema,
path,
enabled,
visible,
renderers,
cells,
}: LayoutProps) {
const horizontalLayout = uischema as HorizontalLayout
const groupLayout = uischema as GroupLayout
const childProps: AntDLayoutRendererProps = {
elements: horizontalLayout.elements,
schema,
path,
enabled,
visible,
}
const form = Form.useFormInstance()
return (
<Form component={form ? false : "form"} form={form}>
{!isEmpty(groupLayout.label) && groupLayout.label}
<Row justify="space-between" gutter={12}>
<AntDLayoutRenderer
{...childProps}
direction="row"
renderers={renderers}
cells={cells}
/>
</Row>
</Form>
)
}
16 changes: 16 additions & 0 deletions src/layouts/VerticalLayoutRenderer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { test } from "vitest"
import { screen } from "@testing-library/react"
import { render } from "../common/test-render"

import {
numericMagnitudeSchema,
numericVerticalUISchema,
} from "../testSchemas/numericSchema/numericSchema"

test("Vertical layout renders", async () => {
NathanFarmer marked this conversation as resolved.
Show resolved Hide resolved
render({
schema: numericMagnitudeSchema,
uischema: numericVerticalUISchema,
})
await screen.findByRole("spinbutton")
})
5 changes: 5 additions & 0 deletions src/renderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { BooleanControl } from "./controls/BooleanControl"
import { AlertControl } from "./controls/AlertControl"
import { TextControl } from "./controls/TextControl"
import { UnknownControl } from "./controls/UnknownControl"
import { HorizontalLayoutRenderer } from "./layouts/HorizontalLayoutRenderer"
import { VerticalLayoutRenderer } from "./layouts/VerticalLayoutRenderer"
import { ObjectControl } from "./controls/ObjectControl"
import { GroupLayoutRenderer } from "./layouts/GroupLayoutRenderer"
Expand All @@ -44,6 +45,10 @@ export const rendererRegistryEntries: JsonFormsRendererRegistryEntry[] = [
tester: rankWith(1, uiTypeIs("Group")),
renderer: React.memo(GroupLayoutRenderer),
},
{
tester: rankWith(2, uiTypeIs("HorizontalLayout")),
renderer: withJsonFormsLayoutProps(HorizontalLayoutRenderer),
},
{
tester: rankWith(2, uiTypeIs("VerticalLayout")),
renderer: withJsonFormsLayoutProps(VerticalLayoutRenderer),
Expand Down
12 changes: 6 additions & 6 deletions src/stories/controls/NumericControls/NumericControl.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
numericTheNumberSchema,
numericWeightSchema,
numericSheepSchema,
numericUISchema,
numericVerticalUISchema,
numericPriceSchema,
numericUSDUISchema,
numericROISchema,
Expand All @@ -18,7 +18,7 @@ const meta: Meta<typeof StorybookAntDJsonForm> = {
component: StorybookAntDJsonForm,
tags: ["autodocs"],
args: {
uiSchema: numericUISchema,
uiSchema: numericVerticalUISchema,
},
argTypes: {
uiSchema: {
Expand All @@ -34,31 +34,31 @@ export const RequiredFloatingPoint: Story = {
tags: ["autodocs"],
args: {
jsonSchema: numericMagnitudeSchema,
uiSchema: numericUISchema,
uiSchema: numericVerticalUISchema,
},
}

export const RequiredFloatingPointWithDefault: Story = {
tags: ["autodocs"],
args: {
jsonSchema: numericTheNumberSchema,
uiSchema: numericUISchema,
uiSchema: numericVerticalUISchema,
},
}

export const OptionalFloatingPoint: Story = {
tags: ["autodocs"],
args: {
jsonSchema: numericWeightSchema,
uiSchema: numericUISchema,
uiSchema: numericVerticalUISchema,
},
}

export const RequiredInteger: Story = {
tags: ["autodocs"],
args: {
jsonSchema: numericSheepSchema,
uiSchema: numericUISchema,
uiSchema: numericVerticalUISchema,
},
}

Expand Down
32 changes: 32 additions & 0 deletions src/stories/layouts/HorizontalLayoutRenderer.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Meta, StoryObj } from "@storybook/react"
import { StorybookAntDJsonForm } from "../../common/StorybookAntDJsonForm"

import {
numericMagnitudeSchema,
numericHorizontalUISchema,
} from "../../testSchemas/numericSchema/numericSchema"

const meta: Meta<typeof StorybookAntDJsonForm> = {
title: "Layout/Horizontal",
component: StorybookAntDJsonForm,
tags: ["autodocs"],
args: {
uiSchema: numericHorizontalUISchema,
},
argTypes: {
uiSchema: {
control: "object",
},
},
}

export default meta
type Story = StoryObj<typeof StorybookAntDJsonForm>

export const NumericControl: Story = {
tags: ["autodocs"],
args: {
jsonSchema: numericMagnitudeSchema,
uiSchema: numericHorizontalUISchema,
},
}
32 changes: 32 additions & 0 deletions src/stories/layouts/VerticalLayoutRenderer.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Meta, StoryObj } from "@storybook/react"
import { StorybookAntDJsonForm } from "../../common/StorybookAntDJsonForm"

import {
numericMagnitudeSchema,
numericVerticalUISchema,
} from "../../testSchemas/numericSchema/numericSchema"

const meta: Meta<typeof StorybookAntDJsonForm> = {
title: "Layout/Vertical",
component: StorybookAntDJsonForm,
tags: ["autodocs"],
args: {
uiSchema: numericVerticalUISchema,
},
argTypes: {
uiSchema: {
control: "object",
},
},
}

export default meta
type Story = StoryObj<typeof StorybookAntDJsonForm>

export const NumericControl: Story = {
tags: ["autodocs"],
args: {
jsonSchema: numericMagnitudeSchema,
uiSchema: numericVerticalUISchema,
},
}
12 changes: 11 additions & 1 deletion src/testSchemas/numericSchema/numericSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const numericROISchema = {
required: ["numericValue"],
} satisfies JSONSchema

export const numericUISchema = {
export const numericVerticalUISchema = {
type: "VerticalLayout",
elements: [
{
Expand All @@ -89,6 +89,16 @@ export const numericUISchema = {
],
} satisfies UISchema

export const numericHorizontalUISchema = {
type: "HorizontalLayout",
elements: [
{
type: "Control",
scope: "#/properties/numericValue",
},
],
} satisfies UISchema

export const numericUSDUISchema = {
type: "VerticalLayout",
elements: [
Expand Down
Loading