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: add oneOf renderer #42

Merged
merged 14 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
"@testing-library/react": "^14.2.1",
"@testing-library/user-event": "^14.5.2",
"@types/lodash.isempty": "^4.4.9",
"@types/lodash.merge": "^4.6.9",
"@types/lodash.range": "^3.2.9",
"@types/lodash.startcase": "^4.4.9",
"@types/react": "^18.2.55",
"@types/react-dom": "^18.2.19",
"@typescript-eslint/eslint-plugin": "^6.14.0",
Expand All @@ -96,6 +98,8 @@
},
"dependencies": {
"lodash.isempty": "^4.4.0",
"lodash.range": "^3.2.0"
"lodash.merge": "^4.6.2",
"lodash.range": "^3.2.0",
"lodash.startcase": "^4.4.0"
}
}
29 changes: 28 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions src/common/ControlLabel.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { JSONSchema } from "json-schema-to-ts"
import { screen } from "@testing-library/react"
import { test, describe, expect } from "vitest"
import { render } from "../common/test-render"
import { UISchema } from "../ui-schema"

const schema = {
type: "object",
properties: {
labelMe: { title: "Label Me", oneOf: [{ type: "string" }] },
},
} satisfies JSONSchema

describe("ControlLabel", () => {
test("ControlLabel renders a title jsonschema property as the label", async () => {
render({ schema })
await screen.findByText("Label Me")
})
test("ControlLabel gives precedence to a string literal label property over the jsonschema title", async () => {
render({
schema,
uischema: {
type: "VerticalLayout",
elements: [
{
type: "Control",
scope: "#/properties/labelMe",
label: "No, Label ME",
},
],
} satisfies UISchema,
})
await screen.findByText("No, Label ME")
})
test("ControlLabel renders text from a label property that is a LabelDescription", async () => {
render({
schema,
uischema: {
type: "VerticalLayout",
elements: [
{
type: "Control",
scope: "#/properties/labelMe",
label: { text: "Srsly, label me instead" },
},
],
} satisfies UISchema,
})
await screen.findByText("Srsly, label me instead")
})
test("ControlLabel renders an AntD Text component from a LabelDescription", async () => {
render({
schema,
uischema: {
type: "VerticalLayout",
elements: [
{
type: "Control",
scope: "#/properties/labelMe",
label: {
text: "Srsly, label me instead",
type: "Text",
textProps: { disabled: true },
},
},
],
} satisfies UISchema,
})
await screen.findByText("Srsly, label me instead")

expect(screen.getByText("Srsly, label me instead")).toHaveClass(
"ant-typography-disabled",
)
})
test("ControlLabel renders an AntD Title component from a LabelDescription", async () => {
render({
schema,
uischema: {
type: "VerticalLayout",
elements: [
{
type: "Control",
scope: "#/properties/labelMe",
label: {
text: "Srsly, label me instead",
type: "Title",
titleProps: { copyable: true },
},
},
],
} satisfies UISchema,
})
await screen.findByLabelText("copy")
})
})
48 changes: 48 additions & 0 deletions src/common/ControlLabel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { JsonSchema, Helpers } from "@jsonforms/core"
import { ControlUISchema } from "../ui-schema"
import { Typography } from "antd"
import { assertNever } from "./assert-never"

// This consumes a LabelDescription (+ other formats) in a Control UI Schema
export function ControlLabel({
uischema,
schema,
}: {
uischema: ControlUISchema
schema: JsonSchema
}) {
const controlUISchema: ControlUISchema = uischema
const labelDescription = Helpers.createLabelDescriptionFrom(uischema, schema)
const text = labelDescription.show ? labelDescription.text : null

if (
typeof controlUISchema.label === "object" &&
"type" in controlUISchema.label
) {
const labelType = controlUISchema.label.type
switch (labelType) {
case "Text":
return (
<Typography.Text {...controlUISchema.label.textProps}>
{text}
</Typography.Text>
)

case "Title":
return (
<Typography.Title {...controlUISchema.label.titleProps}>
{text}
</Typography.Title>
)
default:
try {
assertNever(labelType)
} catch (e) {
console.error(
`Invalid value configured in Control UI Schema for label.type: '${labelType as string}'`,
)
}
}
}
return <Typography.Title>{text}</Typography.Title>
}
5 changes: 3 additions & 2 deletions src/common/StorybookAntDJsonForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import {
import { UISchema } from "../ui-schema"
import { AntDJsonForm } from "./AntDJsonForm"
import { useState } from "react"
import { JSONSchema } from "json-schema-to-ts"

type Props = {
data?: Record<string, unknown>
jsonSchema: JsonSchema7
jsonSchema: JSONSchema
rendererRegistryEntries: JsonFormsRendererRegistryEntry[]
uiSchema?: UISchema
uiSchemaRegistryEntries?: JsonFormsUISchemaRegistryEntry[]
Expand All @@ -35,7 +36,7 @@ export function StorybookAntDJsonForm({
return (
<AntDJsonForm
uiSchema={uiSchema}
jsonSchema={jsonSchema}
jsonSchema={jsonSchema as JsonSchema7}
data={data}
updateData={(newData) => updateData(newData)}
uiSchemaRegistryEntries={uiSchemaRegistryEntries}
Expand Down
18 changes: 18 additions & 0 deletions src/common/usePreviousValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useEffect, useState } from "react"

/**
* Hook that returns the what was passed in the previous render.
* On the first render, returns undefined. Eventually
* "catches up" to the current value.
*/
export function usePreviousValue<T>(value: T): T | undefined {
const [prev, setPrev] = useState<T | undefined>(undefined)

useEffect(() => {
if (value !== prev) {
setPrev(value)
}
}, [value, prev])

return prev
}
1 change: 0 additions & 1 deletion src/controls/BooleanControl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ test("handles onChange event correctly", async () => {
},
data: { name: false },
onChange: (result) => {
console.log(result)
updateData(result)
},
})
Expand Down
Loading
Loading