diff --git a/src/editors/array.js b/src/editors/array.js index d3fcda07..93b1307e 100644 --- a/src/editors/array.js +++ b/src/editors/array.js @@ -339,13 +339,9 @@ export class ArrayEditor extends AbstractEditor { }) } - setValue (value = [], initial) { + ensureArraySize (value) { if (!(Array.isArray(value))) value = [value] - const serialized = JSON.stringify(value) - if (serialized === this.serialized) return - - /* Make sure value has between minItems and maxItems items in it */ if (this.schema.minItems) { while (value.length < this.schema.minItems) { value.push(this.getItemInfo(value.length).default) @@ -354,6 +350,15 @@ export class ArrayEditor extends AbstractEditor { if (this.getMax() && value.length > this.getMax()) { value = value.slice(0, this.getMax()) } + return value + } + + setValue (value = [], initial) { + /* Make sure value has between minItems and maxItems items in it */ + value = this.ensureArraySize(value) + + const serialized = JSON.stringify(value) + if (serialized === this.serialized) return value.forEach((val, i) => { if (this.rows[i]) { @@ -471,6 +476,7 @@ export class ArrayEditor extends AbstractEditor { this.controls.style.display = 'none' } } + this.serialized = JSON.stringify(this.value) } addRow (value, initial) { diff --git a/src/editors/table.js b/src/editors/table.js index 76e6fc6f..04901403 100644 --- a/src/editors/table.js +++ b/src/editors/table.js @@ -158,8 +158,9 @@ export class TableEditor extends ArrayEditor { super.destroy() } - setValue (value = [], initial) { - /* Make sure value has between minItems and maxItems items in it */ + ensureArraySize (value) { + if (!(Array.isArray(value))) value = [value] + if (this.schema.minItems) { while (value.length < this.schema.minItems) { value.push(this.getItemDefault()) @@ -168,6 +169,12 @@ export class TableEditor extends ArrayEditor { if (this.schema.maxItems && value.length > this.schema.maxItems) { value = value.slice(0, this.schema.maxItems) } + return value + } + + setValue (value = [], initial) { + /* Make sure value has between minItems and maxItems items in it */ + value = this.ensureArraySize(value) const serialized = JSON.stringify(value) if (serialized === this.serialized) return diff --git a/tests/unit/editor.spec.js b/tests/unit/editor.spec.js index e2d67208..275fb836 100644 --- a/tests/unit/editor.spec.js +++ b/tests/unit/editor.spec.js @@ -76,6 +76,32 @@ describe('Editor', () => { }) expect(JSON.stringify(editor.getValue())).toBe('[1,2,3,4,5]') }) + + it('oneOf Editor Test', () => { + editor = new JSONEditor(element, { + schema: { + type: 'object', + properties: { + one_or_many: { + oneOf: [ + { + type: 'string' + }, + { + type: 'array', + format: 'table', + items: { + type: 'string' + } + } + ] + } + } + } + }) + const e = editor.getEditor('root.one_or_many') + e.switchEditor(1) + }) }) const fixture = [