Skip to content

Commit

Permalink
fix issue #521
Browse files Browse the repository at this point in the history
  • Loading branch information
riceyeh committed Sep 19, 2019
1 parent 0b427b7 commit c43fd43
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
25 changes: 14 additions & 11 deletions src/shared/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand All @@ -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);
}
}
});
},
Expand Down
45 changes: 44 additions & 1 deletion tests/fixes.values.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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)
})
});

0 comments on commit c43fd43

Please sign in to comment.