From 64bc57a65a6402afdea2c8871ac32f9efe8d7093 Mon Sep 17 00:00:00 2001 From: tohosaku Date: Sun, 28 Jun 2020 10:47:49 +0900 Subject: [PATCH] Fix #777 ensure that value is array --- src/editors/array.js | 16 +++++++++++----- src/editors/table.js | 11 +++++++++-- tests/unit/editor.spec.js | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/editors/array.js b/src/editors/array.js index d3fcda07a..93b1307ef 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 76e6fc6f9..049014037 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 e2d67208f..275fb836c 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 = [