mirror of
https://github.com/ritz078/transform.git
synced 2026-11-20 16:22:53 +03:00
added type setting for json-to-typescript (#317)
This commit is contained in:
1 parent
1c4ba9628f
commit
0c8fa3bc47
1 file changed
+54
-10
@@ -1,18 +1,60 @@
|
||||
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 {
|
||||
typealias: boolean;
|
||||
}
|
||||
|
||||
const formFields = [
|
||||
{
|
||||
type: InputType.SWITCH,
|
||||
key: "typealias",
|
||||
label: "Create Mono Type"
|
||||
}
|
||||
];
|
||||
|
||||
export default function JsonToTypescript() {
|
||||
const transformer = useCallback(async ({ value }) => {
|
||||
const { run } = await import("json_typegen_wasm");
|
||||
return run(
|
||||
"Root",
|
||||
value,
|
||||
JSON.stringify({
|
||||
output_mode: "typescript"
|
||||
})
|
||||
);
|
||||
}, []);
|
||||
const name = "JSON to Typescript";
|
||||
|
||||
const [settings, setSettings] = useSettings(name, {
|
||||
typealias: false
|
||||
});
|
||||
|
||||
const transformer = useCallback(
|
||||
async ({ value }) => {
|
||||
const { run } = await import("json_typegen_wasm");
|
||||
return run(
|
||||
"Root",
|
||||
value,
|
||||
JSON.stringify({
|
||||
output_mode: settings.typealias
|
||||
? "typescript/typealias"
|
||||
: "typescript"
|
||||
})
|
||||
);
|
||||
},
|
||||
[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
|
||||
@@ -21,6 +63,8 @@ export default function JsonToTypescript() {
|
||||
editorLanguage="json"
|
||||
resultTitle="TypeScript"
|
||||
resultLanguage={"typescript"}
|
||||
editorSettingsElement={getSettingsElement}
|
||||
settings={settings}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user