Skip to content

Commit 438d9f2

Browse files
committed
Revert "Update embeddable InputEditor example to use non controlled JsonEditor"
This reverts commit 1f9ae41.
1 parent ba97afd commit 438d9f2

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

examples/dashboard_embeddable_examples/public/by_value/input_editor.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,31 @@ import React from 'react';
2121
import { EuiButton } from '@elastic/eui';
2222
import { 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
>

0 commit comments

Comments
 (0)