mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import type { JSONSchema } from "json-schema-to-ts";
|
|
import { AnyOfField } from "./AnyOfField";
|
|
import { Field } from "./Field";
|
|
import { FieldWrapper, type FieldwrapperProps } from "./FieldWrapper";
|
|
import { useFieldContext } from "./Form";
|
|
|
|
export type ObjectFieldProps = {
|
|
path?: string;
|
|
schema?: Exclude<JSONSchema, boolean>;
|
|
label?: string | false;
|
|
wrapperProps?: Partial<FieldwrapperProps>;
|
|
};
|
|
|
|
export const ObjectField = ({
|
|
path = "",
|
|
schema: _schema,
|
|
label: _label,
|
|
wrapperProps = {}
|
|
}: ObjectFieldProps) => {
|
|
const { errors, ...ctx } = useFieldContext(path);
|
|
const schema = _schema ?? ctx.schema;
|
|
if (!schema) return "ObjectField: no schema";
|
|
const properties = schema.properties ?? {};
|
|
|
|
return (
|
|
<FieldWrapper
|
|
pointer={path}
|
|
errors={errors}
|
|
schema={schema}
|
|
wrapper="fieldset"
|
|
{...wrapperProps}
|
|
>
|
|
{Object.keys(properties).map((prop) => {
|
|
const schema = properties[prop];
|
|
const pointer = `${path}/${prop}`.replace(/\/+/g, "/");
|
|
if (!schema) return;
|
|
|
|
if (schema.anyOf || schema.oneOf) {
|
|
return <AnyOfField key={pointer} path={pointer} schema={schema} />;
|
|
}
|
|
|
|
return <Field key={pointer} name={pointer} schema={schema} />;
|
|
})}
|
|
</FieldWrapper>
|
|
);
|
|
};
|