From b3f7e5876b8c50be0f3d079db57e8791d9630535 Mon Sep 17 00:00:00 2001 From: Hanna Kurban Date: Mon, 13 May 2024 15:34:23 +0300 Subject: [PATCH] FIO-7595 fixed incorrect value for conditionally hidden Checkbox --- src/components/checkbox/Checkbox.js | 13 +----- src/components/checkbox/Checkbox.unit.js | 26 ++++++++++- src/components/checkbox/fixtures/comp5.js | 53 +++++++++++++++++++++++ src/components/checkbox/fixtures/index.js | 3 +- 4 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 src/components/checkbox/fixtures/comp5.js diff --git a/src/components/checkbox/Checkbox.js b/src/components/checkbox/Checkbox.js index 457a770938..7532f905da 100644 --- a/src/components/checkbox/Checkbox.js +++ b/src/components/checkbox/Checkbox.js @@ -202,17 +202,8 @@ export default class CheckBoxComponent extends Field { } setValue(value, flags = {}) { - if ( - this.setCheckedState(value) !== undefined || - (!this.input && value !== undefined && (this.visible || this.conditionallyVisible() || !this.component.clearOnHide)) - ) { - const changed = this.updateValue(value, flags); - if (this.isHtmlRenderMode() && flags && flags.fromSubmission && changed) { - this.redraw(); - } - return changed; - } - return false; + this.setCheckedState(value); + return super.setValue(value, flags); } getValueAsString(value) { diff --git a/src/components/checkbox/Checkbox.unit.js b/src/components/checkbox/Checkbox.unit.js index 27fe3e0bba..2428c843d0 100644 --- a/src/components/checkbox/Checkbox.unit.js +++ b/src/components/checkbox/Checkbox.unit.js @@ -10,7 +10,8 @@ import { customDefaultComponent, comp2, comp3, - comp4 + comp4, + comp5 } from './fixtures'; describe('Checkbox Component', () => { @@ -91,4 +92,27 @@ describe('Checkbox Component', () => { }, 300); }).catch((err) => done(err)); }); + + it('Should set the value for the checkbox if it set before the component from checbox`s condition', (done) => { + const form = _.cloneDeep(comp5); + const element = document.createElement('div'); + const data = { + textField: 'test', + checkboxBefore: true, + checkboxAfter: true + }; + Formio.createForm(element, form).then(form => { + form.setValue({ data }, { sanitize: true }); + const checkboxBefore = form.getComponent('checkboxBefore'); + const checkboxAfter = form.getComponent('checkboxAfter'); + setTimeout(() => { + const inputBefore = Harness.testElements(checkboxBefore, 'input[type="checkbox"]', 1)[0]; + assert.equal(inputBefore.checked, true); + const inputAfter = Harness.testElements(checkboxAfter, 'input[type="checkbox"]', 1)[0]; + assert.equal(inputAfter.checked, true); + assert.deepEqual(form.data, data); + done(); + }, 300); + }).catch((err) => done(err)); + }); }); diff --git a/src/components/checkbox/fixtures/comp5.js b/src/components/checkbox/fixtures/comp5.js new file mode 100644 index 0000000000..bbce3af06d --- /dev/null +++ b/src/components/checkbox/fixtures/comp5.js @@ -0,0 +1,53 @@ +export default { + title: '7595', + name: '7595', + path: '7595', + type: 'form', + display: 'form', + components:[ + { + label: 'Checkbox', + tableView: false, + validateWhenHidden: false, + key: 'checkboxBefore', + conditional: { + show: true, + conjunction: 'all', + conditions: [ + { + component: 'textField', + operator: 'isNotEmpty' + } + ] + }, + type: 'checkbox', + input: true + }, + { + label: 'Text Field', + applyMaskOn: 'change', + tableView: true, + key: 'textField', + type: 'textfield', + input: true + }, + { + label: 'Checkbox', + tableView: false, + validateWhenHidden: false, + key: 'checkboxAfter', + conditional: { + show: true, + conjunction: 'all', + conditions: [ + { + component: 'textField', + operator: 'isNotEmpty' + } + ] + }, + type: 'checkbox', + input: true + }, + ] +}; diff --git a/src/components/checkbox/fixtures/index.js b/src/components/checkbox/fixtures/index.js index 326574b525..6696fe560c 100644 --- a/src/components/checkbox/fixtures/index.js +++ b/src/components/checkbox/fixtures/index.js @@ -3,4 +3,5 @@ import customDefaultComponent from './customDefaultComponent'; import comp2 from './comp2'; import comp3 from './comp3'; import comp4 from './comp4'; -export { comp1, comp2, comp3, comp4, customDefaultComponent }; +import comp5 from './comp5'; +export { comp1, comp2, comp3, comp4, comp5, customDefaultComponent };