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: Implement DatetimeControl and add tests #24

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
"@jsonforms/core": "^3.2.1",
"@jsonforms/react": "^3.2.1",
"antd": "^5.14.0",
"date-fns": "^3.3.1",
"rc-picker": "^4.2.0",
TrangPham marked this conversation as resolved.
Show resolved Hide resolved
"react": "^17 || ^18"
},
"devDependencies": {
Expand Down Expand Up @@ -79,6 +81,7 @@
"@typescript-eslint/parser": "^6.14.0",
"@vitejs/plugin-react": "^4.2.1",
"antd": "^5.14.0",
"date-fns": "^3.3.1",
"eslint": "^8.55.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
Expand All @@ -87,6 +90,7 @@
"eslint-plugin-testing-library": "^6.2.0",
"jsdom": "^24.0.0",
"json-schema-to-ts": "^3.0.0",
"rc-picker": "^4.2.0",
"prettier": "3.2.5",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
67 changes: 63 additions & 4 deletions pnpm-lock.yaml

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

55 changes: 55 additions & 0 deletions src/controls/DatetimeControl.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, expect, test, it } from "vitest"
import { render } from "../common/test-render"

import { screen } from "@testing-library/react"
import {
datetimeSchema,
datetimeUISchema,
datetimeUISchemaWithRule,
} from "../testSchemas/datetimeSchema"
import { isDateTimeControl, rankWith } from "@jsonforms/core"

describe("DatetimeControlTester", () => {
test("tester works", () => {
const uiSchema = {
type: "Control",
scope: "#/properties/datetime",
}
const schema = {
type: "string",
title: "The Future is Now",
format: "date-time",
}
const context = { rootSchema: datetimeSchema, config: {} }

expect(rankWith(1, isDateTimeControl)(uiSchema, schema, context)).toBe(1)
})
})
describe("DatetimeControl", () => {
const timestamp = "2023-07-18T01:02:01.182Z"
const title = "The Future is Now"
test("renders a datetime input with no UISchema provided", async () => {
render({
schema: datetimeSchema,
})
await screen.findByText(title)
})

it("Follows the hide rule", () => {
render({
data: { datetime: timestamp },
schema: datetimeSchema,
uischema: datetimeUISchemaWithRule,
})
expect(screen.queryByText(title)).toBeNull()
})

it("renders when data is included", async () => {
render({
data: { datetime: timestamp },
schema: datetimeSchema,
uischema: datetimeUISchema,
})
await screen.findByText(title)
})
})
35 changes: 35 additions & 0 deletions src/controls/DatetimeControl.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ControlProps } from "@jsonforms/core"
import { withJsonFormsControlProps } from "@jsonforms/react"
import { DatePicker, Form } from "antd"
import dateFnsGenerateConfig from "rc-picker/lib/generate/dateFns"

interface DatetimeControlProps extends ControlProps {
data: string
handleChange(path: string, value: Date | null): void
path: string
}

const MyDatePicker = DatePicker.generatePicker<Date>(dateFnsGenerateConfig)

export function DatetimeControl({
data,
handleChange,
path,
label,
visible,
id,
required,
}: DatetimeControlProps) {
const dateFormat = "MM/DD/YYYY"
TrangPham marked this conversation as resolved.
Show resolved Hide resolved
return !visible ? null : (
<Form.Item label={label} id={id} name={path} required={required}>
<MyDatePicker
value={data ? new Date(data) : null}
format={dateFormat}
onChange={(e) => handleChange(path, e)}
/>
</Form.Item>
)
}

export const DatetimeRenderer = withJsonFormsControlProps(DatetimeControl)
6 changes: 6 additions & 0 deletions src/renderer-registry-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
or,
isPrimitiveArrayControl,
isOneOfControl,
isDateTimeControl,
} from "@jsonforms/core"
import { withJsonFormsCellProps } from "@jsonforms/react"

Expand All @@ -37,6 +38,7 @@ import {
PrimitiveArrayRenderer,
} from "./controls/ArrayControl"
import { OneOfRenderer } from "./controls/combinators/OneOfControl"
import { DatetimeRenderer } from "./controls/DatetimeControl"

// Ordered from lowest rank to highest rank. Higher rank renderers will be preferred over lower rank renderers.
export const rendererRegistryEntries: JsonFormsRendererRegistryEntry[] = [
Expand Down Expand Up @@ -72,6 +74,10 @@ export const rendererRegistryEntries: JsonFormsRendererRegistryEntry[] = [
tester: rankWith(2, or(isNumberControl, isIntegerControl)),
renderer: NumericRenderer,
},
{
tester: rankWith(3, isDateTimeControl),
renderer: DatetimeRenderer,
},
{
tester: rankWith(
3,
Expand Down
49 changes: 49 additions & 0 deletions src/stories/controls/DatetimeControl.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Meta, StoryObj } from "@storybook/react"
import { rendererRegistryEntries } from "../../renderer-registry-entries"
import { StorybookAntDJsonForm } from "../../common/StorybookAntDJsonForm"
import {
datetimeSchema,
datetimeUISchema,
} from "../../testSchemas/datetimeSchema"

const meta: Meta<typeof StorybookAntDJsonForm> = {
title: "Control/Datetime",
component: StorybookAntDJsonForm,
tags: ["autodocs"],
args: {
jsonSchema: datetimeSchema,
rendererRegistryEntries: [...rendererRegistryEntries],
},
argTypes: {
rendererRegistryEntries: { table: { disable: true } },
uiSchemaRegistryEntries: { table: { disable: true } },
jsonSchema: {
control: "object",
},
data: { table: { disable: true } },
config: { control: "object" },
onChange: { table: { disable: true, action: "on-change" } },
},
}

export default meta
type Story = StoryObj<typeof StorybookAntDJsonForm>

export const Datetime: Story = {
tags: ["autodocs"],
args: {
jsonSchema: datetimeSchema,
uiSchema: datetimeUISchema,
},
}

export const RequiredDatetime: Story = {
tags: ["autodocs"],
args: {
jsonSchema: {
...datetimeSchema,
required: ["datetime"],
},
uiSchema: datetimeUISchema,
},
}
38 changes: 38 additions & 0 deletions src/testSchemas/datetimeSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { JSONSchema } from "json-schema-to-ts"
import { UISchema } from "../ui-schema"
import { RuleEffect } from "@jsonforms/core"

export const datetimeSchema = {
type: "object",
properties: {
datetime: {
type: "string",
title: "The Future is Now",
format: "date-time",
},
},
} satisfies JSONSchema

export const datetimeUISchema = {
type: "VerticalLayout",
elements: [
{
type: "Control",
scope: "#/properties/datetime",
},
],
} satisfies UISchema

export const datetimeUISchemaWithRule = {
type: "VerticalLayout",
elements: [
{
type: "Control",
scope: "#/properties/datetime",
rule: {
effect: RuleEffect.HIDE,
condition: {},
},
},
],
} satisfies UISchema
Loading