1
1
import React , { useEffect , useRef } from "react" ;
2
2
import styled , { css } from "styled-components" ;
3
3
4
- import { JSONEditor as Editor } from "vanilla-jsoneditor" ;
4
+ import { createJSONEditor , type JSONEditorPropsOptional , JsonEditor } from "vanilla-jsoneditor" ;
5
5
6
6
import { landscapeStyle } from "styles/landscapeStyle" ;
7
7
@@ -35,14 +35,13 @@ const Container = styled.div`
35
35
36
36
const JSONEditor = ( props : any ) => {
37
37
const refContainer = useRef < HTMLDivElement | null > ( null ) ;
38
- const refEditor = useRef < Editor | null > ( null ) ;
38
+ const refEditor = useRef < JsonEditor | null > ( null ) ;
39
+ const refPrevProps = useRef < JSONEditorPropsOptional > ( props ) ;
39
40
40
41
useEffect ( ( ) => {
41
- refEditor . current = new Editor ( {
42
- target : refContainer . current ! ,
43
- props : {
44
- ...props ,
45
- } ,
42
+ refEditor . current = createJSONEditor ( {
43
+ target : refContainer . current as HTMLDivElement ,
44
+ props,
46
45
} ) ;
47
46
48
47
return ( ) => {
@@ -53,12 +52,25 @@ const JSONEditor = (props: any) => {
53
52
} ;
54
53
} , [ ] ) ;
55
54
55
+ // update props
56
56
useEffect ( ( ) => {
57
57
if ( refEditor . current ) {
58
- refEditor . current . updateProps ( props ) ;
58
+ const changedProps = filterUnchangedProps ( props , refPrevProps . current ) ;
59
+ refEditor . current . updateProps ( changedProps ) ;
60
+ refPrevProps . current = props ;
59
61
}
60
62
} , [ props ] ) ;
61
63
62
64
return < Container ref = { refContainer } className = { props . className } > </ Container > ;
63
65
} ;
66
+
67
+ function filterUnchangedProps (
68
+ props : JSONEditorPropsOptional ,
69
+ prevProps : JSONEditorPropsOptional
70
+ ) : JSONEditorPropsOptional {
71
+ return Object . fromEntries (
72
+ Object . entries ( props ) . filter ( ( [ key , value ] ) => value !== prevProps [ key as keyof JSONEditorPropsOptional ] )
73
+ ) ;
74
+ }
75
+
64
76
export default JSONEditor ;
0 commit comments