-
Notifications
You must be signed in to change notification settings - Fork 3
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
Adding ObjectControl #26
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { test, expect, describe } from "vitest"; | ||
import { objectSchema, objectUISchemaWithName, objectUISchemaWithRule } from "../testSchemas/objectSchema" | ||
import { render } from "../common/test-render"; | ||
import { screen } from "@testing-library/react" | ||
|
||
describe("ObjectControl", () => { | ||
test("renders nested fields", () => { | ||
render({ schema: objectSchema }) | ||
|
||
expect(screen.getByText("Name")).not.toBeNull() | ||
expect(screen.getByText("Last Name")).not.toBeNull() | ||
}) | ||
|
||
describe("only renders when visible", () => { | ||
test("property is not visible if not included in UISchema", () => { | ||
render({ schema: objectSchema, uischema: objectUISchemaWithName }) | ||
|
||
expect(screen.queryByText("Last Name")).toBeNull() | ||
}) | ||
|
||
describe("manage visibility with condition rules", () => { | ||
test("hide field when condition matches", () => { | ||
render({ | ||
data: { name: "John", lastName: "Doe" }, | ||
schema: objectSchema, | ||
uischema: objectUISchemaWithRule, | ||
}) | ||
|
||
expect(screen.queryByText("Last Name")).toBeNull() | ||
}) | ||
|
||
test("render field when condition doesn't match", () => { | ||
render({ | ||
data: { name: "Bob", lastName: "Doe" }, | ||
schema: objectSchema, | ||
uischema: objectUISchemaWithRule, | ||
}) | ||
|
||
expect(screen.getByText("Name")).not.toBeNull() | ||
expect(screen.getByText("Last Name")).not.toBeNull() | ||
}) | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useMemo } from "react" | ||
import { | ||
findUISchema, | ||
Generate, | ||
StatePropsOfControlWithDetail | ||
} from "@jsonforms/core" | ||
import { JsonFormsDispatch } from "@jsonforms/react" | ||
import isEmpty from "lodash.isempty" | ||
|
||
|
||
|
||
export function ObjectControl({ | ||
renderers, | ||
cells, | ||
uischemas, | ||
schema, | ||
label, | ||
path, | ||
visible, | ||
enabled, | ||
uischema, | ||
rootSchema, | ||
}: StatePropsOfControlWithDetail) { | ||
const detailUiSchema = useMemo( | ||
() => | ||
findUISchema( | ||
uischemas ?? [], | ||
schema, | ||
uischema.scope, | ||
path, | ||
() => | ||
isEmpty(path) | ||
? Generate.uiSchema(schema, "VerticalLayout") | ||
: { ...Generate.uiSchema(schema, "Group"), label }, | ||
uischema, | ||
rootSchema, | ||
), | ||
[uischemas, schema, path, label, uischema, rootSchema], | ||
) | ||
|
||
if (!visible) { | ||
return null | ||
} | ||
Comment on lines
+41
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I've been moving these to the top so you don't call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since findUISchema is within a react hook it cannot be conditional. |
||
|
||
return ( | ||
<JsonFormsDispatch | ||
visible={visible} | ||
uischemas={uischemas} | ||
enabled={enabled} | ||
schema={schema} | ||
uischema={detailUiSchema} | ||
path={path} | ||
renderers={renderers} | ||
cells={cells} | ||
/> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { GroupLayout, OwnPropsOfRenderer } from "@jsonforms/core"; | ||
import { UISchema } from "../ui-schema"; | ||
import { Divider } from "antd"; | ||
import { AntDLayoutRenderer } from "./LayoutRenderer"; | ||
|
||
export type LayoutRendererProps = OwnPropsOfRenderer & { | ||
elements: UISchema[]; | ||
}; | ||
|
||
export function GroupLayoutRenderer({ | ||
visible, | ||
enabled, | ||
uischema, | ||
...props | ||
}: LayoutRendererProps) { | ||
const groupLayout = uischema as GroupLayout; | ||
return ( | ||
<> | ||
<Divider /> | ||
{groupLayout?.label && <b>{groupLayout.label}</b>} | ||
<AntDLayoutRenderer | ||
{...props} | ||
visible={visible} | ||
enabled={enabled} | ||
elements={groupLayout.elements} | ||
/> | ||
<Divider /> | ||
</> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,67 @@ | ||
import { JsonFormsRendererRegistryEntry,JsonFormsCellRendererRegistryEntry, isBooleanControl, isStringControl, rankWith, uiTypeIs } from "@jsonforms/core"; | ||
import { withJsonFormsControlProps, withJsonFormsLabelProps, withJsonFormsCellProps, withJsonFormsLayoutProps } from "@jsonforms/react"; | ||
import { | ||
JsonFormsRendererRegistryEntry, | ||
JsonFormsCellRendererRegistryEntry, | ||
isBooleanControl, | ||
isStringControl, | ||
rankWith, | ||
uiTypeIs, | ||
isObjectControl, | ||
isLayout, | ||
not, | ||
and, | ||
} from "@jsonforms/core"; | ||
import { | ||
withJsonFormsControlProps, | ||
withJsonFormsLabelProps, | ||
withJsonFormsCellProps, | ||
withJsonFormsLayoutProps, | ||
withJsonFormsDetailProps, | ||
} from "@jsonforms/react"; | ||
|
||
import { BooleanControl } from "./controls/BooleanControl"; | ||
import { AlertControl } from "./controls/AlertControl"; | ||
import { TextControl } from "./controls/TextControl"; | ||
import { UnknownControl } from "./controls/UnknownControl"; | ||
import { VerticalLayoutRenderer } from "./layouts/VerticalLayout"; | ||
|
||
import { VerticalLayoutRenderer } from "./layouts/VerticalLayoutRenderer"; | ||
import { ObjectControl } from "./controls/ObjectControl"; | ||
import { GroupLayoutRenderer } from "./layouts/GroupLayoutRenderer"; | ||
import React from "react"; | ||
|
||
// Ordered from lowest rank to highest rank. Higher rank renderers will be preferred over lower rank renderers. | ||
export const rendererRegistryEntries: JsonFormsRendererRegistryEntry[] = [ | ||
{ tester: rankWith(1, () => true), renderer: withJsonFormsControlProps(UnknownControl) }, | ||
{ tester: rankWith(2, uiTypeIs("VerticalLayout")), renderer: withJsonFormsLayoutProps(VerticalLayoutRenderer) }, | ||
{ tester: rankWith(2, isBooleanControl), renderer: withJsonFormsControlProps(BooleanControl) }, | ||
{ tester: rankWith(2, isStringControl), renderer: withJsonFormsControlProps(TextControl) }, | ||
{ tester: rankWith(2, uiTypeIs("Label")), renderer: withJsonFormsLabelProps(AlertControl) }, | ||
{ | ||
tester: rankWith(1, () => true), | ||
renderer: withJsonFormsControlProps(UnknownControl), | ||
}, | ||
{ | ||
tester: rankWith(1, uiTypeIs("Group")), | ||
renderer: React.memo(GroupLayoutRenderer), | ||
}, | ||
{ | ||
tester: rankWith(2, uiTypeIs("VerticalLayout")), | ||
renderer: withJsonFormsLayoutProps(VerticalLayoutRenderer), | ||
}, | ||
{ | ||
tester: rankWith(2, isBooleanControl), | ||
renderer: withJsonFormsControlProps(BooleanControl), | ||
}, | ||
{ | ||
tester: rankWith(2, isStringControl), | ||
renderer: withJsonFormsControlProps(TextControl), | ||
}, | ||
{ | ||
tester: rankWith(2, uiTypeIs("Label")), | ||
renderer: withJsonFormsLabelProps(AlertControl), | ||
}, | ||
{ | ||
tester: rankWith(10, and(isObjectControl, not(isLayout))), | ||
renderer: withJsonFormsDetailProps(ObjectControl), | ||
}, | ||
]; | ||
|
||
export const cellRegistryEntries: JsonFormsCellRendererRegistryEntry[] = [ | ||
{ tester: rankWith(1, () => true), cell: withJsonFormsCellProps(UnknownControl)} | ||
{ | ||
tester: rankWith(1, () => true), | ||
cell: withJsonFormsCellProps(UnknownControl), | ||
}, | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
import { rendererRegistryEntries } from "../../renderers"; | ||
import { StorybookAntDJsonForm } from "../../common/StorybookAntDJsonForm"; | ||
import { objectSchema, objectUISchemaWithName, objectUISchemaWithNameAndLastName, objectUISchemaWithRule } from "../../testSchemas/objectSchema"; | ||
|
||
const meta: Meta<typeof StorybookAntDJsonForm> = { | ||
title: "Control/Object", | ||
component: StorybookAntDJsonForm, | ||
tags: ["autodocs"], | ||
args: { | ||
jsonSchema: objectSchema, | ||
rendererRegistryEntries: [ | ||
...rendererRegistryEntries, | ||
] | ||
}, | ||
argTypes: { | ||
rendererRegistryEntries: {}, | ||
jsonSchema: { | ||
control: "object", | ||
} | ||
} | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof StorybookAntDJsonForm>; | ||
|
||
export const ObjectWithUISchemaContainingOnlyName: Story = { | ||
tags: ["autodocs"], | ||
args: { | ||
jsonSchema: objectSchema, | ||
uiSchema: objectUISchemaWithName, | ||
}, | ||
} | ||
|
||
export const ObjectWithUISchemaContainingBothNameAndLastName: Story = { | ||
tags: ["autodocs"], | ||
args: { | ||
jsonSchema: objectSchema, | ||
uiSchema: objectUISchemaWithNameAndLastName, | ||
}, | ||
} | ||
|
||
export const ObjectWithRuleHidingLastNameIfNameIsJohn: Story = { | ||
tags: ["autodocs"], | ||
args: { | ||
jsonSchema: objectSchema, | ||
uiSchema: objectUISchemaWithRule, | ||
}, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I migrated the GroupRenderer since I think this needs that