import type { FieldProps, FormContextType, RJSFSchema, StrictRJSFSchema } from "@rjsf/utils"; import { SimpleRenderer } from "core"; import { ucFirst, ucFirstAll } from "core/utils"; import { useState } from "react"; const modes = ["field", "code"] as const; type Mode = (typeof modes)[number]; export function TemplateField< T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any, >(props: FieldProps) { const formData = props.formData; const hasMarkup = SimpleRenderer.hasMarkup(formData!); const [mode, setMode] = useState(hasMarkup ? "code" : "field"); const [values, setValues] = useState>({ field: hasMarkup ? "" : formData, code: hasMarkup ? formData : "", }); //console.log("TemplateField", props); const { SchemaField } = props.registry.fields; const { schema } = props; function handleModeSwitch(mode: Mode) { setMode(mode); props.onChange(values[mode]); } function onChange(value: any) { setValues({ ...values, [mode]: value }); props.onChange(value); } let _schema: any = schema; if (!("anyOf" in schema)) { _schema = { anyOf: [schema, { type: "string" }], }; } const [fieldSchema, codeSchema] = _schema.anyOf; const currentSchema = mode === "field" ? fieldSchema : codeSchema; const currentValue = values[mode]; const uiSchema = mode === "field" ? { "ui:label": false } : { "ui:label": false, "ui:widget": "textarea", "ui:options": { rows: 1 }, }; return (
{/* @ts-ignore */}
); }