Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIO-8510: changes in nested forms trigger modified change in the parent #5650

Merged
merged 8 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"args": [
"-b",
"-t",
"20000",
"0",
"--colors",
"${workspaceFolder}/src/**/*.unit.js",
"${workspaceFolder}/test/**/*.unit.js",
"-t",
"30000"
],
Expand Down
13 changes: 9 additions & 4 deletions src/Webform.js
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,12 @@ export default class Webform extends NestedDataComponent {
onChange(flags, changed, modified, changes) {
flags = flags || {};
let isChangeEventEmitted = false;
super.onChange(flags, true);
if (this.parent?.subForm === this) {
super.onChange({ ...flags, modified }, false);
}
else {
super.onChange(flags, true);
}
const value = _.clone(this.submission);
flags.changed = value.changed = changed;
flags.changes = changes;
Expand All @@ -1438,9 +1443,9 @@ export default class Webform extends NestedDataComponent {
flags.fromIframe ||
(flags.fromSubmission && this.rootPristine && this.pristine && flags.changed);
const errors = shouldValidate
? this.validate(value.data, {
...flags,
noValidate: false,
? this.validate(value.data, {
...flags,
noValidate: false,
process: 'change'
})
: [];
Expand Down
4 changes: 2 additions & 2 deletions test/forms/helpers/testBasicComponentSettings/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ export default {
assert.deepEqual(basis, basisComponentNewValue, 'Should set basis component value');
checkCalculatedValue();
done();
}, 250);
}, 250);
}, 300);
}, 300);
},
'Should not allow overriding component colculated value'(form, done) {
const basisComponent = form.getComponent('basis');
Expand Down
46 changes: 37 additions & 9 deletions test/unit/Form.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ describe('Form Component', () => {
assert.equal(make.dataValue, 'honda', 'Should set value');
assert.equal(model.dataValue, '', 'Should refresh and clear value');
done();
}, 300);
}, 300);
}, 300);
}, 400);
}, 400);
}, 400);
})
.catch(done);
});
Expand Down Expand Up @@ -219,7 +219,7 @@ describe('Form Component', () => {
expect(textField).to.not.be.null;
assert.equal(textField.value, 'test');
done();
}, 250);
}, 300);
});
}).catch(done);
});
Expand Down Expand Up @@ -372,7 +372,35 @@ describe('SaveDraft functionality for Nested Form', () => {
done();
});

it('Should save draft for Nested Form', function(done) {
it('Changes to the child form should trigger a `modified` change in the parent', (done) => {
const formElement = document.createElement('div');
Formio.createForm(
formElement,
'http://localhost:3000/idwqwhclwioyqbw/testdraftparent',
{
saveDraft: true
}
).then((form) => {
setTimeout(() => {
form.on('change', ({ changed }, _, modified) => {
if (changed) {
const { instance } = changed;
if (instance instanceof Webform) {
assert(modified === true, 'the modified flag should bubble to the root');
done();
}
}
});
const tfNestedInput = form.getComponent('form.nested')?.refs?.input?.[0];
assert(tfNestedInput !== undefined, 'We have the input element');
tfNestedInput.value = 'testNested';
const inputEvent = new Event('input');
tfNestedInput.dispatchEvent(inputEvent);
}, 600);
}).catch((err) => done(err));
});

it('Should save draft for Nested Form and for the Parent Form', function(done) {
const formElement = document.createElement('div');
Formio.createForm(
formElement,
Expand All @@ -387,11 +415,11 @@ describe('SaveDraft functionality for Nested Form', () => {
const inputEvent = new Event('input');
tfNestedInput.dispatchEvent(inputEvent);
setTimeout(() => {
assert.equal(saveDraftCalls, 1);
assert.equal(saveDraftCalls, 2);
assert.equal(state, 'draft');
done();
}, 1000);
}, 200);
}, 400);
}).catch((err) => done(err));
});

Expand Down Expand Up @@ -476,7 +504,7 @@ describe('SaveDraft functionality for Nested Form', () => {
setTimeout(() => {
assert.equal(form.options.readOnly, true);
done();
}).catch((err) => done(err));
});
});
}).catch((err) => done(err));
});
});
Loading