@ukhomeoffice/cop-react-form-renderer 4.86.0 → 4.88.0
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.
- package/dist/components/CheckYourAnswers/CheckYourAnswers.js +1 -1
- package/dist/context/HooksContext/HooksContext.js +9 -1
- package/dist/utils/CheckYourAnswers/getCYARowsForCollection.js +2 -2
- package/dist/utils/CheckYourAnswers/getCYARowsForCollection.test.js +104 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForCollectionPage.js +2 -2
- package/dist/utils/CheckYourAnswers/getCYARowsForCollectionPage.test.js +39 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForContainer.js +11 -3
- package/dist/utils/CheckYourAnswers/getCYARowsForContainer.test.js +99 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +11 -3
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.test.js +79 -0
- package/dist/utils/Component/getComponent.js +1 -8
- package/dist/utils/Validate/validateCollection.js +2 -2
- package/dist/utils/Validate/validateCollection.test.js +36 -0
- package/package.json +1 -1
|
@@ -64,7 +64,7 @@ var CheckYourAnswers = function CheckYourAnswers(_ref) {
|
|
|
64
64
|
hooks = _useHooks.hooks;
|
|
65
65
|
(0, _react.useEffect)(function () {
|
|
66
66
|
var getRows = function getRows(page, pageIndex) {
|
|
67
|
-
var rows = _utils.default.CheckYourAnswers.getRows(page, onRowAction);
|
|
67
|
+
var rows = _utils.default.CheckYourAnswers.getRows(page, onRowAction, hooks.onGetCYARows);
|
|
68
68
|
return rows.map(function (row, index) {
|
|
69
69
|
return _objectSpread(_objectSpread({}, row), {}, {
|
|
70
70
|
value: /*#__PURE__*/_react.default.createElement(_Answer.default, {
|
|
@@ -31,8 +31,13 @@ var DEFAULT_HOOKS = {
|
|
|
31
31
|
onGetCYA: function onGetCYA(config) {
|
|
32
32
|
return null;
|
|
33
33
|
},
|
|
34
|
+
// onGetCYARows should return either a single row, or an array of rows for the given component.
|
|
35
|
+
// If it returns null then Form Renderer will attempt to get the default rows for the component.
|
|
36
|
+
onGetCYARows: function onGetCYARows(page, component, onAction) {
|
|
37
|
+
return null;
|
|
38
|
+
},
|
|
34
39
|
onGoingBack: function onGoingBack() {},
|
|
35
|
-
onPageChange: function onPageChange(pageId) {
|
|
40
|
+
onPageChange: function onPageChange(pageId, state) {
|
|
36
41
|
return pageId;
|
|
37
42
|
},
|
|
38
43
|
onRequest: function onRequest(req) {
|
|
@@ -61,6 +66,7 @@ var HooksContextProvider = function HooksContextProvider(_ref) {
|
|
|
61
66
|
onFormLoad: (overrides === null || overrides === void 0 ? void 0 : overrides.onFormLoad) || DEFAULT_HOOKS.onFormLoad,
|
|
62
67
|
onGetComponent: (overrides === null || overrides === void 0 ? void 0 : overrides.onGetComponent) || DEFAULT_HOOKS.onGetComponent,
|
|
63
68
|
onGetCYA: (overrides === null || overrides === void 0 ? void 0 : overrides.onGetCYA) || DEFAULT_HOOKS.onGetCYA,
|
|
69
|
+
onGetCYARows: (overrides === null || overrides === void 0 ? void 0 : overrides.onGetCYARows) || DEFAULT_HOOKS.onGetCYARows,
|
|
64
70
|
onGoingBack: (overrides === null || overrides === void 0 ? void 0 : overrides.onGoingBack) || DEFAULT_HOOKS.onGoingBack,
|
|
65
71
|
onPageChange: (overrides === null || overrides === void 0 ? void 0 : overrides.onPageChange) || DEFAULT_HOOKS.onPageChange,
|
|
66
72
|
onRequest: (overrides === null || overrides === void 0 ? void 0 : overrides.onRequest) || DEFAULT_HOOKS.onRequest,
|
|
@@ -104,6 +110,7 @@ HooksContextProvider.propTypes = {
|
|
|
104
110
|
onFormLoad: _propTypes.default.func,
|
|
105
111
|
onGetComponent: _propTypes.default.func,
|
|
106
112
|
onGetCYA: _propTypes.default.func,
|
|
113
|
+
onGetCYARows: _propTypes.default.func,
|
|
107
114
|
onGoingBack: _propTypes.default.func,
|
|
108
115
|
onPageChange: _propTypes.default.func,
|
|
109
116
|
onRequest: _propTypes.default.func,
|
|
@@ -120,6 +127,7 @@ HooksContextProvider.defaultProps = {
|
|
|
120
127
|
onFormLoad: undefined,
|
|
121
128
|
onGetComponent: undefined,
|
|
122
129
|
onGetCYA: undefined,
|
|
130
|
+
onGetCYARows: undefined,
|
|
123
131
|
onGoingBack: undefined,
|
|
124
132
|
onPageChange: undefined,
|
|
125
133
|
onRequest: undefined,
|
|
@@ -59,14 +59,14 @@ var getTitleRowForItem = function getTitleRowForItem(collection, item, pageId, l
|
|
|
59
59
|
}
|
|
60
60
|
return null;
|
|
61
61
|
};
|
|
62
|
-
var getCYARowsForCollection = function getCYARowsForCollection(page, collection, items, onAction) {
|
|
62
|
+
var getCYARowsForCollection = function getCYARowsForCollection(page, collection, items, onAction, fnOverride) {
|
|
63
63
|
if (Array.isArray(items) && items.length && (0, _showComponentCYA.default)(collection, page.formData)) {
|
|
64
64
|
return items.flatMap(function (item, index) {
|
|
65
65
|
var labelCount = (collection.countOffset || 0) + index + 1;
|
|
66
66
|
var fullPath = "".concat(collection.full_path || collection.fieldId, "[").concat(index, "]");
|
|
67
67
|
var container = getContainerForItem(collection, item, labelCount, fullPath);
|
|
68
68
|
container = (0, _setupContainerComponentsPath.default)(container);
|
|
69
|
-
return [getTitleRowForItem(collection, item, page.id, labelCount, fullPath)].concat((0, _getCYARowsForContainer.default)(page, container, item, onAction));
|
|
69
|
+
return [getTitleRowForItem(collection, item, page.id, labelCount, fullPath)].concat((0, _getCYARowsForContainer.default)(page, container, item, onAction, fnOverride));
|
|
70
70
|
}).filter(function (r) {
|
|
71
71
|
return !!r;
|
|
72
72
|
});
|
|
@@ -181,4 +181,108 @@ describe('utils.CheckYourAnswers.getCYARowsForCollection', function () {
|
|
|
181
181
|
value: 'Bravo'
|
|
182
182
|
});
|
|
183
183
|
});
|
|
184
|
+
it('should ignore the result of the override function if it returns nullish', function () {
|
|
185
|
+
var FORM_DATA = {
|
|
186
|
+
collection: [{
|
|
187
|
+
a: 'Bravo'
|
|
188
|
+
}]
|
|
189
|
+
};
|
|
190
|
+
var PAGE = {
|
|
191
|
+
id: 'page',
|
|
192
|
+
formData: FORM_DATA,
|
|
193
|
+
cya_link: {}
|
|
194
|
+
};
|
|
195
|
+
var COMPONENT = {
|
|
196
|
+
type: 'text',
|
|
197
|
+
readonly: true,
|
|
198
|
+
id: 'a',
|
|
199
|
+
fieldId: 'a',
|
|
200
|
+
label: 'Alpha'
|
|
201
|
+
};
|
|
202
|
+
var COLLECTION = {
|
|
203
|
+
id: 'collection',
|
|
204
|
+
fieldId: 'collection',
|
|
205
|
+
type: _models.ComponentTypes.COLLECTION,
|
|
206
|
+
countOffset: 22,
|
|
207
|
+
item: [COMPONENT],
|
|
208
|
+
value: FORM_DATA.collection,
|
|
209
|
+
formData: FORM_DATA
|
|
210
|
+
};
|
|
211
|
+
var ON_ACTION = function ON_ACTION() {};
|
|
212
|
+
var ROWS = (0, _getCYARowsForCollection.default)(PAGE, COLLECTION, FORM_DATA.collection, ON_ACTION, function () {
|
|
213
|
+
return null;
|
|
214
|
+
});
|
|
215
|
+
expect(ROWS.length).toEqual(2); // Item title row + component row
|
|
216
|
+
(0, _setupTests.expectObjectLike)(ROWS[0], {
|
|
217
|
+
pageId: PAGE.id,
|
|
218
|
+
fieldId: COLLECTION.fieldId,
|
|
219
|
+
full_path: "".concat(COLLECTION.fieldId, "[0]"),
|
|
220
|
+
key: 'Item 23',
|
|
221
|
+
// includes countOffset
|
|
222
|
+
type: 'title',
|
|
223
|
+
action: null
|
|
224
|
+
});
|
|
225
|
+
(0, _setupTests.expectObjectLike)(ROWS[1], {
|
|
226
|
+
pageId: PAGE.id,
|
|
227
|
+
fieldId: COMPONENT.fieldId,
|
|
228
|
+
full_path: "".concat(COLLECTION.fieldId, "[0].").concat(COMPONENT.fieldId),
|
|
229
|
+
key: COMPONENT.label,
|
|
230
|
+
action: null,
|
|
231
|
+
component: _objectSpread(_objectSpread({}, COMPONENT), {}, {
|
|
232
|
+
full_path: "".concat(COLLECTION.fieldId, "[0].").concat(COMPONENT.fieldId)
|
|
233
|
+
}),
|
|
234
|
+
value: 'Bravo'
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
it('should use rows produced by the override function if provided', function () {
|
|
238
|
+
var FORM_DATA = {
|
|
239
|
+
collection: [{
|
|
240
|
+
a: 'Bravo'
|
|
241
|
+
}]
|
|
242
|
+
};
|
|
243
|
+
var PAGE = {
|
|
244
|
+
id: 'page',
|
|
245
|
+
formData: FORM_DATA,
|
|
246
|
+
cya_link: {}
|
|
247
|
+
};
|
|
248
|
+
var COMPONENT = {
|
|
249
|
+
type: 'text',
|
|
250
|
+
readonly: true,
|
|
251
|
+
id: 'a',
|
|
252
|
+
fieldId: 'a',
|
|
253
|
+
label: 'Alpha'
|
|
254
|
+
};
|
|
255
|
+
var COLLECTION = {
|
|
256
|
+
id: 'collection',
|
|
257
|
+
fieldId: 'collection',
|
|
258
|
+
type: _models.ComponentTypes.COLLECTION,
|
|
259
|
+
countOffset: 22,
|
|
260
|
+
item: [COMPONENT],
|
|
261
|
+
value: FORM_DATA.collection,
|
|
262
|
+
formData: FORM_DATA
|
|
263
|
+
};
|
|
264
|
+
var ON_ACTION = function ON_ACTION() {};
|
|
265
|
+
// eslint-disable-next-line arrow-body-style
|
|
266
|
+
var OVERRIDE = function OVERRIDE(page, comp) {
|
|
267
|
+
// This will be called for each component in the collection,
|
|
268
|
+
// returning a custom row object.
|
|
269
|
+
return {
|
|
270
|
+
key: "CustomRowFor".concat(comp.label)
|
|
271
|
+
};
|
|
272
|
+
};
|
|
273
|
+
var ROWS = (0, _getCYARowsForCollection.default)(PAGE, COLLECTION, FORM_DATA.collection, ON_ACTION, OVERRIDE);
|
|
274
|
+
expect(ROWS.length).toEqual(2); // Item title row + component row
|
|
275
|
+
(0, _setupTests.expectObjectLike)(ROWS[0], {
|
|
276
|
+
pageId: PAGE.id,
|
|
277
|
+
fieldId: COLLECTION.fieldId,
|
|
278
|
+
full_path: "".concat(COLLECTION.fieldId, "[0]"),
|
|
279
|
+
key: 'Item 23',
|
|
280
|
+
// includes countOffset
|
|
281
|
+
type: 'title',
|
|
282
|
+
action: null
|
|
283
|
+
});
|
|
284
|
+
(0, _setupTests.expectObjectLike)(ROWS[1], {
|
|
285
|
+
key: "CustomRowFor".concat(COMPONENT.label)
|
|
286
|
+
});
|
|
287
|
+
});
|
|
184
288
|
});
|
|
@@ -89,7 +89,7 @@ var getChangeActionForPage = function getChangeActionForPage(page, item, onActio
|
|
|
89
89
|
}, changeCallback);
|
|
90
90
|
};
|
|
91
91
|
};
|
|
92
|
-
var getCYARowsForCollectionPage = function getCYARowsForCollectionPage(page, onAction) {
|
|
92
|
+
var getCYARowsForCollectionPage = function getCYARowsForCollectionPage(page, onAction, fnOverride) {
|
|
93
93
|
var _page$formData, _page$formData$page$c, _page$formData2, _page$formData2$page$;
|
|
94
94
|
if (!((_page$formData = page.formData) !== null && _page$formData !== void 0 && (_page$formData$page$c = _page$formData[page.collection.name]) !== null && _page$formData$page$c !== void 0 && _page$formData$page$c.length)) {
|
|
95
95
|
return [];
|
|
@@ -129,7 +129,7 @@ var getCYARowsForCollectionPage = function getCYARowsForCollectionPage(page, onA
|
|
|
129
129
|
}
|
|
130
130
|
var container = getContainerForPage(collectionPage, item, labelCount, fullPath);
|
|
131
131
|
var rowChangeAction = getChangeActionForPage(collectionPage, item, onAction);
|
|
132
|
-
var containerRows = (0, _getCYARowsForContainer.default)(collectionPage, container, item, rowChangeAction);
|
|
132
|
+
var containerRows = (0, _getCYARowsForContainer.default)(collectionPage, container, item, rowChangeAction, fnOverride);
|
|
133
133
|
rows = rows.concat(containerRows);
|
|
134
134
|
}
|
|
135
135
|
});
|
|
@@ -372,4 +372,43 @@ describe('utils.CheckYourAnswers.getCYARowsForCollectionPage', function () {
|
|
|
372
372
|
expect(ROWS[ROWS.length - 2].action.label).toEqual('custom remove label');
|
|
373
373
|
expect(ROWS[ROWS.length - 1].action.label).toEqual('custom change label');
|
|
374
374
|
});
|
|
375
|
+
it('should ignore the result of the override function if it returns nullish', function () {
|
|
376
|
+
var FORM_DATA = {
|
|
377
|
+
collection: [{
|
|
378
|
+
id: '01',
|
|
379
|
+
testText: 'value'
|
|
380
|
+
}]
|
|
381
|
+
};
|
|
382
|
+
var PAGE = _objectSpread(_objectSpread({}, MASTER_PAGE), {}, {
|
|
383
|
+
formData: FORM_DATA
|
|
384
|
+
});
|
|
385
|
+
var ROWS = (0, _getCYARowsForCollectionPage.default)(PAGE, null, function () {
|
|
386
|
+
return null;
|
|
387
|
+
});
|
|
388
|
+
expect(ROWS.length).toEqual(7); // Collection title, item title, action rows & component rows
|
|
389
|
+
expect(ROWS[5].key).toEqual("Test text");
|
|
390
|
+
});
|
|
391
|
+
it('should use the rows produced by the override function if provided', function () {
|
|
392
|
+
var FORM_DATA = {
|
|
393
|
+
collection: [{
|
|
394
|
+
id: '01',
|
|
395
|
+
testText: 'value'
|
|
396
|
+
}]
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
// eslint-disable-next-line arrow-body-style
|
|
400
|
+
var OVERRIDE = function OVERRIDE(page, comp) {
|
|
401
|
+
// This will be called for each component on the page,
|
|
402
|
+
// returning a custom row object.
|
|
403
|
+
return {
|
|
404
|
+
key: "CustomRowFor".concat(comp.label)
|
|
405
|
+
};
|
|
406
|
+
};
|
|
407
|
+
var PAGE = _objectSpread(_objectSpread({}, MASTER_PAGE), {}, {
|
|
408
|
+
formData: FORM_DATA
|
|
409
|
+
});
|
|
410
|
+
var ROWS = (0, _getCYARowsForCollectionPage.default)(PAGE, null, OVERRIDE);
|
|
411
|
+
expect(ROWS.length).toEqual(7); // Collection title, item title, action rows & component rows
|
|
412
|
+
expect(ROWS[5].key).toEqual("CustomRowFor".concat(TEXT_COMP.label));
|
|
413
|
+
});
|
|
375
414
|
});
|
|
@@ -16,18 +16,26 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
16
16
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
17
17
|
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
18
18
|
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
19
|
-
var getCYARowsForContainer = function getCYARowsForContainer(page, container, formData, onAction) {
|
|
19
|
+
var getCYARowsForContainer = function getCYARowsForContainer(page, container, formData, onAction, fnOverride) {
|
|
20
20
|
if ((0, _showComponentCYA.default)(container, _objectSpread(_objectSpread({}, page.formData), formData))) {
|
|
21
21
|
var allComponents = (0, _elevateNestedComponents.default)(container.components, formData);
|
|
22
22
|
return allComponents.filter(function (c) {
|
|
23
23
|
return (0, _showComponentCYA.default)(c, _objectSpread(_objectSpread({}, page.formData), formData));
|
|
24
24
|
}).flatMap(function (component) {
|
|
25
25
|
var fd = formData ? formData[component.fieldId] : undefined;
|
|
26
|
+
if (typeof fnOverride === 'function') {
|
|
27
|
+
var overrideRows = fnOverride(_objectSpread(_objectSpread({}, page), {}, {
|
|
28
|
+
formData: formData
|
|
29
|
+
}), component, onAction);
|
|
30
|
+
if (overrideRows) {
|
|
31
|
+
return overrideRows;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
26
34
|
if (component.type === _models.ComponentTypes.CONTAINER) {
|
|
27
|
-
return getCYARowsForContainer(page, component, _objectSpread(_objectSpread({}, fd), formData), onAction);
|
|
35
|
+
return getCYARowsForContainer(page, component, _objectSpread(_objectSpread({}, fd), formData), onAction, fnOverride);
|
|
28
36
|
}
|
|
29
37
|
if (component.type === _models.ComponentTypes.COLLECTION) {
|
|
30
|
-
return (0, _getCYARowsForCollection.default)(page, component, fd, onAction);
|
|
38
|
+
return (0, _getCYARowsForCollection.default)(page, component, fd, onAction, fnOverride);
|
|
31
39
|
}
|
|
32
40
|
return (0, _getCYARow.default)(_objectSpread(_objectSpread({}, page), {}, {
|
|
33
41
|
formData: formData
|
|
@@ -384,4 +384,103 @@ describe('utils.CheckYourAnswers.getCYARowsForContainer', function () {
|
|
|
384
384
|
var ROWS = (0, _getCYARowsForContainer.default)(PAGE, CONTAINER, FORM_DATA.container, ON_ACTION);
|
|
385
385
|
expect(ROWS.length).toEqual(2);
|
|
386
386
|
});
|
|
387
|
+
it('should ignore the result of the override function if it returns nullish', function () {
|
|
388
|
+
var FORM_DATA = {
|
|
389
|
+
container: {
|
|
390
|
+
a: 'Alpha Charlie',
|
|
391
|
+
b: 'Bravo Charlie'
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
var PAGE = {
|
|
395
|
+
id: 'page',
|
|
396
|
+
formData: FORM_DATA,
|
|
397
|
+
cya_link: {}
|
|
398
|
+
};
|
|
399
|
+
var COMPONENT_A = {
|
|
400
|
+
type: 'text',
|
|
401
|
+
id: 'a',
|
|
402
|
+
fieldId: 'a',
|
|
403
|
+
label: 'Alpha'
|
|
404
|
+
};
|
|
405
|
+
var COMPONENT_B = {
|
|
406
|
+
type: 'text',
|
|
407
|
+
id: 'b',
|
|
408
|
+
fieldId: 'b',
|
|
409
|
+
label: 'Bravo'
|
|
410
|
+
};
|
|
411
|
+
var CONTAINER = {
|
|
412
|
+
id: 'container',
|
|
413
|
+
fieldId: 'container',
|
|
414
|
+
type: _models.ComponentTypes.CONTAINER,
|
|
415
|
+
components: [COMPONENT_A, COMPONENT_B],
|
|
416
|
+
value: FORM_DATA.container,
|
|
417
|
+
formData: FORM_DATA
|
|
418
|
+
};
|
|
419
|
+
var ON_ACTION = function ON_ACTION() {};
|
|
420
|
+
var ROWS = (0, _getCYARowsForContainer.default)(PAGE, CONTAINER, FORM_DATA.container, ON_ACTION, function () {
|
|
421
|
+
return null;
|
|
422
|
+
});
|
|
423
|
+
expect(ROWS.length).toEqual(2);
|
|
424
|
+
ROWS.forEach(function (row, index) {
|
|
425
|
+
(0, _setupTests.expectObjectLike)(row, {
|
|
426
|
+
pageId: PAGE.id,
|
|
427
|
+
fieldId: CONTAINER.components[index].fieldId,
|
|
428
|
+
key: CONTAINER.components[index].label,
|
|
429
|
+
component: CONTAINER.components[index],
|
|
430
|
+
value: "".concat(CONTAINER.components[index].label, " Charlie")
|
|
431
|
+
});
|
|
432
|
+
(0, _setupTests.expectObjectLike)(row.action, {
|
|
433
|
+
onAction: ON_ACTION
|
|
434
|
+
});
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
it('should use rows produced by the override function if provided', function () {
|
|
438
|
+
var FORM_DATA = {
|
|
439
|
+
container: {
|
|
440
|
+
a: 'Alpha Charlie',
|
|
441
|
+
b: 'Bravo Charlie'
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
var PAGE = {
|
|
445
|
+
id: 'page',
|
|
446
|
+
formData: FORM_DATA,
|
|
447
|
+
cya_link: {}
|
|
448
|
+
};
|
|
449
|
+
var COMPONENT_A = {
|
|
450
|
+
type: 'text',
|
|
451
|
+
id: 'a',
|
|
452
|
+
fieldId: 'a',
|
|
453
|
+
label: 'Alpha'
|
|
454
|
+
};
|
|
455
|
+
var COMPONENT_B = {
|
|
456
|
+
type: 'text',
|
|
457
|
+
id: 'b',
|
|
458
|
+
fieldId: 'b',
|
|
459
|
+
label: 'Bravo'
|
|
460
|
+
};
|
|
461
|
+
var CONTAINER = {
|
|
462
|
+
id: 'container',
|
|
463
|
+
fieldId: 'container',
|
|
464
|
+
type: _models.ComponentTypes.CONTAINER,
|
|
465
|
+
components: [COMPONENT_A, COMPONENT_B],
|
|
466
|
+
value: FORM_DATA.container,
|
|
467
|
+
formData: FORM_DATA
|
|
468
|
+
};
|
|
469
|
+
var ON_ACTION = function ON_ACTION() {};
|
|
470
|
+
// eslint-disable-next-line arrow-body-style
|
|
471
|
+
var OVERRIDE = function OVERRIDE(page, comp) {
|
|
472
|
+
// This will be called for each component in the container,
|
|
473
|
+
// returning a custom row object.
|
|
474
|
+
return {
|
|
475
|
+
key: "CustomRowFor".concat(comp.label)
|
|
476
|
+
};
|
|
477
|
+
};
|
|
478
|
+
var ROWS = (0, _getCYARowsForContainer.default)(PAGE, CONTAINER, FORM_DATA.container, ON_ACTION, OVERRIDE);
|
|
479
|
+
expect(ROWS.length).toEqual(2);
|
|
480
|
+
expect(ROWS).toEqual([{
|
|
481
|
+
key: 'CustomRowForAlpha'
|
|
482
|
+
}, {
|
|
483
|
+
key: 'CustomRowForBravo'
|
|
484
|
+
}]);
|
|
485
|
+
});
|
|
387
486
|
});
|
|
@@ -21,24 +21,32 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
21
21
|
*
|
|
22
22
|
* @param {object} page The page to show components for.
|
|
23
23
|
* @param {Function} onAction A function to invoke if the change link on any row is clicked.
|
|
24
|
+
* @param {Function} fnOverride An optional override for getting the CYA rows for each component.
|
|
24
25
|
*
|
|
25
26
|
* @returns An array of configuration objects for Check your answers rows.
|
|
26
27
|
*/
|
|
27
28
|
var getCYARowsForPage = function getCYARowsForPage(page, onAction) {
|
|
29
|
+
var fnOverride = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
28
30
|
if (_FormPage.default.showCYA(page, page.formData)) {
|
|
29
31
|
var _page$groups;
|
|
30
32
|
if (page.collection) {
|
|
31
|
-
return (0, _getCYARowsForCollectionPage.default)(page, onAction);
|
|
33
|
+
return (0, _getCYARowsForCollectionPage.default)(page, onAction, fnOverride);
|
|
32
34
|
}
|
|
33
35
|
var allComponents = (0, _elevateNestedComponents.default)(page.components, page.formData);
|
|
34
36
|
var rows = allComponents.filter(function (c) {
|
|
35
37
|
return (0, _showComponentCYA.default)(c, page.formData);
|
|
36
38
|
}).flatMap(function (component) {
|
|
39
|
+
if (typeof fnOverride === 'function') {
|
|
40
|
+
var overrideRows = fnOverride(page, component, onAction);
|
|
41
|
+
if (overrideRows) {
|
|
42
|
+
return overrideRows;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
37
45
|
switch (component.type) {
|
|
38
46
|
case _models.ComponentTypes.CONTAINER:
|
|
39
|
-
return (0, _getCYARowsForContainer.default)(page, component, page.formData[component.fieldId], onAction);
|
|
47
|
+
return (0, _getCYARowsForContainer.default)(page, component, page.formData[component.fieldId], onAction, fnOverride);
|
|
40
48
|
case _models.ComponentTypes.COLLECTION:
|
|
41
|
-
return (0, _getCYARowsForCollection.default)(page, component, page.formData[component.fieldId], onAction);
|
|
49
|
+
return (0, _getCYARowsForCollection.default)(page, component, page.formData[component.fieldId], onAction, fnOverride);
|
|
42
50
|
default:
|
|
43
51
|
return (0, _getCYARow.default)(page, component, onAction);
|
|
44
52
|
}
|
|
@@ -262,6 +262,85 @@ describe('utils', function () {
|
|
|
262
262
|
value: 'Bravo'
|
|
263
263
|
});
|
|
264
264
|
});
|
|
265
|
+
it('should ignore the result of the override function if it returns nullish', function () {
|
|
266
|
+
var COMPONENT_A = {
|
|
267
|
+
type: 'text',
|
|
268
|
+
id: 'a',
|
|
269
|
+
fieldId: 'a',
|
|
270
|
+
label: 'Alpha'
|
|
271
|
+
};
|
|
272
|
+
var COMPONENT_B = {
|
|
273
|
+
type: 'text',
|
|
274
|
+
id: 'b',
|
|
275
|
+
fieldId: 'b',
|
|
276
|
+
label: 'Bravo'
|
|
277
|
+
};
|
|
278
|
+
var PAGE = {
|
|
279
|
+
id: 'page',
|
|
280
|
+
components: [COMPONENT_A, COMPONENT_B],
|
|
281
|
+
formData: {
|
|
282
|
+
a: 'Alpha Charlie',
|
|
283
|
+
b: 'Bravo Charlie'
|
|
284
|
+
},
|
|
285
|
+
cya_link: {}
|
|
286
|
+
};
|
|
287
|
+
var ON_ACTION = function ON_ACTION() {};
|
|
288
|
+
var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION, function () {
|
|
289
|
+
return null;
|
|
290
|
+
});
|
|
291
|
+
expect(ROWS.length).toEqual(2);
|
|
292
|
+
ROWS.forEach(function (row, index) {
|
|
293
|
+
(0, _setupTests.expectObjectLike)(row, {
|
|
294
|
+
pageId: PAGE.id,
|
|
295
|
+
fieldId: PAGE.components[index].fieldId,
|
|
296
|
+
key: PAGE.components[index].label,
|
|
297
|
+
component: PAGE.components[index],
|
|
298
|
+
value: "".concat(PAGE.components[index].label, " Charlie")
|
|
299
|
+
});
|
|
300
|
+
(0, _setupTests.expectObjectLike)(row.action, {
|
|
301
|
+
onAction: ON_ACTION
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
it('should use rows produced by the override function if provided', function () {
|
|
306
|
+
var COMPONENT_A = {
|
|
307
|
+
type: 'text',
|
|
308
|
+
id: 'a',
|
|
309
|
+
fieldId: 'a',
|
|
310
|
+
label: 'Alpha'
|
|
311
|
+
};
|
|
312
|
+
var COMPONENT_B = {
|
|
313
|
+
type: 'text',
|
|
314
|
+
id: 'b',
|
|
315
|
+
fieldId: 'b',
|
|
316
|
+
label: 'Bravo'
|
|
317
|
+
};
|
|
318
|
+
var PAGE = {
|
|
319
|
+
id: 'page',
|
|
320
|
+
components: [COMPONENT_A, COMPONENT_B],
|
|
321
|
+
formData: {
|
|
322
|
+
a: 'Alpha Charlie',
|
|
323
|
+
b: 'Bravo Charlie'
|
|
324
|
+
},
|
|
325
|
+
cya_link: {}
|
|
326
|
+
};
|
|
327
|
+
var ON_ACTION = function ON_ACTION() {};
|
|
328
|
+
// eslint-disable-next-line arrow-body-style
|
|
329
|
+
var OVERRIDE = function OVERRIDE(page, comp) {
|
|
330
|
+
// This will be called for each component on the page,
|
|
331
|
+
// returning a custom row object.
|
|
332
|
+
return {
|
|
333
|
+
key: "CustomRowFor".concat(comp.label)
|
|
334
|
+
};
|
|
335
|
+
};
|
|
336
|
+
var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION, OVERRIDE);
|
|
337
|
+
expect(ROWS.length).toEqual(2);
|
|
338
|
+
expect(ROWS).toEqual([{
|
|
339
|
+
key: 'CustomRowForAlpha'
|
|
340
|
+
}, {
|
|
341
|
+
key: 'CustomRowForBravo'
|
|
342
|
+
}]);
|
|
343
|
+
});
|
|
265
344
|
});
|
|
266
345
|
});
|
|
267
346
|
});
|
|
@@ -22,7 +22,6 @@ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input ==
|
|
|
22
22
|
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
23
23
|
var LIST_CLASS = 'govuk-list';
|
|
24
24
|
var BODY_CLASS = 'govuk-body';
|
|
25
|
-
var INPUT_CLASS = 'govuk-input';
|
|
26
25
|
|
|
27
26
|
/**
|
|
28
27
|
* Separate function for each component type for the sake of
|
|
@@ -159,13 +158,7 @@ var getTextArea = function getTextArea(config) {
|
|
|
159
158
|
};
|
|
160
159
|
var getTextInput = function getTextInput(config) {
|
|
161
160
|
var attrs = (0, _cleanAttributes.default)(config);
|
|
162
|
-
|
|
163
|
-
var className = attrs.fluidWidth ? [attrs.fluidWidth] : undefined;
|
|
164
|
-
return /*#__PURE__*/_react.default.createElement(_copReactComponents.TextInput, _extends({}, attrs, {
|
|
165
|
-
classBlock: INPUT_CLASS,
|
|
166
|
-
classModifiers: classModifiers,
|
|
167
|
-
className: className
|
|
168
|
-
}));
|
|
161
|
+
return /*#__PURE__*/_react.default.createElement(_copReactComponents.TextInput, attrs);
|
|
169
162
|
};
|
|
170
163
|
var getTime = function getTime(config) {
|
|
171
164
|
var attrs = (0, _cleanAttributes.default)(config);
|
|
@@ -32,10 +32,10 @@ var validateCollection = function validateCollection(collection, items, formData
|
|
|
32
32
|
var newComponent = _objectSpread(_objectSpread({}, component), {}, {
|
|
33
33
|
full_path: "".concat(fullPath, ".").concat(component.fieldId)
|
|
34
34
|
});
|
|
35
|
-
if (Array.isArray(component.custom_errors)) {
|
|
35
|
+
if (component.custom_errors && Array.isArray(component.custom_errors)) {
|
|
36
36
|
newComponent.custom_errors = component.custom_errors.map(function (error) {
|
|
37
37
|
return _objectSpread(_objectSpread({}, error), {}, {
|
|
38
|
-
message: _copReactComponents.Utils.interpolateString(error.message, {
|
|
38
|
+
message: _copReactComponents.Utils.interpolateString(index === 0 && error.firstOnlyMessage ? error.firstOnlyMessage : error.message, {
|
|
39
39
|
index: labelCount
|
|
40
40
|
})
|
|
41
41
|
});
|
|
@@ -86,4 +86,40 @@ describe('utils.Validate.Collection', function () {
|
|
|
86
86
|
id: "".concat(ID, "[0].").concat(COLLECTION.type)
|
|
87
87
|
}]);
|
|
88
88
|
});
|
|
89
|
+
it('should correctly display firstOnlyMessage error message', function () {
|
|
90
|
+
var ID = 'container';
|
|
91
|
+
var LABEL = 'field';
|
|
92
|
+
var TYPE = 'autocomplete';
|
|
93
|
+
var ITEM = setup(ID, TYPE, LABEL, true, []);
|
|
94
|
+
ITEM.custom_errors = [{
|
|
95
|
+
"type": "required",
|
|
96
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
97
|
+
"message": "Test to check that ${index} is 1",
|
|
98
|
+
"firstOnlyMessage": "Test for first message"
|
|
99
|
+
}];
|
|
100
|
+
var COLLECTION = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false, [ITEM]);
|
|
101
|
+
COLLECTION.countOffset = 0;
|
|
102
|
+
expect((0, _validateCollection.default)(COLLECTION, ITEM.custom_errors)).toEqual([{
|
|
103
|
+
error: "Test for first message",
|
|
104
|
+
id: "".concat(ID, "[0].").concat(COLLECTION.type)
|
|
105
|
+
}]);
|
|
106
|
+
});
|
|
107
|
+
it('should correctly display message if firstOnlyMessage is undefined', function () {
|
|
108
|
+
var ID = 'container';
|
|
109
|
+
var LABEL = 'field';
|
|
110
|
+
var TYPE = 'autocomplete';
|
|
111
|
+
var ITEM = setup(ID, TYPE, LABEL, true, []);
|
|
112
|
+
ITEM.custom_errors = [{
|
|
113
|
+
"type": "required",
|
|
114
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
115
|
+
"message": "Test to check that ${index} is 1",
|
|
116
|
+
"firstOnlyMessage": undefined
|
|
117
|
+
}];
|
|
118
|
+
var COLLECTION = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false, [ITEM]);
|
|
119
|
+
COLLECTION.countOffset = 0;
|
|
120
|
+
expect((0, _validateCollection.default)(COLLECTION, ITEM.custom_errors)).toEqual([{
|
|
121
|
+
error: "Test to check that 1 is 1",
|
|
122
|
+
id: "".concat(ID, "[0].").concat(COLLECTION.type)
|
|
123
|
+
}]);
|
|
124
|
+
});
|
|
89
125
|
});
|