Skip to content

Commit

Permalink
Fix jdorn#777 ensure that value is array
Browse files Browse the repository at this point in the history
  • Loading branch information
tohosaku committed Jun 28, 2020
1 parent 426aa63 commit 64bc57a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/editors/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]) {
Expand Down Expand Up @@ -471,6 +476,7 @@ export class ArrayEditor extends AbstractEditor {
this.controls.style.display = 'none'
}
}
this.serialized = JSON.stringify(this.value)
}

addRow (value, initial) {
Expand Down
11 changes: 9 additions & 2 deletions src/editors/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/editor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 64bc57a

Please sign in to comment.