diff --git a/packages/strapi-plugin-content-manager/admin/src/components/InputJSON/index.js b/packages/strapi-plugin-content-manager/admin/src/components/InputJSON/index.js index f03f3d49bf..5f492f2909 100644 --- a/packages/strapi-plugin-content-manager/admin/src/components/InputJSON/index.js +++ b/packages/strapi-plugin-content-manager/admin/src/components/InputJSON/index.js @@ -29,7 +29,7 @@ class InputJSON extends React.Component { constructor(props) { super(props); this.editor = React.createRef(); - this.state = { error: false, markedText: null, hasInitValue: false }; + this.state = { error: false, markedText: null }; } componentDidMount() { @@ -54,7 +54,7 @@ class InputJSON extends React.Component { } componentDidUpdate(prevProps) { - if (prevProps.value !== this.props.value && !this.state.hasInitValue) { + if (prevProps.value !== this.props.value && !this.codeMirror.state.focused) { this.setInitValue(); } } @@ -65,9 +65,9 @@ class InputJSON extends React.Component { try { if (value === null) return this.codeMirror.setValue(''); - this.setState({ hasInitValue: true }); + const nextValue = typeof value !== 'string' ? stringify(value, null, 2) : value; - return this.codeMirror.setValue(stringify(value, null, 2)); + return this.codeMirror.setValue(nextValue); } catch (err) { return this.setState({ error: true }); } @@ -119,8 +119,6 @@ class InputJSON extends React.Component { return; } - this.setState({ hasInitValue: true }); - const { name, onChange } = this.props; let value = doc.getValue();