diff --git a/lib/components/src/controls/Object.tsx b/lib/components/src/controls/Object.tsx index e2f623676a4..77b5a5f2d0a 100644 --- a/lib/components/src/controls/Object.tsx +++ b/lib/components/src/controls/Object.tsx @@ -207,38 +207,43 @@ export const ObjectControl: React.FC = ({ name, value = {}, onChang }, [onChange] ); + const rawJSONForm = ( + updateRaw(event.target.value)} + size="flex" + placeholder="Enter JSON string" + valid={parseError ? 'error' : null} + /> + ); return ( - Cancel} - editButtonElement={} - addButtonElement={ - - } - plusMenuElement={} - minusMenuElement={} - inputElement={(_: any, __: any, ___: any, key: string) => - key ? : - } - fallback={ - updateRaw(event.target.value)} - size="flex" - placeholder="Enter JSON string" - valid={parseError ? 'error' : null} - /> - } - /> + {data ? ( + Cancel} + editButtonElement={} + addButtonElement={ + + } + plusMenuElement={} + minusMenuElement={} + inputElement={(_: any, __: any, ___: any, key: string) => + key ? : + } + fallback={rawJSONForm} + /> + ) : ( + rawJSONForm + )} ); }; diff --git a/lib/components/src/controls/react-editable-json-tree/components/JsonArray.js b/lib/components/src/controls/react-editable-json-tree/components/JsonArray.js index 5adf6544f60..61bf8240aa2 100644 --- a/lib/components/src/controls/react-editable-json-tree/components/JsonArray.js +++ b/lib/components/src/controls/react-editable-json-tree/components/JsonArray.js @@ -166,7 +166,6 @@ class JsonArray extends Component { const { handleRemove, readOnly, getStyle, dataType, minusMenuElement } = this.props; const { minus, collapsed } = getStyle(name, data, keyPath, deep, dataType); - const isRoot = deep === -1; const isReadOnly = readOnly(name, data, keyPath, deep, dataType); const removeItemButton = React.cloneElement(minusMenuElement, { @@ -181,7 +180,7 @@ class JsonArray extends Component { [...] {data.length} {data.length === 1 ? 'item' : 'items'} - {!isRoot && !isReadOnly && removeItemButton} + {!isReadOnly && removeItemButton} ); /* eslint-enable */ @@ -210,7 +209,7 @@ class JsonArray extends Component { onSubmitValueParser, } = this.props; const { minus, plus, delimiter, ul, addForm } = getStyle(name, data, keyPath, deep, dataType); - const isRoot = deep === -1; + const isReadOnly = readOnly(name, data, keyPath, deep, dataType); const addItemButton = React.cloneElement(plusMenuElement, { @@ -281,7 +280,7 @@ class JsonArray extends Component { {endObject} - {!isReadOnly && !isRoot && removeItemButton} + {!isReadOnly && removeItemButton} ); } diff --git a/lib/components/src/controls/react-editable-json-tree/components/JsonObject.js b/lib/components/src/controls/react-editable-json-tree/components/JsonObject.js index 54ac473db4a..7e3c298e97d 100644 --- a/lib/components/src/controls/react-editable-json-tree/components/JsonObject.js +++ b/lib/components/src/controls/react-editable-json-tree/components/JsonObject.js @@ -167,7 +167,6 @@ class JsonObject extends Component { const { minus, collapsed } = getStyle(name, data, keyPath, deep, dataType); const keyList = Object.getOwnPropertyNames(data); - const isRoot = deep === -1; const isReadOnly = readOnly(name, data, keyPath, deep, dataType); const removeItemButton = React.cloneElement(minusMenuElement, { @@ -182,7 +181,7 @@ class JsonObject extends Component { {'{...}'} {keyList.length} {keyList.length === 1 ? 'key' : 'keys'} - {!isRoot && !isReadOnly && removeItemButton} + {!isReadOnly && removeItemButton} ); /* eslint-enable */ @@ -214,7 +213,6 @@ class JsonObject extends Component { const { minus, plus, addForm, ul, delimiter } = getStyle(name, data, keyPath, deep, dataType); const keyList = Object.getOwnPropertyNames(data); - const isRoot = deep === -1; const isReadOnly = readOnly(name, data, keyPath, deep, dataType); const addItemButton = React.cloneElement(plusMenuElement, { @@ -286,7 +284,7 @@ class JsonObject extends Component { {endObject} - {!isRoot && !isReadOnly && removeItemButton} + {!isReadOnly && removeItemButton} ); } diff --git a/lib/components/src/controls/react-editable-json-tree/components/JsonValue.js b/lib/components/src/controls/react-editable-json-tree/components/JsonValue.js index 25db639e83c..f2bf4dcd056 100644 --- a/lib/components/src/controls/react-editable-json-tree/components/JsonValue.js +++ b/lib/components/src/controls/react-editable-json-tree/components/JsonValue.js @@ -32,9 +32,9 @@ class JsonValue extends Component { componentDidUpdate() { const { editEnabled, inputRef, name, value, keyPath, deep } = this.state; const { readOnly, dataType } = this.props; - const readOnlyResult = readOnly(name, value, keyPath, deep, dataType); + const isReadOnly = readOnly(name, value, keyPath, deep, dataType); - if (editEnabled && !readOnlyResult && typeof inputRef.focus === 'function') { + if (editEnabled && !isReadOnly && typeof inputRef.focus === 'function') { inputRef.focus(); } } @@ -114,65 +114,54 @@ class JsonValue extends Component { } = this.props; const style = getStyle(name, originalValue, keyPath, deep, dataType); - let result = null; - let minusElement = null; - const readOnlyResult = readOnly(name, originalValue, keyPath, deep, dataType); + const isReadOnly = readOnly(name, originalValue, keyPath, deep, dataType); + const isEditing = editEnabled && !isReadOnly; + const inputElement = inputElementGenerator( + inputUsageTypes.VALUE, + comeFromKeyPath, + deep, + name, + originalValue, + dataType + ); - if (editEnabled && !readOnlyResult) { - const inputElement = inputElementGenerator( - inputUsageTypes.VALUE, - comeFromKeyPath, - deep, - name, - originalValue, - dataType - ); - - const editButtonElementLayout = React.cloneElement(editButtonElement, { - onClick: this.handleEdit, - }); - const cancelButtonElementLayout = React.cloneElement(cancelButtonElement, { - onClick: this.handleCancelEdit, - }); - const inputElementLayout = React.cloneElement(inputElement, { - ref: this.refInput, - defaultValue: JSON.stringify(originalValue), - }); - - result = ( - - {inputElementLayout} {cancelButtonElementLayout} - {editButtonElementLayout} - - ); - minusElement = null; - } else { - /* eslint-disable jsx-a11y/no-static-element-interactions */ - result = ( - - {String(value)} - - ); - /* eslint-enable */ - const minusMenuLayout = React.cloneElement(minusMenuElement, { - onClick: handleRemove, - className: 'rejt-minus-menu', - style: style.minus, - }); - minusElement = readOnlyResult ? null : minusMenuLayout; - } + const editButtonElementLayout = React.cloneElement(editButtonElement, { + onClick: this.handleEdit, + }); + const cancelButtonElementLayout = React.cloneElement(cancelButtonElement, { + onClick: this.handleCancelEdit, + }); + const inputElementLayout = React.cloneElement(inputElement, { + ref: this.refInput, + defaultValue: JSON.stringify(originalValue), + }); + const minusMenuLayout = React.cloneElement(minusMenuElement, { + onClick: handleRemove, + className: 'rejt-minus-menu', + style: style.minus, + }); return (
  • - {name} :{' '} + {name} + {' : '} - {result} - {minusElement} + {isEditing ? ( + + {inputElementLayout} {cancelButtonElementLayout} + {editButtonElementLayout} + + ) : ( + + {String(value)} + + )} + {!isReadOnly && !isEditing && minusMenuLayout}
  • ); } diff --git a/lib/components/src/controls/react-editable-json-tree/index.js b/lib/components/src/controls/react-editable-json-tree/index.js index 9325e05f89b..7e77c0b2f00 100644 --- a/lib/components/src/controls/react-editable-json-tree/index.js +++ b/lib/components/src/controls/react-editable-json-tree/index.js @@ -18,6 +18,7 @@ class JsonTree extends Component { }; // Bind this.onUpdate = this.onUpdate.bind(this); + this.removeRoot = this.removeRoot.bind(this); } static getDerivedStateFromProps(props, state) { @@ -35,6 +36,10 @@ class JsonTree extends Component { this.props.onFullyUpdate(data); } + removeRoot() { + this.onUpdate(null, null); + } + render() { const { data, rootName } = this.state; const { @@ -59,7 +64,7 @@ class JsonTree extends Component { // Node type const dataType = getObjectType(data); - let node = null; + let readOnlyFunction = readOnly; if (getObjectType(readOnly) === 'Boolean') { readOnlyFunction = () => readOnly; @@ -93,6 +98,7 @@ class JsonTree extends Component { textareaElementGenerator={textareaElementFunction} minusMenuElement={minusMenuElement} plusMenuElement={plusMenuElement} + handleRemove={this.removeRoot} beforeRemoveAction={beforeRemoveAction} beforeAddAction={beforeAddAction} beforeUpdateAction={beforeUpdateAction} diff --git a/lib/router/src/utils.ts b/lib/router/src/utils.ts index 8c810ccbbdf..b4f463ba7ea 100644 --- a/lib/router/src/utils.ts +++ b/lib/router/src/utils.ts @@ -41,7 +41,7 @@ const deepDiff = (value: any, update: any): any => { if (deepEqual(value, update)) return undefined; if (typeof value !== typeof update) return update; if (Array.isArray(value)) return update; - if (typeof update === 'object') { + if (update && typeof update === 'object') { return Object.keys(update).reduce((acc, key) => { const diff = deepDiff(value[key], update[key]); return diff === undefined ? acc : Object.assign(acc, { [key]: diff });