-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathVanillaJSONEditor.tsx
45 lines (39 loc) · 1.3 KB
/
VanillaJSONEditor.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
import React, { useEffect, useRef } from "react";
import 'vanilla-jsoneditor/themes/jse-theme-dark.css'
import { JSONEditor } from "vanilla-jsoneditor";
export const VanillaJSONEditor = (props: any) => {
const refContainer = useRef(null);
const refEditor = useRef(null);
useEffect(() => {
// create editor
console.log("create editor", refContainer.current);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
refEditor.current = new JSONEditor({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
target: refContainer.current,
props: {}
});
return () => {
// destroy editor
if (refEditor.current) {
console.log("destroy editor");
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
refEditor.current.destroy();
refEditor.current = null;
}
};
}, []);
// update props
useEffect(() => {
if (refEditor.current) {
console.log("update props", props);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
refEditor.current.updateProps(props);
}
}, [props]);
return <div style={{height: '100%'}} className="jse-theme-dark" ref={refContainer}></div>;
}