@ukhomeoffice/cop-react-form-renderer 6.15.2-alpha → 6.15.2

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.
Files changed (47) hide show
  1. package/dist/components/CollectionSummary/BannerStrip.scss +4 -0
  2. package/dist/components/CollectionSummary/CollectionSummary.js +2 -2
  3. package/dist/components/CollectionSummary/SummaryCard.js +2 -2
  4. package/dist/components/FormComponent/Collection.js +2 -2
  5. package/dist/components/FormComponent/Container.js +2 -2
  6. package/dist/components/FormComponent/FormComponent.js +2 -2
  7. package/dist/components/FormPage/FormPage.js +8 -10
  8. package/dist/components/FormRenderer/FormRenderer.js +7 -12
  9. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-component-show-when-in-component-and-page.json +62 -0
  10. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-page-same-component-reused.json +61 -0
  11. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-page-same-component-reused-one-shown.json +74 -0
  12. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/cop-airpax-change-what-happened-before.json +300 -0
  13. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/cop-mandec-remove-unspent-convictions-before.json +143 -0
  14. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-component-show-when-in-component-and-page.json +6 -0
  15. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-page-same-component-reused.json +6 -0
  16. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-page-same-component-reused-one-shown.json +8 -0
  17. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/cop-airpax-change-what-happened-after.json +280 -0
  18. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/cop-mandec-remove-unspent-convictions-after.json +127 -0
  19. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-component-show-when-in-component-and-page-removed.json +5 -0
  20. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-page-same-component-reused-removed.json +5 -0
  21. package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-page-same-component-reused-one-shown-removed.json +7 -0
  22. package/dist/components/FormRenderer/helpers/clearOutUncompletedRoutes.js +259 -282
  23. package/dist/components/FormRenderer/helpers/clearOutUncompletedRoutes.test.js +77 -26
  24. package/dist/components/FormRenderer/helpers/clearOutUncompletedRoutesUtils.js +381 -0
  25. package/dist/components/FormRenderer/helpers/clearOutUncompletedRoutesUtils.test.js +559 -0
  26. package/dist/components/FormRenderer/onCYAAction.js +12 -0
  27. package/dist/components/FormRenderer/onCYAAction.test.js +5 -0
  28. package/dist/components/PageActions/ActionButton.js +2 -2
  29. package/dist/components/SummaryList/SummaryList.js +2 -2
  30. package/dist/components/TaskList/TaskList.js +2 -2
  31. package/dist/hooks/useGetRequest.js +15 -15
  32. package/dist/hooks/useRefData.js +3 -2
  33. package/dist/utils/CollectionPage/getQuickEditPage.js +2 -2
  34. package/dist/utils/Component/getComponentTests/getComponent.multifile.test.js +2 -1
  35. package/dist/utils/Component/getDefaultValueFromConfig.js +2 -1
  36. package/dist/utils/Condition/meetsCondition.js +26 -12
  37. package/dist/utils/Condition/meetsCondition.test.js +21 -0
  38. package/dist/utils/Data/getAutocompleteSource.js +68 -51
  39. package/dist/utils/Data/getAutocompleteSource.test.js +31 -18
  40. package/dist/utils/Operate/doesContainValue.js +34 -0
  41. package/dist/utils/Operate/doesContainValue.test.js +75 -0
  42. package/dist/utils/Operate/runPageOperations.js +2 -0
  43. package/dist/utils/Validate/validateOnPageLoad.js +23 -0
  44. package/dist/utils/Validate/validateOnPageLoad.test.js +88 -0
  45. package/package.json +4 -4
  46. package/dist/components/FormRenderer/helpers/deleteNodeByPath.js +0 -26
  47. package/dist/components/FormRenderer/helpers/deleteNodeByPath.test.js +0 -56
@@ -0,0 +1,381 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.toArray = exports.removeObjectWithOnlySingleIdField = exports.removeEmptyArraysAndUnusedCollectionIDs = exports.pruneCollectionEntry = exports.isShowEntity = exports.getNestedQuestionPath = exports.getImmediateParent = exports.getDependencyObjectFromPath = exports.getDependencies = exports.findComponentDefinitionInForm = exports.deleteNodeByPath = exports.deleteNodeAndOptions = exports.deleteCorrespondingMetaInfo = exports.deleteComponentData = exports.addValue = void 0;
7
+ var _Condition = _interopRequireDefault(require("../../../utils/Condition"));
8
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
+ /* eslint-disable no-param-reassign */
10
+
11
+ /**
12
+ * Add a value to a map of arrays. If the key exists, append the value to the array.
13
+ * If not, create the map entry with that key.
14
+ *
15
+ * @param {*} key
16
+ * @param {*} value
17
+ * @param {*} multiMap
18
+ */
19
+ const addValue = (key, value, multiMap) => {
20
+ if (!multiMap.has(key)) {
21
+ multiMap.set(key, []);
22
+ }
23
+ multiMap.get(key).push(value);
24
+ };
25
+
26
+ /**
27
+ * Given a path, remove a node from this path within an object.
28
+ *
29
+ * @param {Object} payload Javascript object from which the node will be deleted. Updated by the method.
30
+ * @param {String} path A string containing a decimal point delimited path specifying the node to delete.
31
+ * @return {void}, obj above updated.
32
+ */
33
+ exports.addValue = addValue;
34
+ const deleteNodeByPath = (payload, path) => {
35
+ if (Array.isArray(payload)) {
36
+ // If payload is an array, recursively call deleteNodeByPath on each element
37
+ for (let i = 0; i < payload.length; i += 1) {
38
+ deleteNodeByPath(payload[i], path);
39
+ }
40
+ }
41
+ const keys = path.split('.');
42
+ let current = payload;
43
+ for (let i = 0; i < keys.length - 1; i += 1) {
44
+ current = current[keys[i]];
45
+ if (current === undefined) {
46
+ return;
47
+ }
48
+ }
49
+ if (current[keys[keys.length - 1]]) {
50
+ delete current[keys[keys.length - 1]];
51
+ }
52
+ };
53
+
54
+ /**
55
+ * Return the immediate parent of a path string passed in.
56
+ * Useful for processing 'options' in a form, as nested fieldIds are placed in the payload at
57
+ * the same level in the heirarchy as the option question they relate to.
58
+ *
59
+ * @param {String} path Decimal point delimited path.
60
+ * @returns {String} Immediate parent of the path passed in.
61
+ */
62
+ exports.deleteNodeByPath = deleteNodeByPath;
63
+ const getImmediateParent = path => {
64
+ if (typeof path !== 'string' || !path.includes('.')) {
65
+ return null;
66
+ }
67
+ const parts = path.split('.');
68
+ parts.pop();
69
+ return parts.join('.');
70
+ };
71
+
72
+ /**
73
+ * Questions can be nested within options, eg if you answer 'yes' to a radio question, this can
74
+ * reveal additional nested questions.
75
+ * The path of the fields from these nested questions are at the same level as the original option
76
+ * answer, so this utility derives the nested question path from the option path.
77
+ *
78
+ * @param {String} optionPath Decimal point delimited path
79
+ * @param {String} nestedFieldId fieldId (dataname) of the nested question within the above option
80
+ * @returns {String} Fully qualified path of the nested question
81
+ */
82
+ exports.getImmediateParent = getImmediateParent;
83
+ const getNestedQuestionPath = (optionPath, nestedFieldId) => {
84
+ const parentPath = getImmediateParent(optionPath);
85
+ return parentPath ? "".concat(parentPath, ".").concat(nestedFieldId) : nestedFieldId;
86
+ };
87
+
88
+ /**
89
+ * If the component has options, go through each option removing the data for any nested fields.
90
+ * Required as nested options are in the payload at the same heirarchical level.
91
+
92
+ * @param {Object} payload Javascript object from which the node will be deleted. Updated by the method.
93
+ * @param {String} path A string containing a decimal point delimited path specifying the node to delete.
94
+ * @param {Object} component The form component representing the path being deleted
95
+ * @return {void}, obj above updated.
96
+ */
97
+ exports.getNestedQuestionPath = getNestedQuestionPath;
98
+ const deleteNodeAndOptions = (payload, path, component) => {
99
+ var _component$data;
100
+ deleteNodeByPath(payload, path);
101
+ // If the component has options, go through each option removing the data for any nested fields. Required as nested options are in the payload at the same heirarchical level.
102
+ if (component !== null && component !== void 0 && (_component$data = component.data) !== null && _component$data !== void 0 && _component$data.options) {
103
+ var _component$data2;
104
+ component === null || component === void 0 || (_component$data2 = component.data) === null || _component$data2 === void 0 || (_component$data2 = _component$data2.options) === null || _component$data2 === void 0 || _component$data2.forEach(option => {
105
+ var _option$nested;
106
+ (_option$nested = option.nested) === null || _option$nested === void 0 || _option$nested.forEach(nested => {
107
+ deleteNodeByPath(payload, getNestedQuestionPath(path, nested.fieldId));
108
+ });
109
+ });
110
+ }
111
+ };
112
+
113
+ /**
114
+ * Pruning a collection payload may have resulted in objects that are only left with their 'id' field, which isn't data
115
+ * but added by the renderer to find the activeId. If so, remove these objects entirely as they have been pruned.
116
+ *
117
+ * @param {Array} array Array of objects. Each object which has only 1 remaining field called 'id' should be removed.
118
+ * @return {void}, array above updated.
119
+ */
120
+ exports.deleteNodeAndOptions = deleteNodeAndOptions;
121
+ const removeObjectWithOnlySingleIdField = array => {
122
+ for (let i = array.length - 1; i >= 0; i -= 1) {
123
+ const obj = array[i];
124
+ if (Object.keys(obj).length === 1 && Object.keys(obj)[0] === 'id') {
125
+ array.splice(i, 1);
126
+ }
127
+ }
128
+ };
129
+
130
+ /**
131
+ * Helper method to establish all the payload paths (dependencies) that an entity (page or component) is
132
+ * dependent on through its show_when rule.
133
+ *
134
+ * This will be used to build a graph of chained dependencies to establish in which order
135
+ * to resolve dependencies. The exact rule is not required at this point, just the dependencies.
136
+ *
137
+ * The form specification allows complex show_when blocks including 1...n levels of nesting, so
138
+ * recursively search through the show_when block looking for all 'field' data items.
139
+ *
140
+ * @param {Object} entity Entity whose show_when rule is to be searched for 'field' data items within.
141
+ * @returns {Set} Set of payload paths that the entity (page or component) is dependent on.
142
+ */
143
+ exports.removeObjectWithOnlySingleIdField = removeObjectWithOnlySingleIdField;
144
+ const getDependencies = entity => {
145
+ const findShowWhenFields = (showWhenObject, showWhenFields) => {
146
+ if (typeof showWhenObject === 'object' && showWhenObject !== null) {
147
+ if (Array.isArray(showWhenObject)) {
148
+ showWhenObject.forEach(value => {
149
+ findShowWhenFields(value, showWhenFields);
150
+ });
151
+ } else {
152
+ Object.keys(showWhenObject).forEach(key => {
153
+ if (key === 'field') {
154
+ showWhenFields.push(showWhenObject[key]);
155
+ }
156
+ findShowWhenFields(showWhenObject[key], showWhenFields);
157
+ });
158
+ }
159
+ }
160
+ return showWhenFields;
161
+ };
162
+ return entity.show_when ? new Set(findShowWhenFields(entity.show_when, []) || []) : null;
163
+ };
164
+
165
+ /**
166
+ * Some show_when field values point to data items that are within objects provided by
167
+ * external calls to refdata, eg modeOfTransport.id. These won't map directly to the path
168
+ * keyed components in the allComponents map, so go back up levels in the path until we find
169
+ * the component that creates this field.
170
+ *
171
+ * This component might not be found at all, as the dependency might be on a field that is either
172
+ * provided by cop-ui (eg jobHolderStaffDetails.linemanagerEmail), or by the "addToFormData" function
173
+ * (eg epmsSubmitted). These will be leaf level in the dependency chain so component is not required.
174
+ *
175
+ * @param {String} optionPath Decimal point delimited path
176
+ * @param {String} nestedFieldId fieldId (dataname) of the nested question within the above option
177
+ * @returns {String} Fully qualified path of the nested question
178
+ *
179
+ */
180
+ exports.getDependencies = getDependencies;
181
+ const getDependencyObjectFromPath = (dependencyPath, allComponents) => {
182
+ const segments = dependencyPath.split(".");
183
+ for (let i = segments.length; i > 0; i -= 1) {
184
+ const currentPath = segments.slice(0, i).join(".");
185
+ const dependencyObject = allComponents.get(currentPath);
186
+ if (dependencyObject) return dependencyObject;
187
+ }
188
+ return null;
189
+ };
190
+
191
+ /**
192
+ *
193
+ * Evaluate the show_when rule to establish if the component should be shown (and the
194
+ * payload data should not be pruned). If there is no show_when rule, then return true.
195
+ *
196
+ * @param {*} entity A page or component that may have a show_when rule associated with it
197
+ * @param {*} data The payload data used to evaluate the show_when rule
198
+ * @returns {boolean} true if the show_when rule evaluate to true, or there is no show_when rule
199
+ */
200
+ exports.getDependencyObjectFromPath = getDependencyObjectFromPath;
201
+ const isShowEntity = (entity, data) => {
202
+ var _entity$show_when;
203
+ // If there is no rule set, then the entity can be shown
204
+ if (!entity.show_when) {
205
+ return true;
206
+ }
207
+ if (((_entity$show_when = entity.show_when) === null || _entity$show_when === void 0 ? void 0 : _entity$show_when.type) === "or") {
208
+ return _Condition.default.meetsOne(entity, data);
209
+ }
210
+ return _Condition.default.meetsAll(entity, data);
211
+ };
212
+
213
+ /**
214
+ *
215
+ * Components can be assigned to pages in 2 ways in the form specification:
216
+ *
217
+ * 1 - They can be included in the page's components array with the 'use' field, e.g.
218
+ * {
219
+ * "use": "port"
220
+ * }
221
+ * This "use" value will normally match a component's id, but can match its fieldId
222
+ * NB. In this case, a show_when rule can be applied here, which will supercede any show_when in the component. e.g.
223
+ *
224
+ * {
225
+ * "use": "port",
226
+ * "show_when": ....
227
+ * }
228
+ *
229
+ * 2 - The entire component can be embedded as an entry in the page's component array.
230
+ *
231
+ *
232
+ * @param {*} componentId The id of the component to find in the form.
233
+ * @param {*} componentByIdMap A map of all the form's components keyed on Id, for better performance than searching the form.
234
+ * @param {*} componentByFieldIdMap A map of all the form's components keyed on fieldId, for better performance than searching the form.
235
+ * @returns {Object} A cloned component object, containing the full definition of that component.
236
+ */
237
+ exports.isShowEntity = isShowEntity;
238
+ const findComponentDefinitionInForm = (pageComponentDef, componentByIdMap, componentByFieldIdMap) => {
239
+ var _ref, _componentByIdMap$get;
240
+ const componentInForm = (_ref = (_componentByIdMap$get = componentByIdMap.get(pageComponentDef.use)) !== null && _componentByIdMap$get !== void 0 ? _componentByIdMap$get : componentByFieldIdMap.get(pageComponentDef.use)) !== null && _ref !== void 0 ? _ref : pageComponentDef;
241
+ if (pageComponentDef.use && pageComponentDef.show_when) {
242
+ componentInForm.show_when = pageComponentDef.show_when;
243
+ }
244
+ // Retun clone of component, so subsequent processing can make changes to it without changing the form
245
+ return JSON.parse(JSON.stringify(componentInForm));
246
+ };
247
+
248
+ /**
249
+ *
250
+ * When documents are added to the payload of type multifile, they are also added to a section of the payload
251
+ * 'meta', specifically an array 'documents'. When we remove the multifile elements, we need to remove
252
+ * the corresponding meta.document entries.
253
+ *
254
+ * @param {*} component The component definition being deleted that needs corresponding meta data also deleted
255
+ * @param {*} collectionDataObject The payload containing the file data being deleted (for which the corresponding meta needs deleting)
256
+ * @param {*} formData The entire payload, which will include the meta section
257
+ * @returns {void}, as the formData will be updated in situ
258
+ */
259
+ exports.findComponentDefinitionInForm = findComponentDefinitionInForm;
260
+ const deleteCorrespondingMetaInfo = (component, collectionDataObject, formData) => {
261
+ const {
262
+ meta: {
263
+ documents
264
+ }
265
+ } = formData;
266
+ const fileDataBeingDeleted = collectionDataObject[component.fieldId];
267
+ if (!documents || !fileDataBeingDeleted) return;
268
+ const fileDataAsArray = Array.isArray(fileDataBeingDeleted) ? fileDataBeingDeleted : [fileDataBeingDeleted];
269
+ // Iterate backwards to avoid index shifting when removing elements
270
+ for (let i = fileDataAsArray.length - 1; i >= 0; i -= 1) {
271
+ const matchIndex = documents.findIndex(metaDocument => metaDocument.url === fileDataAsArray[i].url);
272
+ if (matchIndex !== -1) {
273
+ documents.splice(matchIndex, 1);
274
+ }
275
+ }
276
+ };
277
+
278
+ /**
279
+ * After the payload has been cleansed of individual data items, empty arrays and objects may remain.
280
+ * Removing an array (when the payload for a collection) may leave a redundant activeId field. The active Id
281
+ * fields are not part of the payload data, but added by the react renderer to track which collection object
282
+ * is currently being worked on, so can be removed.
283
+ *
284
+ * To tidy this all up, recursively empty arrays and their associated "ActiveId" fields from a nested payload.
285
+ *
286
+ * This function traverses a given payload, which can be an array or an object, and performs the following operations:
287
+ * 1. If an empty array is found inside an array, it is removed using `splice`.
288
+ * 2. If an empty array is found inside an object:
289
+ * - It is removed from the object.
290
+ * - If there is a corresponding `<key>ActiveId` field, that field is also removed.
291
+ * 3. The function operates recursively to handle deeply nested structures.
292
+ *
293
+ * @param {any} payload - The input data structure, which can be an array or an object.
294
+ *
295
+ */
296
+ exports.deleteCorrespondingMetaInfo = deleteCorrespondingMetaInfo;
297
+ const removeEmptyArraysAndUnusedCollectionIDs = payload => {
298
+ if (Array.isArray(payload)) {
299
+ for (let i = payload.length - 1; i >= 0; i -= 1) {
300
+ if (Array.isArray(payload[i]) && payload[i].length === 0) {
301
+ payload.splice(i, 1);
302
+ } else {
303
+ removeEmptyArraysAndUnusedCollectionIDs(payload[i]); // Recurse for nested structures
304
+ }
305
+ // When unwinding out of the recursion, we may have emptied an object which is the remaining element of an
306
+ // array, in which case remove the element
307
+ if (typeof payload[i] === 'object' && Object.keys(payload[i]).length === 0) {
308
+ payload.splice(i, 1);
309
+ }
310
+ }
311
+ } else if (payload !== null && typeof payload === 'object') {
312
+ Object.keys(payload).forEach(key => {
313
+ if (Array.isArray(payload[key]) && payload[key].length === 0) {
314
+ // If the array being removed has an activeId associated with it, remove it
315
+ if (payload["".concat(key, "ActiveId")]) {
316
+ delete payload["".concat(key, "ActiveId")];
317
+ }
318
+ delete payload["".concat(key, "ActiveId")];
319
+ if (payload[key]) {
320
+ delete payload[key];
321
+ }
322
+ } else {
323
+ removeEmptyArraysAndUnusedCollectionIDs(payload[key]); // Recurse for nested structures
324
+ }
325
+ });
326
+ }
327
+ };
328
+
329
+ /**
330
+ * Delete a component's payload item from the overall payload.
331
+ * A component can be defined in >1 places in the form spec. To cater for this,
332
+ * if the componentsToKeep counter tells us that there is > 1 uses of this component
333
+ * still unaccounted for in the form then don't delete, but reduce the count by 1.
334
+ *
335
+ * When the counter reaches 1 we know all other occurences of the component have been resolved
336
+ * so it is safe to delete.
337
+ *
338
+ * @param {*} payload The form payload from which to delete the component data
339
+ * @param {*} path The payload path of the component
340
+ * @param {*} component The component whose data we should attempt to delete
341
+ * @param {*} componentsToKeep A list of all components with a count of their number of uses in the form
342
+ */
343
+ exports.removeEmptyArraysAndUnusedCollectionIDs = removeEmptyArraysAndUnusedCollectionIDs;
344
+ const deleteComponentData = (payload, path, component, componentsToKeep) => {
345
+ if (componentsToKeep[path] > 1) {
346
+ componentsToKeep[path] -= 1;
347
+ } else {
348
+ deleteNodeAndOptions(payload, path, component);
349
+ }
350
+ };
351
+
352
+ /**
353
+ *
354
+ * Takes a single page collection payload object and removes the payload items specified in the componentsToPrune list
355
+ * as long as they don't appear in the componentsToKeep list.
356
+ * Additionally, if the component type is multifile, remove the corresponding data entries that will have been created
357
+ * in the meta section of the payload by the form renderer.
358
+ *
359
+ * @param {Set} pathsToKeep paths that we cannot delete from the collectionDataObject
360
+ * @param {Map} componentsToPrune paths that we should delete, as long as they are not in the pathsToKeep
361
+ * @param {Object} collectionDataObject the payload from which to delete the paths
362
+ * @param {Object} formData The form data, whose meta section may include corresponding documents entries for multifile entries
363
+ */
364
+ exports.deleteComponentData = deleteComponentData;
365
+ const pruneCollectionEntry = (pathsToKeep, componentsToPrune, collectionDataObject, formData) => {
366
+ componentsToPrune.forEach(component => {
367
+ if (!pathsToKeep.has(component.fieldId)) {
368
+ if (component.type === "multifile") {
369
+ deleteCorrespondingMetaInfo(component, collectionDataObject, formData);
370
+ }
371
+ deleteNodeAndOptions(collectionDataObject, component.fieldId, component);
372
+ }
373
+ });
374
+ };
375
+
376
+ /*
377
+ * Converts an object to an array if it isn't already, for use when combining show when rules.
378
+ */
379
+ exports.pruneCollectionEntry = pruneCollectionEntry;
380
+ const toArray = value => Array.isArray(value) ? value : [value];
381
+ exports.toArray = toArray;