11import React , { useEffect , useRef } from "react" ;
22import styled , { css } from "styled-components" ;
33
4- import { JSONEditor as Editor } from "vanilla-jsoneditor" ;
4+ import { createJSONEditor , type JSONEditorPropsOptional , JsonEditor } from "vanilla-jsoneditor" ;
55
66import { landscapeStyle } from "styles/landscapeStyle" ;
77
@@ -35,14 +35,13 @@ const Container = styled.div`
3535
3636const JSONEditor = ( props : any ) => {
3737 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 ) ;
3940
4041 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,
4645 } ) ;
4746
4847 return ( ) => {
@@ -53,12 +52,25 @@ const JSONEditor = (props: any) => {
5352 } ;
5453 } , [ ] ) ;
5554
55+ // update props
5656 useEffect ( ( ) => {
5757 if ( refEditor . current ) {
58- refEditor . current . updateProps ( props ) ;
58+ const changedProps = filterUnchangedProps ( props , refPrevProps . current ) ;
59+ refEditor . current . updateProps ( changedProps ) ;
60+ refPrevProps . current = props ;
5961 }
6062 } , [ props ] ) ;
6163
6264 return < Container ref = { refContainer } className = { props . className } > </ Container > ;
6365} ;
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+
6476export default JSONEditor ;
0 commit comments