File tree Expand file tree Collapse file tree 1 file changed +18
-14
lines changed
examples/dashboard_embeddable_examples/public/by_value Expand file tree Collapse file tree 1 file changed +18
-14
lines changed Original file line number Diff line number Diff line change @@ -21,27 +21,31 @@ import React from 'react';
2121import { EuiButton } from '@elastic/eui' ;
2222import { JsonEditor } from '../../../../src/plugins/es_ui_shared/public' ;
2323
24- export const InputEditor = < T extends object > ( props : {
25- input : T ;
26- onSubmit : ( value : T ) => void ;
27- } ) => {
28- const getJsonData = React . useRef ( ( ) => props . input ) ;
29- const [ isValid , setIsValid ] = React . useState ( true ) ;
30-
24+ export const InputEditor = < T , > ( props : { input : T ; onSubmit : ( value : T ) => void } ) => {
25+ const input = JSON . stringify ( props . input , null , 4 ) ;
26+ const [ value , setValue ] = React . useState ( input ) ;
27+ const isValid = ( ( ) => {
28+ try {
29+ JSON . parse ( value ) ;
30+ return true ;
31+ } catch ( e ) {
32+ return false ;
33+ }
34+ } ) ( ) ;
35+ React . useEffect ( ( ) => {
36+ setValue ( input ) ;
37+ } , [ input ] ) ;
3138 return (
3239 < >
33- < JsonEditor < T >
34- defaultValue = { props . input }
35- onUpdate = { ( jsonState ) => {
36- getJsonData . current = jsonState . data . format ;
37- setIsValid ( jsonState . isValid ) ;
38- } }
40+ < JsonEditor
41+ value = { value }
42+ onUpdate = { ( v ) => setValue ( v . data . raw ) }
3943 euiCodeEditorProps = { {
4044 'data-test-subj' : 'dashboardEmbeddableByValueInputEditor' ,
4145 } }
4246 />
4347 < EuiButton
44- onClick = { ( ) => props . onSubmit ( getJsonData . current ( ) ) }
48+ onClick = { ( ) => props . onSubmit ( JSON . parse ( value ) ) }
4549 disabled = { ! isValid }
4650 data-test-subj = { 'dashboardEmbeddableByValueInputSubmit' }
4751 >
You can’t perform that action at this time.
0 commit comments