/
html-to-jsx.tsx
101 lines (91 loc) · 2.41 KB
/
html-to-jsx.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as React from "react";
import { useCallback, useState } from "react";
import ConversionPanel, { Transformer } from "@components/ConversionPanel";
import HtmlToJsx from "htmltojsx";
import { EditorPanelProps } from "@components/EditorPanel";
import Form, { InputType } from "@components/Form";
import { useSettings } from "@hooks/useSettings";
import isSvg from "is-svg";
import { Alert, Heading } from "evergreen-ui";
import Router from "next/router";
interface Settings {
createClass: boolean;
outputClassName: string;
}
const formFields = [
{
type: InputType.SWITCH,
key: "createClass",
label: "Create Class"
},
{
type: InputType.TEXT_INPUT,
key: "outputClassName",
label: "Output Class Name"
}
];
export default function() {
const name = "HTML to JSX";
const [settings, setSettings] = useSettings(name, {
createClass: false,
outputClassName: "TransformedComponent"
});
const [_isSvg, setSvg] = useState(false);
const transformer = useCallback<Transformer>(
async ({ value }) => {
setSvg(isSvg(value));
const converter = new HtmlToJsx(settings);
return converter.convert(value);
},
[settings]
);
const getSettingsElement = useCallback<EditorPanelProps["settingElement"]>(
({ open, toggle }) => {
return (
<Form<Settings>
title="HTML to JSX"
onSubmit={setSettings}
open={open}
toggle={toggle}
formsFields={formFields}
initialValues={settings}
/>
);
},
[]
);
return (
<ConversionPanel
transformer={transformer}
editorTitle="HTML"
resultLanguage="javascript"
resultTitle="JSX"
editorLanguage="html"
editorSettingsElement={getSettingsElement}
settings={settings}
editorProps={{
topNotifications: () =>
_isSvg ? (
<Alert
backgroundColor="#e7f7ff"
title={
<>
SVG detected. For preview and optimization, go to{" "}
<Heading
size={400}
is="a"
color={"blue"}
onClick={() => Router.push("/svg-to-jsx")}
>
SVG to JSX converter.
</Heading>
</>
}
/>
) : (
undefined
)
}}
/>
);
}