mirror of
https://github.com/ritz078/transform.git
synced 2026-11-26 10:42:54 +03:00
63 lines
1.4 KiB
TypeScript
63 lines
1.4 KiB
TypeScript
import ConversionPanel from "@components/ConversionPanel";
|
|
import { EditorPanelProps } from "@components/EditorPanel";
|
|
import Form, { InputType } from "@components/Form";
|
|
import { useSettings } from "@hooks/useSettings";
|
|
import * as React from "react";
|
|
import { useCallback } from "react";
|
|
|
|
interface Settings {
|
|
rootName: string;
|
|
}
|
|
|
|
const formFields = [
|
|
{
|
|
type: InputType.TEXT_INPUT,
|
|
key: "rootName",
|
|
label: "Root Schema Name"
|
|
}
|
|
];
|
|
|
|
export default function JsonToZod() {
|
|
const name = "JSON to Zod Schema";
|
|
|
|
const [settings, setSettings] = useSettings(name, {
|
|
rootName: "schema"
|
|
});
|
|
|
|
const transformer = useCallback(
|
|
async ({ value }) => {
|
|
const { jsonToZod } = await import("json-to-zod");
|
|
return jsonToZod(JSON.parse(value), settings.rootName, true);
|
|
},
|
|
[settings]
|
|
);
|
|
|
|
const getSettingsElement = useCallback<EditorPanelProps["settingElement"]>(
|
|
({ open, toggle }) => {
|
|
return (
|
|
<Form<Settings>
|
|
title={name}
|
|
onSubmit={setSettings}
|
|
open={open}
|
|
toggle={toggle}
|
|
formsFields={formFields}
|
|
initialValues={settings}
|
|
/>
|
|
);
|
|
},
|
|
[]
|
|
);
|
|
|
|
return (
|
|
<ConversionPanel
|
|
transformer={transformer}
|
|
editorTitle="JSON"
|
|
editorLanguage="json"
|
|
resultTitle="Zod Schema"
|
|
resultLanguage={"typescript"}
|
|
editorSettingsElement={getSettingsElement}
|
|
settings={settings}
|
|
/>
|
|
);
|
|
}
|