Skip to content

Commit

Permalink
Merge branch 'master' into FIO-8281-fix-select-data-for-multiple-sele…
Browse files Browse the repository at this point in the history
…ct-component
  • Loading branch information
brendanbond committed May 23, 2024
2 parents ba5dbfe + 3119734 commit 9e0ba69
Show file tree
Hide file tree
Showing 12 changed files with 362 additions and 22 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ Formio.createForm(document.getElementById('formio'), 'https://examples.form.io/e
- FIO-8091: Fixed missing metadata for html5 select component with default value
- FIO-7445: fixed an issue with interpolated data in HTML
- FIO-7507: publish-dev-tag-to-npm
- FIO-8330: fixed saving draft if saveDraft and skipDraftRestore are true
- FIO-7595: fixed incorrect value for conditionally hidden Checkbox
- FIO-8342: fixed triggering saveDraft after submitting the form
- FIO-8240: fixed skipDraftRestore effect for the nested Forms
- FIO-8360 fixed submission state for nested form


## 5.0.0-rc.37
### Fixed
Expand Down
19 changes: 13 additions & 6 deletions src/Webform.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,19 @@ export default class Webform extends NestedDataComponent {
this.language = this.i18next.language;

// See if we need to restore the draft from a user.
if (this.options.saveDraft && !this.options.skipDraftRestore) {
if (this.options.saveDraft) {
this.formReady.then(()=> {
const user = Formio.getUser();
// Only restore a draft if the submission isn't explicitly set.
if (user && !this.submissionSet) {
this.restoreDraft(user._id);
if (!this.options.skipDraftRestore) {
const user = Formio.getUser();
// Only restore a draft if the submission isn't explicitly set.
if (user && !this.submissionSet) {
this.restoreDraft(user._id);
}
}
else {
// Enable drafts
this.draftEnabled = true;
this.savingDraft = false;
}
});
}
Expand Down Expand Up @@ -860,7 +867,7 @@ export default class Webform extends NestedDataComponent {
const draft = fastCloneDeep(this.submission);
draft.state = 'draft';

if (!this.savingDraft) {
if (!this.savingDraft && !this.submitting) {
this.emit('saveDraftBegin');
this.savingDraft = true;
this.formio.saveSubmission(draft).then((sub) => {
Expand Down
35 changes: 35 additions & 0 deletions src/Webform.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4601,6 +4601,7 @@ describe('Webform tests', function() {
const originalMakeRequest = Formio.makeRequest;
let saveDraftCalls = 0;
let restoreDraftCalls = 0;
let state = null;
const scenario = {
restoreDraftError: false,
saveDraftError: false,
Expand All @@ -4624,6 +4625,11 @@ describe('Webform tests', function() {
? Promise.reject('Save Draft Error')
: Promise.resolve(fastCloneDeep(data));
}
if (type === 'submission' && method === 'post') {
state = data.state;
saveDraftCalls = ++saveDraftCalls;
return Promise.resolve(fastCloneDeep(data));
}
if (type === 'form' && method === 'get') {
return Promise.resolve(fastCloneDeep({
_id: '65cdd69efb1b9683c216fa1d',
Expand Down Expand Up @@ -4707,6 +4713,7 @@ describe('Webform tests', function() {
afterEach(() => {
saveDraftCalls = 0;
restoreDraftCalls = 0;
state = null;
scenario.restoreDraftError = false;
scenario.saveDraftError = false;
});
Expand Down Expand Up @@ -4810,6 +4817,34 @@ describe('Webform tests', function() {
}, 200);
}).catch((err) => done(err));
});

it('Should save the draft after changing the data if skipDraftRestore is set as true', function(done) {
const formElement = document.createElement('div');
Formio.createForm(
formElement,
'http://localhost:3000/zarbzxibjafpcjb/testdrafterrors',
{
saveDraft: true,
skipDraftRestore: true
}
).then((form) => {
setTimeout(() => {
assert.equal(restoreDraftCalls, 0, 'Should not restore Draft');
assert.equal(saveDraftCalls, 0);
assert.equal(_.isUndefined(form.submission.state), true);
const tfInput = form.getComponent('textField').refs.input[0];
tfInput.value = 'test';
const inputEvent = new Event('input');
tfInput.dispatchEvent(inputEvent);
setTimeout(() => {
assert.equal(restoreDraftCalls, 0);
assert.equal(saveDraftCalls, 1, 'Should save Draft');
assert.equal(state, 'draft');
done();
}, 300);
},200);
}).catch((err) => done(err));
});
});

for (const formTest of FormTests) {
Expand Down
13 changes: 2 additions & 11 deletions src/components/checkbox/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 25 additions & 1 deletion src/components/checkbox/Checkbox.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
customDefaultComponent,
comp2,
comp3,
comp4
comp4,
comp5
} from './fixtures';

describe('Checkbox Component', () => {
Expand Down Expand Up @@ -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));
});
});
53 changes: 53 additions & 0 deletions src/components/checkbox/fixtures/comp5.js
Original file line number Diff line number Diff line change
@@ -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
},
]
};
3 changes: 2 additions & 1 deletion src/components/checkbox/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
6 changes: 5 additions & 1 deletion src/components/form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ export default class FormComponent extends Component {
if (this.options.saveDraftThrottle) {
options.saveDraftThrottle = this.options.saveDraftThrottle;
}
if (this.options.skipDraftRestore) {
options.skipDraftRestore = this.options.skipDraftRestore;
}
return options;
}
/* eslint-enable max-statements */
Expand Down Expand Up @@ -649,9 +652,10 @@ export default class FormComponent extends Component {
}

const isAlreadySubmitted = submission && submission._id && submission.form;
const isDraftSubmission = this.options.saveDraft && submission.state === 'draft';

// This submission has already been submitted, so just return the reference data.
if (isAlreadySubmitted && !this.subForm?.wizard) {
if (isAlreadySubmitted && !this.subForm?.wizard && !isDraftSubmission) {
this.dataValue = submission;
return Promise.resolve(this.dataValue);
}
Expand Down
Loading

0 comments on commit 9e0ba69

Please sign in to comment.