11// @ts -nocheck
2- import React , { FC , MutableRefObject , useEffect , useRef , useState } from 'react'
2+ import React , { FC , forwardRef , MutableRefObject , useEffect , useImperativeHandle , useRef , useState } from 'react'
33import classNames from 'classnames'
44import JSONEditor from 'jsoneditor'
55import 'jsoneditor/dist/jsoneditor.css'
66import isFunction from 'lodash/isFunction'
77
8- import { defaultProps , Props } from './Editor.types'
8+ import { defaultProps , EditorRefType , Props } from './Editor.types'
99import * as styles from './Editor.styles'
1010import IconShowPassword from '../Icon/components/IconShowPassword'
1111
12- const Editor : FC < Props > = ( props ) => {
12+ const Editor = forwardRef < EditorRefType , Props > ( ( props , ref ) => {
1313 const {
1414 autofocus,
1515 className,
@@ -81,7 +81,6 @@ const Editor: FC<Props> = (props) => {
8181 } , [ heightProp ] )
8282
8383 useEffect ( ( ) => {
84- // setTimeout(() => {
8584 const options = {
8685 mode,
8786 mainMenuBar : false ,
@@ -125,7 +124,6 @@ const Editor: FC<Props> = (props) => {
125124 }
126125
127126 handleEditorRef ( jsonEditor )
128- // }, 1)
129127
130128 return ( ) => {
131129 if ( jsonEditor && jsonEditor . current ) {
@@ -141,6 +139,18 @@ const Editor: FC<Props> = (props) => {
141139 }
142140 } , [ ] )
143141
142+ useImperativeHandle ( ref , ( ) => ( {
143+ setValue : ( value ) => {
144+ if ( typeof json === 'object' ) {
145+ // @ts -ignore
146+ jsonEditor ?. current ?. set ( value )
147+ } else if ( typeof json === 'string' ) {
148+ // @ts -ignore
149+ jsonEditor ?. current ?. setText ( value )
150+ }
151+ } ,
152+ } ) )
153+
144154 return (
145155 < div
146156 { ...rest }
@@ -166,7 +176,7 @@ const Editor: FC<Props> = (props) => {
166176 ) }
167177 </ div >
168178 )
169- }
179+ } )
170180
171181Editor . displayName = 'Editor'
172182Editor . defaultProps = defaultProps
0 commit comments