From c43fd4373df3e6f1b080096707ef1ae31c3d9f46 Mon Sep 17 00:00:00 2001 From: Rice Yeh Date: Thu, 19 Sep 2019 11:43:43 +0800 Subject: [PATCH] fix issue #521 --- src/shared/Actions.js | 25 +++++++++++++----------- tests/fixes.values.js | 45 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/src/shared/Actions.js b/src/shared/Actions.js index 4c172d0b..c34c7e04 100755 --- a/src/shared/Actions.js +++ b/src/shared/Actions.js @@ -100,12 +100,16 @@ export default { || this.state.form.select(this.path, null, false); - if (!_.isNil($field) && !_.isNil(field)) { + if (!_.isNil($field) && !_.isUndefined(field)) { if (_.isArray($field.values())) { - _.each(utils.getObservableMapValues($field.fields), $f => - $field.fields.delete($f.name)); + let n = _.max(_.map(field.fields, (f, i) => Number(i))) + if (n === undefined) n = -1 // field's value is [] + _.each(utils.getObservableMapValues($field.fields), $f => { + if (Number($f.name) > n) + $field.fields.delete($f.name) + }); } - if (_.isNil(field.fields)) { + if (_.isNull(field) || _.isNil(field.fields)) { $field.$value = parser.parseInput($field.$input, { separated: field }); @@ -119,17 +123,16 @@ export default { // init field into the container field $container.initField($key, $newFieldPath, field, true); } - else { - if (recursion) { + else if (recursion) { + if (_.has(field, 'fields') && !_.isNil(field.fields)) { + // handle nested fields if defined + this.deepUpdate(field.fields, $path); + } + else { // handle nested fields if undefined or null const $fields = parser.pathToFieldsTree(this.state.struct(), $path); this.deepUpdate($fields, $path, false); } - - if (recursion && _.has(field, 'fields') && !_.isNil(field.fields)) { - // handle nested fields if defined - this.deepUpdate(field.fields, $path); - } } }); }, diff --git a/tests/fixes.values.js b/tests/fixes.values.js index 8db3b5d2..62adb583 100755 --- a/tests/fixes.values.js +++ b/tests/fixes.values.js @@ -336,7 +336,7 @@ describe('new form with nested array values', () => { 'trip.itineraryItems[].hotel.name', 'trip.itineraryItems[].hotel.starRating', ]; - + const values = { purpose: 'Summer vacation', trip: { @@ -361,3 +361,46 @@ describe('new form with nested array values', () => { expect($516.$('trip.itineraryItems').size).to.be.equal(3) }) }); + +describe('update to nested array items', () => { + it('', () => { + const fields = [ + 'bulletin', + 'bulletin.jobs', + 'bulletin.jobs[].jobId', + 'bulletin.jobs[].companyName', + ]; + + const values = { + bulletin: { + jobs: [ + { + jobId: 1, + companyName: 'Acer' + }, + { + jobId: 2, + companyName: 'Asus' + }] + } + }; + + const $521 = new Form({fields, values}, {name: 'Form 521'}) + // debugger + $521.update({ + bulletin: { + jobs: [{ + jobId: 1, + companyName: 'Apple' + }] + } + }) + expect($521.$('bulletin.jobs').size).to.be.equal(1) + expect($521.$('bulletin.jobs.0.jobId').value).to.be.equal(1) + expect($521.$('bulletin.jobs.0.jobId').isDirty).to.be.equal(false) + expect($521.$('bulletin.jobs.0.companyName').value).to.be.equal('Apple') + expect($521.$('bulletin.jobs.0.companyName').isDirty).to.be.equal(true) + expect($521.$('bulletin.jobs.0').isDirty).to.be.equal(true) + expect($521.$('bulletin.jobs').isDirty).to.be.equal(true) + }) +});