Skip to content

Commit

Permalink
Revert "Update embeddable InputEditor example to use non controlled J…
Browse files Browse the repository at this point in the history
…sonEditor"

This reverts commit 1f9ae41.
  • Loading branch information
sebelga committed Sep 3, 2020
1 parent ba97afd commit 438d9f2
Showing 1 changed file with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,31 @@ import React from 'react';
import { EuiButton } from '@elastic/eui';
import { JsonEditor } from '../../../../src/plugins/es_ui_shared/public';

export const InputEditor = <T extends object>(props: {
input: T;
onSubmit: (value: T) => void;
}) => {
const getJsonData = React.useRef(() => props.input);
const [isValid, setIsValid] = React.useState(true);

export const InputEditor = <T,>(props: { input: T; onSubmit: (value: T) => void }) => {
const input = JSON.stringify(props.input, null, 4);
const [value, setValue] = React.useState(input);
const isValid = (() => {
try {
JSON.parse(value);
return true;
} catch (e) {
return false;
}
})();
React.useEffect(() => {
setValue(input);
}, [input]);
return (
<>
<JsonEditor<T>
defaultValue={props.input}
onUpdate={(jsonState) => {
getJsonData.current = jsonState.data.format;
setIsValid(jsonState.isValid);
}}
<JsonEditor
value={value}
onUpdate={(v) => setValue(v.data.raw)}
euiCodeEditorProps={{
'data-test-subj': 'dashboardEmbeddableByValueInputEditor',
}}
/>
<EuiButton
onClick={() => props.onSubmit(getJsonData.current())}
onClick={() => props.onSubmit(JSON.parse(value))}
disabled={!isValid}
data-test-subj={'dashboardEmbeddableByValueInputSubmit'}
>
Expand Down

0 comments on commit 438d9f2

Please sign in to comment.