From d11c5c718c9e6d3b0d88f0eea7b45320b1655381 Mon Sep 17 00:00:00 2001 From: alexandraRamanenka <60643585+alexandraRamanenka@users.noreply.github.com> Date: Tue, 14 Nov 2023 16:59:39 +0200 Subject: [PATCH] FIO-7547: Container hidden with conditional logic still appears in submission (#5401) * FIO-7547: Container hidden with conditional logic still appears in submission * Fixed tests --- .../_classes/nested/NestedComponent.js | 2 +- src/components/container/Container.unit.js | 22 +++++++++- src/components/container/fixtures/comp4.js | 43 +++++++++++++++++++ src/components/container/fixtures/index.js | 1 + src/components/select/Select.unit.js | 2 +- 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 src/components/container/fixtures/comp4.js diff --git a/src/components/_classes/nested/NestedComponent.js b/src/components/_classes/nested/NestedComponent.js index a2cc226d5d..a70e77712c 100644 --- a/src/components/_classes/nested/NestedComponent.js +++ b/src/components/_classes/nested/NestedComponent.js @@ -616,7 +616,7 @@ export default class NestedComponent extends Field { clearOnHide(show) { super.clearOnHide(show); if (this.component.clearOnHide) { - if (this.allowData && !this.hasValue()) { + if (this.allowData && !this.hasValue() && !(this.options.server && !this.visible)) { this.dataValue = this.defaultValue; } if (this.hasValue()) { diff --git a/src/components/container/Container.unit.js b/src/components/container/Container.unit.js index 8e3dde5c08..fe96056b42 100644 --- a/src/components/container/Container.unit.js +++ b/src/components/container/Container.unit.js @@ -6,7 +6,8 @@ import ContainerComponent from './Container'; import { comp1, comp2, - comp3 + comp3, + comp4, } from './fixtures'; import Formio from '../../Formio'; @@ -77,4 +78,23 @@ describe('Container Component', () => { }, 100); }).catch(done); }); + + it('Should not set the default value when clearOnHide during the server-side validation', (done) => { + const form = _.cloneDeep(comp4); + const element = document.createElement('div'); + + Formio.createForm(element, form, { server: true, noDefaults: true }).then(form => { + form.setValue({ data: { checkbox: false } }, { + sanitize: true, + }, true); + + form.checkConditions(); + form.clearOnHide(); + + setTimeout(() => { + assert.deepEqual(form._data, { checkbox: false }, 'Should not add Container\'s key'); + done(); + }, 200); + }).catch(done); + }); }); diff --git a/src/components/container/fixtures/comp4.js b/src/components/container/fixtures/comp4.js new file mode 100644 index 0000000000..c48b94e359 --- /dev/null +++ b/src/components/container/fixtures/comp4.js @@ -0,0 +1,43 @@ +export default { + type: 'form', + display: 'form', + components: [ + { + label: 'Checkbox', + tableView: false, + key: 'checkbox', + type: 'checkbox', + input: true, + }, + { + label: 'Container', + tableView: false, + key: 'container', + conditional: { + show: true, + when: 'checkbox', + eq: 'true', + }, + type: 'container', + input: true, + components: [ + { + label: 'Text Field', + applyMaskOn: 'change', + tableView: true, + key: 'textField', + type: 'textfield', + input: true, + }, + ], + }, + { + type: 'button', + label: 'Submit', + key: 'submit', + disableOnInvalid: true, + input: true, + tableView: false, + }, + ], +}; diff --git a/src/components/container/fixtures/index.js b/src/components/container/fixtures/index.js index bb8fc410b7..6664cb8954 100644 --- a/src/components/container/fixtures/index.js +++ b/src/components/container/fixtures/index.js @@ -1,3 +1,4 @@ export comp1 from './comp1'; export comp2 from './comp2'; export comp3 from './comp3'; +export comp4 from './comp4'; diff --git a/src/components/select/Select.unit.js b/src/components/select/Select.unit.js index 0f8445f7b4..250afe9143 100644 --- a/src/components/select/Select.unit.js +++ b/src/components/select/Select.unit.js @@ -41,7 +41,7 @@ describe('Select Component', () => { assert.equal(component.dataValue.value, 'a'); assert.equal(typeof component.dataValue , 'object'); done(); - }, 100); + }, 300); }); });