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-7808: fixed an issue where conditional editGrid loses the value after submission #5511

Merged
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
1 change: 0 additions & 1 deletion src/Webform.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ describe('Webform tests', function() {
Formio.makeRequest = function() {
return new Promise((res, rej) => {
setTimeout(() => {
console.log(8888);
rej(errorText);
}, 50);
});
Expand Down
9 changes: 6 additions & 3 deletions src/components/editgrid/EditGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ export default class EditGridComponent extends NestedArrayComponent {
}

changeState(changed, flags) {
if (changed || (flags.resetValue && this.component.modalEdit)) {
if (this.visible && (changed || (flags.resetValue && this.component.modalEdit))) {
this.rebuild();
}
else {
Expand All @@ -1308,7 +1308,7 @@ export default class EditGridComponent extends NestedArrayComponent {

const changed = this.hasChanged(value, this.dataValue);
flags.noValidate = !changed;
if (this.parent && !(this.options.server && !this.parent.parentVisible)) {
if (this.parent && !this.options.server) {
this.parent.checkComponentConditions();
}
this.dataValue = value;
Expand Down Expand Up @@ -1343,7 +1343,10 @@ export default class EditGridComponent extends NestedArrayComponent {

this.openWhenEmpty();
this.updateOnChange(flags, changed);
this.checkData();
// do not call checkData with server option, it is called when change is triggered in updateOnChange
if (!this.options.server) {
this.checkData();
}

this.changeState(changed, flags);

Expand Down
142 changes: 142 additions & 0 deletions src/components/editgrid/EditGrid.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
compOpenWhenEmpty,
compWithCustomDefaultValue,
} from './fixtures';
import formsWithEditGridAndConditions from './fixtures/formsWithEditGridAndConditions';

import ModalEditGrid from '../../../test/forms/modalEditGrid';
import EditGridOpenWhenEmpty from '../../../test/forms/editGridOpenWhenEmpty';
Expand Down Expand Up @@ -1246,6 +1247,147 @@ describe('EditGrid Component', () => {
}, 100);
}).catch(done);
});

it('Should keep value for conditional editGrid on setValue when server option is provided', (done) => {
const element = document.createElement('div');

Formio.createForm(element, formsWithEditGridAndConditions.form1, { server: true }).then(form => {
const formData = {
checkbox: true,
radio: 'yes',
editGrid: [
{ textField: 'test', number: 4 },
{ textField: 'test1', number: 5 },
],
};

form.setValue({ data: _.cloneDeep(formData) });

setTimeout(() => {
const editGrid = form.getComponent('editGrid');
assert.deepEqual(editGrid.dataValue, formData.editGrid);

done();
}, 500);
}).catch(done);
});

it('Should set value for conditional editGrid inside editGrid on event when form is not pristine ', (done) => {
const element = document.createElement('div');

Formio.createForm(element, formsWithEditGridAndConditions.form2).then(form => {
form.setPristine(false);
const editGrid1 = form.getComponent('editGrid1');
editGrid1.addRow();

setTimeout(() => {
const btn = editGrid1.getComponent('setPanelValue')[0];
const clickEvent = new Event('click');
btn.refs.button.dispatchEvent(clickEvent);
setTimeout(() => {
const conditionalEditGrid = editGrid1.getComponent('editGrid')[0];
assert.deepEqual(conditionalEditGrid.dataValue, [{ textField:'testyyyy' }]);
assert.equal(conditionalEditGrid.editRows.length, 1);
done();
}, 500);
}, 300);
}).catch(done);
});

it('Should keep value for conditional editGrid in tabs on setValue when server option is provided', (done) => {
const element = document.createElement('div');

Formio.createForm(element, formsWithEditGridAndConditions.form3, { server: true }).then(form => {
const formData = {
affectedRiskTypes: {
creditRisk: false,
marketRisk: true,
operationalRisk: false,
counterpartyCreditRisk: false,
creditValuationRiskAdjustment: false,
},
rwaImpact: 'yes',
submit: true,
mr: {
quantitativeInformation: {
cva: 'yes',
sameRiskCategories: false,
impactsPerEntity: [{ number: 123 }],
sameImpactAcrossEntities: false,
},
},
euParentInstitution: 'EUParent',
};

form.setValue({ data: _.cloneDeep(formData) });

setTimeout(() => {
const editGrid = form.getComponent('impactsPerEntity');
assert.deepEqual(editGrid.dataValue, formData.mr.quantitativeInformation.impactsPerEntity);
assert.deepEqual(editGrid.editRows.length, 1);

done();
}, 500);
}).catch(done);
});

it('Should calculate editGrid value when calculateOnServer is enabled and server option is passed', (done) => {
const element = document.createElement('div');

Formio.createForm(element, formsWithEditGridAndConditions.form4, { server: true }).then(form => {
const editGrid = form.getComponent('editGrid');
assert.deepEqual(editGrid.dataValue, [{ textArea: 'test' }]);
assert.deepEqual(editGrid.editRows.length, 1);
done();
}).catch(done);
});

it('Should keep value for conditional editGrid deeply nested in panels and containers on setValue when server option is provided', (done) => {
const element = document.createElement('div');

Formio.createForm(element, formsWithEditGridAndConditions.form5, { server: true }).then(form => {
const formData = {
generalInformation: {
listSupervisedEntitiesCovered: [
{ id: 6256, longName: 'Bank_DE', leiCode: 'LEI6256', countryCode: 'DE' },
],
deSpecific: {
criticalPartsToBeOutsourcedSuboutsourcer: 'yes',
suboutsourcers: [
{ nameSuboutsourcer: 'test' },
{ nameSuboutsourcer: 'test 1' },
],
},
},
};

form.setValue({ data: _.cloneDeep(formData) });

setTimeout(() => {
const editGrid = form.getComponent('suboutsourcers');
assert.deepEqual(editGrid.dataValue, formData.generalInformation.deSpecific.suboutsourcers);
assert.deepEqual(editGrid.editRows.length, 2);

done();
}, 500);
}).catch(done);
});

it('Should calculate editGrid value when condition is met in advanced logic', (done) => {
const element = document.createElement('div');

Formio.createForm(element, formsWithEditGridAndConditions.form6).then(form => {
form.getComponent('textField').setValue('show');

setTimeout(() => {
const editGrid = form.getComponent('editGrid');
assert.deepEqual(editGrid.dataValue, [{ number:1, textArea: 'test' }, { number:2, textArea: 'test2' }]);
assert.deepEqual(editGrid.editRows.length, 2);

done();
}, 300);
}).catch(done);
});
});

describe('EditGrid Open when Empty', () => {
Expand Down
Loading
Loading