@ukhomeoffice/cop-react-form-renderer 6.16.1 → 6.16.2-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -61,6 +61,22 @@ const onPageAction = (action, patch, patchLabel, hooks, data, formState, validat
|
|
|
61
61
|
setSubmitting(false);
|
|
62
62
|
} else {
|
|
63
63
|
let pageUpdate = next => onPageChange(_helpers.default.getNextPageId(type, pages, pageId, action, next));
|
|
64
|
+
let collectionName;
|
|
65
|
+
let addAnotherName;
|
|
66
|
+
if (action.collection) {
|
|
67
|
+
collectionName = action.collection;
|
|
68
|
+
const collectionNameCapitalised = collectionName.charAt(0).toUpperCase() + collectionName.slice(1);
|
|
69
|
+
addAnotherName = "addAnother".concat(collectionNameCapitalised).slice(0, -1);
|
|
70
|
+
}
|
|
71
|
+
let collectionArray;
|
|
72
|
+
let index;
|
|
73
|
+
if (collectionName) {
|
|
74
|
+
collectionArray = form.page.formData[collectionName];
|
|
75
|
+
if (collectionArray && collectionArray.length > 0 && form.page.formData["".concat(collectionName, "ActiveId")]) {
|
|
76
|
+
index = collectionArray.findIndex(obj => obj.id === form.page.formData["".concat(collectionName, "ActiveId")]);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
64
80
|
/* eslint-disable no-case-declarations */
|
|
65
81
|
switch (action.type) {
|
|
66
82
|
case _models.PageAction.TYPES.SUBMIT:
|
|
@@ -68,10 +84,37 @@ const onPageAction = (action, patch, patchLabel, hooks, data, formState, validat
|
|
|
68
84
|
break;
|
|
69
85
|
case _models.PageAction.TYPES.SAVE_AND_NAVIGATE:
|
|
70
86
|
const state = _objectSpread({}, currentTask);
|
|
87
|
+
// If resetActiveId is true then point active ID to first entry in collection
|
|
88
|
+
if (collectionArray && collectionArray.length > 0 && action.resetActiveId) {
|
|
89
|
+
form.page.formData["".concat(collectionName, "ActiveId")] = collectionArray[0].id;
|
|
90
|
+
}
|
|
91
|
+
// If resetActiveId is false
|
|
92
|
+
if (collectionArray && collectionArray.length > 0 && !action.resetActiveId) {
|
|
93
|
+
// Truncate collection if user has selected no to more entries
|
|
94
|
+
if (index !== undefined && action.truncateCollection && form.page.formData[addAnotherName] === 'No') {
|
|
95
|
+
form.page.formData[collectionName] = collectionArray.filter((_data, idx) => idx <= index);
|
|
96
|
+
}
|
|
97
|
+
// Set addAnother field to yes if not reached last entry in collection
|
|
98
|
+
if (index !== undefined && index + 1 < form.page.formData[collectionName].length) {
|
|
99
|
+
form.page.formData[addAnotherName] = 'Yes';
|
|
100
|
+
}
|
|
101
|
+
// Set addAnother field to no if reached last entry
|
|
102
|
+
if (index !== undefined && index + 1 === form.page.formData[collectionName].length && form.page.formData[addAnotherName]) {
|
|
103
|
+
form.page.formData[addAnotherName] = 'No';
|
|
104
|
+
}
|
|
105
|
+
}
|
|
71
106
|
pageUpdate = () => _handlers.default.navigate(action, pageId, onPageChange, state);
|
|
72
107
|
break;
|
|
73
108
|
case _models.PageAction.TYPES.COLLECTION_ADD:
|
|
74
|
-
|
|
109
|
+
// Only add entry if the index is undefined,
|
|
110
|
+
// the array is empty
|
|
111
|
+
// or the active ID is the last one in the collection,
|
|
112
|
+
// otherwise move to next entry.
|
|
113
|
+
if (index === undefined || collectionArray.length === 0 || index + 1 === form.page.formData[collectionName].length) {
|
|
114
|
+
_utils.default.CollectionPage.addEntry(action.collection, form.page.formData);
|
|
115
|
+
} else {
|
|
116
|
+
form.page.formData["".concat(action.collection, "ActiveId")] = collectionArray[index + 1].id;
|
|
117
|
+
}
|
|
75
118
|
|
|
76
119
|
// We need to delete the collection entry fields from formData as it will be holding previous values.
|
|
77
120
|
if (form.page.formData["".concat(action.collection)] && form.page.formData["".concat(action.collection)].length > 0) {
|
|
@@ -105,16 +148,13 @@ const onPageAction = (action, patch, patchLabel, hooks, data, formState, validat
|
|
|
105
148
|
removedEntryFieldsToDelete.forEach(i => delete form.page.formData[i]);
|
|
106
149
|
}
|
|
107
150
|
|
|
108
|
-
// 3.
|
|
109
|
-
const collectionArray = form.page.formData[action.collection];
|
|
110
|
-
|
|
111
|
-
// 4. Assign the fields in the last collection entry to formData.
|
|
151
|
+
// 3. Assign the fields in the last collection entry to formData.
|
|
112
152
|
Object.assign(form.page.formData, collectionArray[collectionArray.length - 1]);
|
|
113
153
|
|
|
114
|
-
//
|
|
154
|
+
// 4. Assign the formData ID back to the formData.
|
|
115
155
|
form.page.formData.id = formDataId;
|
|
116
156
|
|
|
117
|
-
//
|
|
157
|
+
// 5. If the collection array has entries set the correct ActiveId
|
|
118
158
|
// otherwise delete it as well as the collection array.
|
|
119
159
|
if (collectionArray.length > 0) {
|
|
120
160
|
form.page.formData["".concat(action.collection, "ActiveId")] = collectionArray[collectionArray.length - 1].id;
|
|
@@ -380,6 +380,74 @@ describe('components.FormRenderer.onPageAction', () => {
|
|
|
380
380
|
// that's called as part of the onSubmit hook's onSuccess callback.
|
|
381
381
|
expect(_handlers.default.navigateCalls).toEqual(1);
|
|
382
382
|
});
|
|
383
|
+
it("should handle the ".concat(_models.PageAction.TYPES.COLLECTION_ADD, " Page Action type where active ID is not the last entry in collection"), () => {
|
|
384
|
+
// If the active ID is not the last entry in the collection
|
|
385
|
+
// then a new entry is not added
|
|
386
|
+
// and the active ID moves to the next entry ID.
|
|
387
|
+
const FORM_STATE = {
|
|
388
|
+
page: {
|
|
389
|
+
formData: {
|
|
390
|
+
'testCollection': [{
|
|
391
|
+
id: '1'
|
|
392
|
+
}, {
|
|
393
|
+
id: '2'
|
|
394
|
+
}],
|
|
395
|
+
testCollectionActiveId: '1'
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
const ACTION = {
|
|
400
|
+
type: _models.PageAction.TYPES.COLLECTION_ADD,
|
|
401
|
+
collection: 'testCollection'
|
|
402
|
+
};
|
|
403
|
+
const CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
|
|
404
|
+
action: ACTION,
|
|
405
|
+
formState: FORM_STATE
|
|
406
|
+
});
|
|
407
|
+
_onPageAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
|
|
408
|
+
preActionChecks();
|
|
409
|
+
expect(FORM_STATE.page.formData["".concat(ACTION.collection, "ActiveId")]).toEqual('2');
|
|
410
|
+
expect(FORM_STATE.page.formData[ACTION.collection].length).toEqual(2);
|
|
411
|
+
postActionChecks(CUSTOM_ARGS);
|
|
412
|
+
|
|
413
|
+
// This action type also sets up a call to handlers.navigate
|
|
414
|
+
// that's called as part of the onSubmit hook's onSuccess callback.
|
|
415
|
+
expect(_handlers.default.navigateCalls).toEqual(1);
|
|
416
|
+
});
|
|
417
|
+
it("should handle the ".concat(_models.PageAction.TYPES.COLLECTION_ADD, " Page Action type where active ID is the last entry in collection"), () => {
|
|
418
|
+
// If the active ID is the last entry in the collection
|
|
419
|
+
// then a new entry is added (although not picked up in this test)
|
|
420
|
+
// and the active ID becomes null.
|
|
421
|
+
const FORM_STATE = {
|
|
422
|
+
page: {
|
|
423
|
+
formData: {
|
|
424
|
+
'testCollection': [{
|
|
425
|
+
id: '1'
|
|
426
|
+
}, {
|
|
427
|
+
id: '2'
|
|
428
|
+
}],
|
|
429
|
+
testCollectionActiveId: '2'
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
const ACTION = {
|
|
434
|
+
type: _models.PageAction.TYPES.COLLECTION_ADD,
|
|
435
|
+
collection: 'testCollection'
|
|
436
|
+
};
|
|
437
|
+
const CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
|
|
438
|
+
action: ACTION,
|
|
439
|
+
formState: FORM_STATE
|
|
440
|
+
});
|
|
441
|
+
_onPageAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
|
|
442
|
+
preActionChecks();
|
|
443
|
+
expect(FORM_STATE.page.formData["".concat(ACTION.collection, "ActiveId")]).toEqual(null);
|
|
444
|
+
expect(FORM_STATE.page.formData[ACTION.collection].length).toEqual(2);
|
|
445
|
+
postActionChecks(CUSTOM_ARGS);
|
|
446
|
+
|
|
447
|
+
// This action type also sets up a call to handlers.navigate
|
|
448
|
+
// that's called as part of the onSubmit hook's onSuccess callback.
|
|
449
|
+
expect(_handlers.default.navigateCalls).toEqual(1);
|
|
450
|
+
});
|
|
383
451
|
it("should clean previous collection entry fields at top level of formData", () => {
|
|
384
452
|
const FORM_STATE = {
|
|
385
453
|
page: {
|
|
@@ -708,6 +776,116 @@ describe('components.FormRenderer.onPageAction', () => {
|
|
|
708
776
|
expect(FORM_STATE.page.formData.parent[0].child[0].id).toEqual('12345');
|
|
709
777
|
});
|
|
710
778
|
});
|
|
779
|
+
it("should work for the ".concat(_models.PageAction.TYPES.SAVE_AND_NAVIGATE, " action type to reset the active ID"), () => {
|
|
780
|
+
// In this test the action includes the resetActiveId field
|
|
781
|
+
// requiring that the active ID is pointed to the first entry in the collection
|
|
782
|
+
// so that user can cycle through the full collection again.
|
|
783
|
+
const FORM_STATE = {
|
|
784
|
+
page: {
|
|
785
|
+
formData: {
|
|
786
|
+
testCollection: [{
|
|
787
|
+
id: '1'
|
|
788
|
+
}, {
|
|
789
|
+
id: '2'
|
|
790
|
+
}, {
|
|
791
|
+
id: '3'
|
|
792
|
+
}],
|
|
793
|
+
testCollectionActiveId: '3'
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
const ACTION = {
|
|
798
|
+
type: _models.PageAction.TYPES.SAVE_AND_NAVIGATE,
|
|
799
|
+
collection: 'testCollection',
|
|
800
|
+
resetActiveId: true
|
|
801
|
+
};
|
|
802
|
+
const CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
|
|
803
|
+
formState: FORM_STATE,
|
|
804
|
+
action: ACTION
|
|
805
|
+
});
|
|
806
|
+
_onPageAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
|
|
807
|
+
preActionChecks();
|
|
808
|
+
// All the actions does in this case is set a function
|
|
809
|
+
// to be called in the onSuccess callback for the onSubmit hook.
|
|
810
|
+
postActionChecks(CUSTOM_ARGS);
|
|
811
|
+
expect(_handlers.default.navigateCalls).toEqual(1);
|
|
812
|
+
expect(FORM_STATE.page.formData["".concat(ACTION.collection, "ActiveId")]).toEqual('1');
|
|
813
|
+
});
|
|
814
|
+
it("should work for the ".concat(_models.PageAction.TYPES.SAVE_AND_NAVIGATE, " action type to truncate the collection"), () => {
|
|
815
|
+
// In this test the action includes the truncateCollection field
|
|
816
|
+
// and if the user has selected no to more entries
|
|
817
|
+
// then all entries after current index (from active ID) are truncated.
|
|
818
|
+
const FORM_STATE = {
|
|
819
|
+
page: {
|
|
820
|
+
formData: {
|
|
821
|
+
tests: [{
|
|
822
|
+
id: '1'
|
|
823
|
+
}, {
|
|
824
|
+
id: '2'
|
|
825
|
+
}, {
|
|
826
|
+
id: '3'
|
|
827
|
+
}],
|
|
828
|
+
testsActiveId: '2',
|
|
829
|
+
addAnotherTest: 'No'
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
};
|
|
833
|
+
const ACTION = {
|
|
834
|
+
type: _models.PageAction.TYPES.SAVE_AND_NAVIGATE,
|
|
835
|
+
collection: 'tests',
|
|
836
|
+
truncateCollection: true
|
|
837
|
+
};
|
|
838
|
+
const CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
|
|
839
|
+
formState: FORM_STATE,
|
|
840
|
+
action: ACTION
|
|
841
|
+
});
|
|
842
|
+
_onPageAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
|
|
843
|
+
preActionChecks();
|
|
844
|
+
// All the actions does in this case is set a function
|
|
845
|
+
// to be called in the onSuccess callback for the onSubmit hook.
|
|
846
|
+
postActionChecks(CUSTOM_ARGS);
|
|
847
|
+
expect(_handlers.default.navigateCalls).toEqual(1);
|
|
848
|
+
expect(FORM_STATE.page.formData["".concat(ACTION.collection, "ActiveId")]).toEqual('2');
|
|
849
|
+
expect(FORM_STATE.page.formData[ACTION.collection].length).toEqual(2);
|
|
850
|
+
expect(FORM_STATE.page.formData['addAnotherTest']).toEqual('No');
|
|
851
|
+
});
|
|
852
|
+
it("should work for the ".concat(_models.PageAction.TYPES.SAVE_AND_NAVIGATE, " action type to set addAnother to yes"), () => {
|
|
853
|
+
// If the user is midway through the collection
|
|
854
|
+
// then the addAnother field is set to yes
|
|
855
|
+
// to allow the navigation to move to next entry.
|
|
856
|
+
const FORM_STATE = {
|
|
857
|
+
page: {
|
|
858
|
+
formData: {
|
|
859
|
+
tests: [{
|
|
860
|
+
id: '1'
|
|
861
|
+
}, {
|
|
862
|
+
id: '2'
|
|
863
|
+
}, {
|
|
864
|
+
id: '3'
|
|
865
|
+
}],
|
|
866
|
+
testsActiveId: '2',
|
|
867
|
+
addAnotherTest: 'No'
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
const ACTION = {
|
|
872
|
+
type: _models.PageAction.TYPES.SAVE_AND_NAVIGATE,
|
|
873
|
+
collection: 'tests'
|
|
874
|
+
};
|
|
875
|
+
const CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
|
|
876
|
+
formState: FORM_STATE,
|
|
877
|
+
action: ACTION
|
|
878
|
+
});
|
|
879
|
+
_onPageAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
|
|
880
|
+
preActionChecks();
|
|
881
|
+
// All the actions does in this case is set a function
|
|
882
|
+
// to be called in the onSuccess callback for the onSubmit hook.
|
|
883
|
+
postActionChecks(CUSTOM_ARGS);
|
|
884
|
+
expect(_handlers.default.navigateCalls).toEqual(1);
|
|
885
|
+
expect(FORM_STATE.page.formData["".concat(ACTION.collection, "ActiveId")]).toEqual('2');
|
|
886
|
+
expect(FORM_STATE.page.formData[ACTION.collection].length).toEqual(3);
|
|
887
|
+
expect(FORM_STATE.page.formData['addAnotherTest']).toEqual('Yes');
|
|
888
|
+
});
|
|
711
889
|
it("should work for the ".concat(_models.PageAction.TYPES.NAVIGATE, " action type"), () => {
|
|
712
890
|
const FORM_STATE = {
|
|
713
891
|
page: {
|
|
@@ -28,6 +28,8 @@ const removeCollectionPageEntry = (collectionName, formData, id) => {
|
|
|
28
28
|
}
|
|
29
29
|
const deletedEntry = collectionData[indexToDelete];
|
|
30
30
|
collectionData.splice(indexToDelete, 1);
|
|
31
|
+
const data = formData;
|
|
32
|
+
data["".concat(collectionName, "ActiveId")] = collectionData.length > 0 ? collectionData[0].id : null;
|
|
31
33
|
return deletedEntry;
|
|
32
34
|
};
|
|
33
35
|
var _default = exports.default = removeCollectionPageEntry;
|