Skip to content

Commit

Permalink
fix: refactor renderers to renderer registry entries for clarity (#45)
Browse files Browse the repository at this point in the history
* fix: refactor renderers to renderer registry entries for clarity

* remove file that got moved
  • Loading branch information
DrewHoo authored Mar 19, 2024
1 parent ba7cd85 commit 170007b
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/common/AntDJsonForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { UISchema } from "../ui-schema"
import {
rendererRegistryEntries as _rendererRegistryEntries,
cellRegistryEntries,
} from "../renderers"
} from "../renderer-registry-entries"

type Props = {
data: Record<string, unknown>
Expand Down
5 changes: 4 additions & 1 deletion src/common/FormStateWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { Form } from "antd"

import { JsonSchema7 } from "@jsonforms/core"
import { UISchema } from "../ui-schema"
import { cellRegistryEntries, rendererRegistryEntries } from "../renderers"
import {
cellRegistryEntries,
rendererRegistryEntries,
} from "../renderer-registry-entries"
import { useState } from "react"

type RenderProps<T extends Record<string, unknown>> = {
Expand Down
3 changes: 3 additions & 0 deletions src/controls/BooleanControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ControlProps, isDescriptionHidden } from "@jsonforms/core"
import { Form } from "antd"
import { Checkbox } from "../antd/Checkbox"
import { QuestionCircleOutlined } from "@ant-design/icons"
import { withJsonFormsControlProps } from "@jsonforms/react"

export function BooleanControl({
data,
Expand Down Expand Up @@ -72,3 +73,5 @@ export function BooleanControl({
</Form.Item>
)
}

export const BooleanRenderer = withJsonFormsControlProps(BooleanControl)
3 changes: 3 additions & 0 deletions src/controls/NumericControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ControlProps, RendererProps } from "@jsonforms/core"
import { Col, Form } from "antd"
import type { Rule } from "antd/es/form"
import { InputNumber } from "../antd/InputNumber"
import { withJsonFormsControlProps } from "@jsonforms/react"

export const NumericControl = (props: ControlProps & RendererProps) => {
if (!props.visible) return null
Expand All @@ -27,3 +28,5 @@ export const NumericControl = (props: ControlProps & RendererProps) => {
</Form.Item>
)
}

export const NumericRenderer = withJsonFormsControlProps(NumericControl)
4 changes: 4 additions & 0 deletions src/controls/NumericSliderControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Col, Form, Row } from "antd"
import type { Rule } from "antd/es/form"
import { InputNumber } from "../antd/InputNumber"
import { Slider } from "../antd/Slider"
import { withJsonFormsControlProps } from "@jsonforms/react"

export const NumericSliderControl = (props: ControlProps & RendererProps) => {
if (!props.visible) return null
Expand Down Expand Up @@ -33,3 +34,6 @@ export const NumericSliderControl = (props: ControlProps & RendererProps) => {
</Form.Item>
)
}

export const NumericSliderRenderer =
withJsonFormsControlProps(NumericSliderControl)
4 changes: 3 additions & 1 deletion src/controls/ObjectControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Generate,
StatePropsOfControlWithDetail,
} from "@jsonforms/core"
import { JsonFormsDispatch } from "@jsonforms/react"
import { JsonFormsDispatch, withJsonFormsDetailProps } from "@jsonforms/react"
import isEmpty from "lodash.isempty"

export function ObjectControl({
Expand Down Expand Up @@ -53,3 +53,5 @@ export function ObjectControl({
/>
)
}

export const ObjectRenderer = withJsonFormsDetailProps(ObjectControl)
3 changes: 3 additions & 0 deletions src/controls/TextControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { ControlProps } from "@jsonforms/core"

import type { TextControlOptions, TextControlType } from "../ui-schema"
import { assertNever } from "../common/assert-never"
import { withJsonFormsControlProps } from "@jsonforms/react"
interface TextControlProps extends ControlProps {
data: string
handleChange(path: string, value: string): void
Expand Down Expand Up @@ -113,3 +114,5 @@ function TextControlInput({ type, ...rest }: TextControlInputProps) {
function coerceToString(value: number) {
return value.toString()
}

export const TextRenderer = withJsonFormsControlProps(TextControl)
3 changes: 3 additions & 0 deletions src/controls/UnknownControl.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CellProps, ControlProps } from "@jsonforms/core"
import { withJsonFormsControlProps } from "@jsonforms/react"

export function UnknownControl(props: ControlProps | CellProps) {
// console.log({ schema: props.schema, path: props.path, schemaPath: props.schemaPath, props })
Expand All @@ -9,3 +10,5 @@ export function UnknownControl(props: ControlProps | CellProps) {
</div>
)
}

export const UnknownRenderer = withJsonFormsControlProps(UnknownControl)
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
export { rendererRegistryEntries, cellRegistryEntries } from "./renderers"
export {
rendererRegistryEntries,
cellRegistryEntries,
} from "./renderer-registry-entries"
export type * from "./ui-schema"

export { AlertLayout } from "./layouts/AlertLayout"
export { BooleanControl } from "./controls/BooleanControl"
export { TextControl } from "./controls/TextControl"
export { UnknownControl } from "./controls/UnknownControl"
export { HorizontalLayout } from "./layouts/HorizontalLayout"
export { VerticalLayout } from "./layouts/VerticalLayout"
export { ObjectControl } from "./controls/ObjectControl"
export { GroupLayout } from "./layouts/GroupLayout"
export { HorizontalLayout } from "./layouts/HorizontalLayout"
export { NumericControl } from "./controls/NumericControl"
export { NumericSliderControl } from "./controls/NumericSliderControl"
export { ObjectControl } from "./controls/ObjectControl"
export { TextControl } from "./controls/TextControl"
export { UnknownControl } from "./controls/UnknownControl"
export { VerticalLayout } from "./layouts/VerticalLayout"
3 changes: 3 additions & 0 deletions src/layouts/AlertLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LabelProps, RendererProps } from "@jsonforms/core"
import { Alert } from "antd"
import { AlertLayoutOptions } from "../ui-schema"
import { withJsonFormsLabelProps } from "@jsonforms/react"

export function AlertLayout({ text, uischema }: LabelProps & RendererProps) {
const options = uischema.options as AlertLayoutOptions
Expand All @@ -13,3 +14,5 @@ export function AlertLayout({ text, uischema }: LabelProps & RendererProps) {
/>
)
}

export const AlertLayoutRenderer = withJsonFormsLabelProps(AlertLayout)
3 changes: 3 additions & 0 deletions src/layouts/GroupLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import { UISchema } from "../ui-schema"
import { Divider } from "antd"
import { AntDLayout } from "./LayoutRenderer"
import React from "react"

export type LayoutRendererProps = OwnPropsOfRenderer & {
elements: UISchema[]
Expand All @@ -31,3 +32,5 @@ export function GroupLayout({
</>
)
}

export const GroupLayoutRenderer = React.memo(GroupLayout)
4 changes: 4 additions & 0 deletions src/layouts/HorizontalLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import isEmpty from "lodash.isempty"
import { AntDLayout, AntDLayoutProps } from "./LayoutRenderer"
import { HorizontalLayout as HorizontalLayoutUISchema } from "../ui-schema"
import { Form, Row } from "antd"
import { withJsonFormsLayoutProps } from "@jsonforms/react"

export function HorizontalLayout({
uischema,
Expand Down Expand Up @@ -37,3 +38,6 @@ export function HorizontalLayout({
</Form>
)
}

export const HorizontalLayoutRenderer =
withJsonFormsLayoutProps(HorizontalLayout)
3 changes: 3 additions & 0 deletions src/layouts/VerticalLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LayoutProps, GroupLayout } from "@jsonforms/core"
import { AntDLayout, AntDLayoutProps } from "./LayoutRenderer"
import { Form, FormInstance, FormProps } from "antd"
import { VerticalLayout as VerticalLayoutUISchema } from "../ui-schema"
import { withJsonFormsLayoutProps } from "@jsonforms/react"

export function VerticalLayout({
uischema,
Expand Down Expand Up @@ -38,3 +39,5 @@ function getFormLayoutOptions(form: FormInstance): Partial<FormProps> {
component: form ? "div" : "form",
}
}

export const VerticalLayoutRenderer = withJsonFormsLayoutProps(VerticalLayout)
49 changes: 21 additions & 28 deletions src/renderers.ts → src/renderer-registry-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,52 @@ import {
and,
or,
} from "@jsonforms/core"
import {
withJsonFormsControlProps,
withJsonFormsLabelProps,
withJsonFormsCellProps,
withJsonFormsLayoutProps,
withJsonFormsDetailProps,
} from "@jsonforms/react"
import { withJsonFormsCellProps } from "@jsonforms/react"

import { BooleanControl } from "./controls/BooleanControl"
import { AlertLayout } from "./layouts/AlertLayout"
import { TextControl } from "./controls/TextControl"
import { UnknownControl } from "./controls/UnknownControl"
import { HorizontalLayout } from "./layouts/HorizontalLayout"
import { VerticalLayout } from "./layouts/VerticalLayout"
import { ObjectControl } from "./controls/ObjectControl"
import { GroupLayout } from "./layouts/GroupLayout"
import { NumericControl } from "./controls/NumericControl"
import { NumericSliderControl } from "./controls/NumericSliderControl"
import React from "react"
import { BooleanRenderer } from "./controls/BooleanControl"
import { AlertLayoutRenderer } from "./layouts/AlertLayout"
import { TextRenderer } from "./controls/TextControl"
import { UnknownControl, UnknownRenderer } from "./controls/UnknownControl"
import { HorizontalLayoutRenderer } from "./layouts/HorizontalLayout"
import { VerticalLayoutRenderer } from "./layouts/VerticalLayout"
import { ObjectRenderer } from "./controls/ObjectControl"
import { GroupLayoutRenderer } from "./layouts/GroupLayout"
import { NumericRenderer } from "./controls/NumericControl"
import { NumericSliderRenderer } from "./controls/NumericSliderControl"

// 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),
renderer: UnknownRenderer,
},
{
tester: rankWith(1, uiTypeIs("Group")),
renderer: React.memo(GroupLayout),
renderer: GroupLayoutRenderer,
},
{
tester: rankWith(2, uiTypeIs("HorizontalLayout")),
renderer: withJsonFormsLayoutProps(HorizontalLayout),
renderer: HorizontalLayoutRenderer,
},
{
tester: rankWith(2, uiTypeIs("VerticalLayout")),
renderer: withJsonFormsLayoutProps(VerticalLayout),
renderer: VerticalLayoutRenderer,
},
{
tester: rankWith(2, isBooleanControl),
renderer: withJsonFormsControlProps(BooleanControl),
renderer: BooleanRenderer,
},
{
tester: rankWith(2, isStringControl),
renderer: withJsonFormsControlProps(TextControl),
renderer: TextRenderer,
},
{
tester: rankWith(2, uiTypeIs("Label")),
renderer: withJsonFormsLabelProps(AlertLayout),
renderer: AlertLayoutRenderer,
},
{
tester: rankWith(2, or(isNumberControl, isIntegerControl)),
renderer: withJsonFormsControlProps(NumericControl),
renderer: NumericRenderer,
},
{
tester: rankWith(
Expand All @@ -79,11 +72,11 @@ export const rendererRegistryEntries: JsonFormsRendererRegistryEntry[] = [
}),
),
),
renderer: withJsonFormsControlProps(NumericSliderControl),
renderer: NumericSliderRenderer,
},
{
tester: rankWith(10, and(isObjectControl, not(isLayout))),
renderer: withJsonFormsDetailProps(ObjectControl),
renderer: ObjectRenderer,
},
]

Expand Down
2 changes: 1 addition & 1 deletion src/stories/controls/BooleanControl.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta, StoryObj } from "@storybook/react"
import { rendererRegistryEntries } from "../../renderers"
import { rendererRegistryEntries } from "../../renderer-registry-entries"
import { UISchema } from "../../ui-schema"
import { StorybookAntDJsonForm } from "../../common/StorybookAntDJsonForm"

Expand Down
2 changes: 1 addition & 1 deletion src/stories/controls/ObjectControl.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta, StoryObj } from "@storybook/react"
import { rendererRegistryEntries } from "../../renderers"
import { rendererRegistryEntries } from "../../renderer-registry-entries"
import { StorybookAntDJsonForm } from "../../common/StorybookAntDJsonForm"
import {
objectSchema,
Expand Down
2 changes: 1 addition & 1 deletion src/stories/controls/TextControl.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta, StoryObj } from "@storybook/react"
import { rendererRegistryEntries } from "../../renderers"
import { rendererRegistryEntries } from "../../renderer-registry-entries"
import { TextControlOptions, UISchema } from "../../ui-schema"
import { StorybookAntDJsonForm } from "../../common/StorybookAntDJsonForm"

Expand Down
2 changes: 1 addition & 1 deletion src/stories/layouts/AlertLayout.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta, StoryObj } from "@storybook/react"
import { rendererRegistryEntries } from "../../renderers"
import { rendererRegistryEntries } from "../../renderer-registry-entries"
import { UISchema } from "../../ui-schema"
import { StorybookAntDJsonForm } from "../../common/StorybookAntDJsonForm"

Expand Down

0 comments on commit 170007b

Please sign in to comment.