-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(AnyOfControl): fix missing default combinator value bug (#75)
- Loading branch information
Showing
14 changed files
with
314 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,68 @@ | ||
import { JsonForms } from "@jsonforms/react" | ||
import { Form } from "antd" | ||
/* eslint-disable @typescript-eslint/no-misused-promises */ | ||
import { JsonForms, JsonFormsReactProps } from "@jsonforms/react" | ||
import { Button, Form } from "antd" | ||
|
||
import { JsonFormsUISchemaRegistryEntry, JsonSchema7 } from "@jsonforms/core" | ||
import { UISchema } from "../ui-schema" | ||
import { | ||
cellRegistryEntries, | ||
rendererRegistryEntries, | ||
} from "../renderer-registry-entries" | ||
import { useState } from "react" | ||
import { useCallback, useState } from "react" | ||
|
||
export type RenderProps<T extends Record<string, unknown>, S> = { | ||
schema: S | ||
data?: T | ||
uischema?: UISchema<S> | ||
onChange?: (result: { data: T }) => void | ||
onChange?: JsonFormsReactProps["onChange"] | ||
uiSchemaRegistryEntries?: JsonFormsUISchemaRegistryEntry[] | ||
} | ||
|
||
export function FormStateWrapper<T extends Record<string, unknown>, S>({ | ||
schema, | ||
uischema, | ||
data: initialData, | ||
onChange, | ||
onChange: _onChange, | ||
uiSchemaRegistryEntries, | ||
}: RenderProps<T, S>) { | ||
const [data, setData] = useState<Record<string, unknown> | undefined>( | ||
initialData, | ||
const [result, setResult] = useState({ | ||
data: initialData, | ||
}) | ||
const onChange: Required<JsonFormsReactProps>["onChange"] = useCallback( | ||
(r) => { | ||
_onChange?.(r) | ||
setResult(r) | ||
}, | ||
[_onChange], | ||
) | ||
const [form] = Form.useForm() | ||
const onSubmit = useCallback(async () => { | ||
const formValidationResult = await form | ||
.validateFields() | ||
.then((values: Record<string, unknown>) => values) | ||
.catch((errorInfo: { errorFields: unknown[] }) => errorInfo) | ||
|
||
if ("errorFields" in formValidationResult) { | ||
return // nothing to do; validateFields will have already rendered error messages on form fields | ||
} | ||
}, [form]) | ||
|
||
return ( | ||
<Form> | ||
<Form form={form}> | ||
<JsonForms | ||
schema={schema as JsonSchema7} | ||
uischema={uischema} | ||
renderers={rendererRegistryEntries} | ||
cells={cellRegistryEntries} | ||
data={data} | ||
data={result?.data} | ||
uischemas={uiSchemaRegistryEntries ?? []} | ||
{...(onChange | ||
? { onChange } | ||
: { | ||
onChange: (result) => | ||
setData(result.data as Record<string, unknown>), | ||
})} | ||
onChange={onChange} | ||
/> | ||
<Form.Item> | ||
<Button type="primary" onClick={onSubmit}> | ||
Submit | ||
</Button> | ||
</Form.Item> | ||
</Form> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.