@ukhomeoffice/cop-react-form-renderer 4.22.5 → 4.24.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.
@@ -103,7 +103,7 @@ var DEFAULT_CLASS = 'hods-form';
103
103
  exports.DEFAULT_CLASS = DEFAULT_CLASS;
104
104
 
105
105
  var InternalFormRenderer = function InternalFormRenderer(_ref2) {
106
- var _formState$page4;
106
+ var _formState$page5;
107
107
 
108
108
  var title = _ref2.title,
109
109
  type = _ref2.type,
@@ -261,7 +261,11 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
261
261
 
262
262
  if (pages) {
263
263
  pages.forEach(function (page) {
264
- page.formData = data;
264
+ if (page) {
265
+ page.formData = data;
266
+ }
267
+
268
+ ;
265
269
  });
266
270
  setCurrentTask(function (prev) {
267
271
  return _objectSpread(_objectSpread({}, prev), {}, {
@@ -413,11 +417,13 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
413
417
  }
414
418
  }; // Handle navigation from "Check your answers".
415
419
  // Passing a dataCallback allows changes to formData from the
416
- // CYA screen. This is necessary to alow changing the active
420
+ // CYA screen. This is necessary to allow changing the active
417
421
  // entry in page collections.
418
422
 
419
423
 
420
424
  var onCYARowAction = function onCYARowAction(page, dataCallback) {
425
+ var _formState$page3;
426
+
421
427
  if (typeof dataCallback === 'function') {
422
428
  var newData = dataCallback(_objectSpread({}, data));
423
429
 
@@ -426,6 +432,19 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
426
432
  }
427
433
  }
428
434
 
435
+ if (((_formState$page3 = formState.page) === null || _formState$page3 === void 0 ? void 0 : _formState$page3.type) === _models.FormPages.PARTIAL_CYA) {
436
+ hubDetails.sections.every(function (section) {
437
+ return section.tasks.every(function (task) {
438
+ if (task.pages.includes(page.pageId) && task.name !== currentTask.name) {
439
+ onTaskAction(task);
440
+ return false;
441
+ }
442
+
443
+ return true;
444
+ });
445
+ });
446
+ }
447
+
429
448
  if (page) {
430
449
  _handlers.default.cyaAction(page, pageId, onPageChange);
431
450
  }
@@ -501,16 +520,40 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
501
520
  }
502
521
  }
503
522
 
504
- if (action.type === _models.PageAction.TYPES.SAVE_AND_RETURN) {
523
+ if (action.type === _models.PageAction.TYPES.SUBMIT && hub === _models.HubFormats.TASK) {
524
+ setPagePoint('submit');
525
+
505
526
  if (_helpers.default.canCYASubmit(currentTask.fullPages, validate.pages)) {
527
+ // Submit.
506
528
  var _submissionData2 = _utils.default.Format.form({
507
529
  pages: pages,
508
530
  components: components
509
531
  }, _objectSpread({}, data), _models.EventTypes.SUBMIT);
510
532
 
511
- _submissionData2.formStatus = _helpers.default.getSubmissionStatus(type, pages, pageId, action, _submissionData2, currentTask);
512
- setData(_submissionData2);
533
+ _submissionData2.formStatus = _helpers.default.getSubmissionStatus(type, pages, pageId, action, _submissionData2, currentTask, true);
534
+ setData(_submissionData2); // Now submit the data to the backend...
535
+
513
536
  hooks.onSubmit(action.type, _submissionData2, function () {
537
+ return hooks.onFormComplete();
538
+ }, function (errors) {
539
+ return _handlers.default.submissionError(errors, addErrors);
540
+ });
541
+ }
542
+ }
543
+
544
+ if (action.type === _models.PageAction.TYPES.SAVE_AND_RETURN) {
545
+ var shouldValidate = !(action.hasOwnProperty('validate') && !action.validate);
546
+ var canSubmit = shouldValidate ? _helpers.default.canCYASubmit(currentTask.fullPages, validate.pages) : true;
547
+
548
+ if (canSubmit) {
549
+ var _submissionData3 = _utils.default.Format.form({
550
+ pages: pages,
551
+ components: components
552
+ }, _objectSpread({}, data), _models.EventTypes.SUBMIT);
553
+
554
+ _submissionData3.formStatus = _helpers.default.getSubmissionStatus(type, pages, pageId, action, _submissionData3, currentTask);
555
+ setData(_submissionData3);
556
+ hooks.onSubmit(action.type, _submissionData3, function () {
514
557
  if (type === _models.FormTypes.TASK) {
515
558
  onPageChange(undefined);
516
559
  } else {
@@ -523,9 +566,9 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
523
566
  }
524
567
 
525
568
  if (action.type === _models.PageAction.TYPES.NAVIGATE) {
526
- var _formState$page3;
569
+ var _formState$page4;
527
570
 
528
- onPageChange(_helpers.default.getNextPageId(type, pages, pageId, action, (_formState$page3 = formState.page) === null || _formState$page3 === void 0 ? void 0 : _formState$page3.formData));
571
+ onPageChange(_helpers.default.getNextPageId(type, pages, pageId, action, (_formState$page4 = formState.page) === null || _formState$page4 === void 0 ? void 0 : _formState$page4.formData));
529
572
  }
530
573
 
531
574
  if (action.type === _models.PageAction.TYPES.CANCEL) {
@@ -543,7 +586,7 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
543
586
  className: classes()
544
587
  }, title && !hide_title && pageId === _models.FormPages.HUB && /*#__PURE__*/_react.default.createElement(_copReactComponents.LargeHeading, null, title), formState.cya && /*#__PURE__*/_react.default.createElement(_CheckYourAnswers.default, _extends({
545
588
  pages: _helpers.default.getRelevantPages(formState, pages, currentTask.fullPages)
546
- }, cya, ((_formState$page4 = formState.page) === null || _formState$page4 === void 0 ? void 0 : _formState$page4.type) === _models.FormPages.PARTIAL_CYA && {
589
+ }, cya, ((_formState$page5 = formState.page) === null || _formState$page5 === void 0 ? void 0 : _formState$page5.type) === _models.FormPages.PARTIAL_CYA && {
547
590
  actions: formState.page.actions
548
591
  }, formState.cya, {
549
592
  onAction: onCYAAction,
@@ -9,7 +9,7 @@ var _models = require("../../../models");
9
9
 
10
10
  var slicePages = function slicePages(pages, currentPageId) {
11
11
  return pages.slice(0, pages.findIndex(function (p) {
12
- return p.id === currentPageId;
12
+ return (p === null || p === void 0 ? void 0 : p.id) === currentPageId ? true : false;
13
13
  }));
14
14
  };
15
15
 
@@ -29,12 +29,12 @@ var getSubmissionStatus = function getSubmissionStatus(formType, pages, currentP
29
29
  section.tasks.forEach(function (task) {
30
30
  var lastPage = task.pages[task.pages.length - 1];
31
31
 
32
- if ((lastPage === currentPageId || lastPage.id === currentPageId) && isCompleted) {
32
+ if ((lastPage === currentPageId || lastPage.id === currentPageId) && isCompleted && (task === null || task === void 0 ? void 0 : task.type) === 'pre-task-list') {
33
33
  formStatus.tasks[task.name] = {
34
34
  complete: isCompleted,
35
35
  currentPage: currentPageId
36
36
  };
37
- } else if (task.pages.includes(currentPageId)) {
37
+ } else if (task.pages.includes(currentPageId) && (task === null || task === void 0 ? void 0 : task.type) === 'pre-task-list') {
38
38
  formStatus.tasks[task.name] = {
39
39
  complete: false,
40
40
  currentPage: currentPageId
@@ -255,7 +255,7 @@ describe('components', function () {
255
255
  })
256
256
  });
257
257
  });
258
- it("should mark the current task as complete if the current task is 'undefined' i.e. it is a pre-task-list task", function () {
258
+ it("should mark the current task as complete if the current task is a pre-task-list task", function () {
259
259
  var CURRENT_PAGE_ID = PAGES[PAGES.length - 1].id;
260
260
  var TASK_NAME = undefined;
261
261
  var CURRENT_TASK = {
@@ -265,6 +265,7 @@ describe('components', function () {
265
265
  complete: true
266
266
  };
267
267
  var TASKS = [{
268
+ type: 'pre-task-list',
268
269
  pages: PAGES
269
270
  }];
270
271
  var SECTIONS = [{
@@ -7,8 +7,6 @@ exports.default = void 0;
7
7
 
8
8
  var _copReactComponents = require("@ukhomeoffice/cop-react-components");
9
9
 
10
- var _elevateNestedComponents = _interopRequireDefault(require("../Component/elevateNestedComponents"));
11
-
12
10
  var _models = require("../../models");
13
11
 
14
12
  var _getCYARowsForContainer = _interopRequireDefault(require("./getCYARowsForContainer"));
@@ -17,8 +15,6 @@ var _getCYACollectionDeleteAction = _interopRequireDefault(require("./getCYAColl
17
15
 
18
16
  var _getCYACollectionChangeAction = _interopRequireDefault(require("./getCYACollectionChangeAction"));
19
17
 
20
- var _showComponentCYA = _interopRequireDefault(require("./showComponentCYA"));
21
-
22
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
19
 
24
20
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
@@ -28,27 +24,13 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
28
24
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
29
25
 
30
26
  var getContainerForPage = function getContainerForPage(page, item, labelCount, full_path) {
31
- var allComponents = (0, _elevateNestedComponents.default)(page.components, item);
32
27
  return {
33
28
  id: item.id,
34
29
  fieldId: item.id,
35
30
  type: _models.ComponentTypes.CONTAINER,
36
31
  required: true,
37
32
  full_path: full_path,
38
- components: allComponents.map(function (component) {
39
- if ((0, _showComponentCYA.default)(component, item)) {
40
- return _objectSpread(_objectSpread({}, component), {}, {
41
- label: _copReactComponents.Utils.interpolateString(component.label, _objectSpread(_objectSpread({}, item), {}, {
42
- index: labelCount
43
- })),
44
- full_path: "".concat(full_path, ".").concat(component.fieldId)
45
- });
46
- }
47
-
48
- return undefined;
49
- }).filter(function (c) {
50
- return c;
51
- })
33
+ components: page.components
52
34
  };
53
35
  };
54
36
 
@@ -5,6 +5,10 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
+ var _addShowWhen = _interopRequireDefault(require("../Component/addShowWhen"));
9
+
10
+ var _applyToComponentTree = _interopRequireDefault(require("../Component/applyToComponentTree"));
11
+
8
12
  var _showFormPageCYA = _interopRequireDefault(require("../FormPage/showFormPageCYA"));
9
13
 
10
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -15,15 +19,31 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
15
19
 
16
20
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
21
 
22
+ var getComponentsWithRouting = function getComponentsWithRouting(page) {
23
+ if (!page.collection.route) {
24
+ return page.components;
25
+ } // Apply show_whens to components on certain conditional pages.
26
+ // This ensures that those components aren't shown on the CYA
27
+ // screen if they were never visited by the user.
28
+
29
+
30
+ return page.components.map(function (component) {
31
+ return (0, _applyToComponentTree.default)(component, function (c) {
32
+ return (0, _addShowWhen.default)(c, page.collection.route);
33
+ });
34
+ });
35
+ };
36
+
18
37
  var createMasterPage = function createMasterPage(page) {
19
38
  return _objectSpread(_objectSpread({}, page), {}, {
20
- title: page.collection.name.charAt(0).toUpperCase() + page.collection.name.slice(1)
39
+ title: page.collection.name.charAt(0).toUpperCase() + page.collection.name.slice(1),
40
+ components: getComponentsWithRouting(page)
21
41
  });
22
42
  };
23
43
 
24
44
  var mergeIntoMasterPage = function mergeIntoMasterPage(page, masterPage) {
25
45
  masterPage.collection = _objectSpread(_objectSpread({}, masterPage.collection), page.collection);
26
- masterPage.components = [].concat(masterPage.components, page.components);
46
+ masterPage.components = [].concat(masterPage.components, getComponentsWithRouting(page));
27
47
  masterPage.formData = _objectSpread({}, page.formData);
28
48
  };
29
49
  /**
@@ -44,7 +64,7 @@ var mergeCollectionPages = function mergeCollectionPages(pages) {
44
64
  return pages.map(function (page) {
45
65
  var _page$collection;
46
66
 
47
- if ((_page$collection = page.collection) !== null && _page$collection !== void 0 && _page$collection.name && (0, _showFormPageCYA.default)(page, page.formData)) {
67
+ if (page !== null && page !== void 0 && (_page$collection = page.collection) !== null && _page$collection !== void 0 && _page$collection.name && (0, _showFormPageCYA.default)(page, page.formData)) {
48
68
  if (!masterPages[page.collection.name]) {
49
69
  // If no master page exists for this collection.name, then
50
70
  // we kick one off using this page as a template.
@@ -128,4 +128,74 @@ describe('utils.CollectionPage.mergeCollectionPages', function () {
128
128
  formData: {}
129
129
  });
130
130
  });
131
+ it('should add a defined route as a show_when to components on the page', function () {
132
+ var PAGES = [{
133
+ id: 'page1',
134
+ collection: {
135
+ name: 'collection',
136
+ route: {
137
+ field: 'field',
138
+ op: '=',
139
+ value: 'value'
140
+ }
141
+ },
142
+ components: [{
143
+ type: 'text'
144
+ }]
145
+ }, {
146
+ id: 'page2',
147
+ collection: {
148
+ name: 'collection',
149
+ route: {
150
+ field: 'field',
151
+ op: '=',
152
+ value: 'otherValue'
153
+ }
154
+ },
155
+ components: [{
156
+ type: 'container',
157
+ components: [{
158
+ type: 'date'
159
+ }]
160
+ }]
161
+ }];
162
+ var RESULT = (0, _mergeCollectionPages.default)(PAGES);
163
+ expect(RESULT.length).toEqual(1);
164
+ expect(RESULT[0]).toEqual({
165
+ id: 'page1',
166
+ title: 'Collection',
167
+ collection: {
168
+ name: 'collection',
169
+ route: {
170
+ field: 'field',
171
+ op: '=',
172
+ value: 'otherValue'
173
+ }
174
+ },
175
+ components: [{
176
+ type: 'text',
177
+ show_when: [{
178
+ field: 'field',
179
+ op: '=',
180
+ value: 'value'
181
+ }]
182
+ }, {
183
+ type: 'container',
184
+ components: [{
185
+ type: 'date',
186
+ show_when: [{
187
+ field: 'field',
188
+ op: '=',
189
+ value: 'otherValue'
190
+ }]
191
+ }],
192
+ show_when: [{
193
+ field: 'field',
194
+ op: '=',
195
+ value: 'otherValue'
196
+ }]
197
+ }],
198
+ formData: {}
199
+ });
200
+ });
131
201
  });
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
9
+
10
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
11
+
12
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
13
+
14
+ /**
15
+ * Adds a show_when to a component.
16
+ * If the component has no show_when, then an array is created
17
+ * with condition as the only entry.
18
+ * If the component has existing show_whens then condition is
19
+ * appended to the existing array.
20
+ * @param {*} component The component to add the show_when to.
21
+ * @param {*} condition The show_when condition to add.
22
+ * @returns The component with the new show_when added.
23
+ * null if component or condition are invalid.
24
+ */
25
+ var addShowWhen = function addShowWhen(component, condition) {
26
+ if (!component) {
27
+ return null;
28
+ }
29
+
30
+ if (!condition) {
31
+ return component;
32
+ }
33
+
34
+ var result = _objectSpread({}, component);
35
+
36
+ if (result.show_when) {
37
+ result.show_when = Array.isArray(result.show_when) ? [].concat(result.show_when, [_objectSpread({}, condition)]) : [result.show_when, _objectSpread({}, condition)];
38
+ } else {
39
+ result.show_when = [_objectSpread({}, condition)];
40
+ }
41
+
42
+ return result;
43
+ };
44
+
45
+ var _default = addShowWhen;
46
+ exports.default = _default;
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+
3
+ var _addShowWhen = _interopRequireDefault(require("./addShowWhen"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ // Local imports
8
+ describe('utils.Component.addShowWhen', function () {
9
+ var NEW_SHOW_WHEN = {
10
+ field: 'field',
11
+ op: '=',
12
+ value: 'value'
13
+ };
14
+ it('should return null when component is undefined', function () {
15
+ var result = (0, _addShowWhen.default)(undefined, {});
16
+ expect(result).toEqual(null);
17
+ });
18
+ it('should return null when component is null', function () {
19
+ var result = (0, _addShowWhen.default)(null, {});
20
+ expect(result).toEqual(null);
21
+ });
22
+ it('should return component when condition is undefined', function () {
23
+ var COMPONENT = {
24
+ alpha: 'bravo'
25
+ };
26
+ var result = (0, _addShowWhen.default)(COMPONENT, undefined);
27
+ expect(result).toEqual({
28
+ alpha: 'bravo'
29
+ });
30
+ });
31
+ it('should return component when condition is null', function () {
32
+ var COMPONENT = {
33
+ alpha: 'bravo'
34
+ };
35
+ var result = (0, _addShowWhen.default)(COMPONENT, null);
36
+ expect(result).toEqual({
37
+ alpha: 'bravo'
38
+ });
39
+ });
40
+ it('should add the show_when to a component with no existing show_whens', function () {
41
+ var COMPONENT = {
42
+ alpha: 'bravo'
43
+ };
44
+ var result = (0, _addShowWhen.default)(COMPONENT, NEW_SHOW_WHEN);
45
+ expect(result).toEqual({
46
+ alpha: 'bravo',
47
+ show_when: [NEW_SHOW_WHEN]
48
+ });
49
+ });
50
+ it('should add the show_when to a component with a single show_when', function () {
51
+ var COMPONENT = {
52
+ alpha: 'bravo',
53
+ show_when: {
54
+ field: 'abc',
55
+ op: '=',
56
+ value: 'bcd'
57
+ }
58
+ };
59
+ var result = (0, _addShowWhen.default)(COMPONENT, NEW_SHOW_WHEN);
60
+ expect(result).toEqual({
61
+ alpha: 'bravo',
62
+ show_when: [{
63
+ field: 'abc',
64
+ op: '=',
65
+ value: 'bcd'
66
+ }, NEW_SHOW_WHEN]
67
+ });
68
+ });
69
+ it('should add the show_when to a component with an array of show_whens', function () {
70
+ var COMPONENT = {
71
+ alpha: 'bravo',
72
+ show_when: [{
73
+ field: 'abc',
74
+ op: '=',
75
+ value: 'bcd'
76
+ }, {
77
+ field: 'cde',
78
+ op: '=',
79
+ value: 'efg'
80
+ }]
81
+ };
82
+ var result = (0, _addShowWhen.default)(COMPONENT, NEW_SHOW_WHEN);
83
+ expect(result).toEqual({
84
+ alpha: 'bravo',
85
+ show_when: [{
86
+ field: 'abc',
87
+ op: '=',
88
+ value: 'bcd'
89
+ }, {
90
+ field: 'cde',
91
+ op: '=',
92
+ value: 'efg'
93
+ }, NEW_SHOW_WHEN]
94
+ });
95
+ });
96
+ });
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _models = require("../../models");
9
+
10
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
11
+
12
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
13
+
14
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15
+
16
+ /**
17
+ * Call a callback on a component and all of its child/nested components.
18
+ * At each stage, the value of the component is set to the returned value
19
+ * from the callback.
20
+ *
21
+ * @param {*} root The root component to call the callback on.
22
+ * @param {*} callback The callback to call. It should take a single
23
+ * component as an argument and return the new
24
+ * desired value for that component.
25
+ * @returns The result of calling callback on root.
26
+ * null if root is not valid.
27
+ * root if callback is not valid.
28
+ */
29
+ var applyToComponentTree = function applyToComponentTree(root, callback) {
30
+ var _root$data;
31
+
32
+ if (!root) {
33
+ return null;
34
+ }
35
+
36
+ if (!callback) {
37
+ return root;
38
+ }
39
+
40
+ var result = _objectSpread({}, root);
41
+
42
+ switch (root.type) {
43
+ case _models.ComponentTypes.CONTAINER:
44
+ result.components.forEach(function (c, index, array) {
45
+ array[index] = applyToComponentTree(c, callback);
46
+ });
47
+ break;
48
+
49
+ case _models.ComponentTypes.COLLECTION:
50
+ result.item.forEach(function (c, index, array) {
51
+ array[index] = applyToComponentTree(c, callback);
52
+ });
53
+ break;
54
+
55
+ case _models.ComponentTypes.CHECKBOXES:
56
+ case _models.ComponentTypes.RADIOS:
57
+ if (!((_root$data = root.data) !== null && _root$data !== void 0 && _root$data.options)) {
58
+ break;
59
+ }
60
+
61
+ result.data = _objectSpread({}, root.data);
62
+ result.data.options = root.data.options.map(function (option) {
63
+ var newOption = _objectSpread({}, option);
64
+
65
+ if (option.nested) {
66
+ newOption.nested = option.nested.map(function (c) {
67
+ return applyToComponentTree(c, callback);
68
+ });
69
+ }
70
+
71
+ return newOption;
72
+ });
73
+ break;
74
+
75
+ default:
76
+ break;
77
+ }
78
+
79
+ return callback(result);
80
+ };
81
+
82
+ var _default = applyToComponentTree;
83
+ exports.default = _default;
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+
3
+ var _applyToComponentTree = _interopRequireDefault(require("./applyToComponentTree"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
8
+
9
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
10
+
11
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
12
+
13
+ describe('utils.Component.applyToComponentTree', function () {
14
+ var SINGLE_COMP = {
15
+ type: 'text',
16
+ label: 'alpha'
17
+ };
18
+ var CONTAINER = {
19
+ type: 'container',
20
+ label: 'bravo',
21
+ components: [{
22
+ type: 'text',
23
+ label: 'charlie'
24
+ }]
25
+ };
26
+ var COLLECTION = {
27
+ type: 'collection',
28
+ label: 'delta',
29
+ item: [{
30
+ type: 'text',
31
+ label: 'echo'
32
+ }]
33
+ };
34
+ var COMP_WITH_NESTED = {
35
+ type: 'radios',
36
+ label: 'foxtrot',
37
+ data: {
38
+ options: [{
39
+ nested: [{
40
+ type: 'text',
41
+ label: 'golf'
42
+ }]
43
+ }, {
44
+ nested: [{
45
+ type: 'text',
46
+ label: 'hotel'
47
+ }]
48
+ }]
49
+ }
50
+ };
51
+
52
+ var CALLBACK = function CALLBACK(component) {
53
+ return _objectSpread(_objectSpread({}, component), {}, {
54
+ label: component.label.toUpperCase()
55
+ });
56
+ };
57
+
58
+ it('should return null when root is undefined', function () {
59
+ var result = (0, _applyToComponentTree.default)(undefined, CALLBACK);
60
+ expect(result).toEqual(null);
61
+ });
62
+ it('should return null when root is null', function () {
63
+ var result = (0, _applyToComponentTree.default)(null, CALLBACK);
64
+ expect(result).toEqual(null);
65
+ });
66
+ it('should return root when callback is undefined', function () {
67
+ var result = (0, _applyToComponentTree.default)({
68
+ type: 'text'
69
+ }, undefined);
70
+ expect(result).toEqual({
71
+ type: 'text'
72
+ });
73
+ });
74
+ it('should return root when callback is null', function () {
75
+ var result = (0, _applyToComponentTree.default)({
76
+ type: 'text'
77
+ }, null);
78
+ expect(result).toEqual({
79
+ type: 'text'
80
+ });
81
+ });
82
+ it('should correctly call callback on a single component', function () {
83
+ var result = (0, _applyToComponentTree.default)(SINGLE_COMP, CALLBACK);
84
+ expect(result).toEqual({
85
+ type: 'text',
86
+ label: 'ALPHA'
87
+ });
88
+ });
89
+ it('should correctly call callback on a container component', function () {
90
+ var result = (0, _applyToComponentTree.default)(CONTAINER, CALLBACK);
91
+ expect(result).toEqual({
92
+ type: 'container',
93
+ label: 'BRAVO',
94
+ components: [{
95
+ type: 'text',
96
+ label: 'CHARLIE'
97
+ }]
98
+ });
99
+ });
100
+ it('should correctly call callback on a collection component', function () {
101
+ var result = (0, _applyToComponentTree.default)(COLLECTION, CALLBACK);
102
+ expect(result).toEqual({
103
+ type: 'collection',
104
+ label: 'DELTA',
105
+ item: [{
106
+ type: 'text',
107
+ label: 'ECHO'
108
+ }]
109
+ });
110
+ });
111
+ it('should correctly call callback on a component with nested components', function () {
112
+ var result = (0, _applyToComponentTree.default)(COMP_WITH_NESTED, CALLBACK);
113
+ expect(result).toEqual({
114
+ type: 'radios',
115
+ label: 'FOXTROT',
116
+ data: {
117
+ options: [{
118
+ nested: [{
119
+ type: 'text',
120
+ label: 'GOLF'
121
+ }]
122
+ }, {
123
+ nested: [{
124
+ type: 'text',
125
+ label: 'HOTEL'
126
+ }]
127
+ }]
128
+ }
129
+ });
130
+ });
131
+ });
@@ -72,6 +72,13 @@ var meetsCondition = function meetsCondition(condition, value) {
72
72
  return value === null || value === void 0 ? void 0 : value.toString().toLowerCase().includes(compare); // If no value is provided, the field cannot contain it, so it must fail the condition.
73
73
  }
74
74
 
75
+ case '!contains':
76
+ {
77
+ var _value$toString$toLow;
78
+
79
+ return (_value$toString$toLow = !(value !== null && value !== void 0 && value.toString().toLowerCase().includes(compare))) !== null && _value$toString$toLow !== void 0 ? _value$toString$toLow : true; // If no value is provided, the field cannot contain it, so it must meet the condition.
80
+ }
81
+
75
82
  default:
76
83
  return false;
77
84
  }
@@ -350,6 +350,95 @@ describe('utils.Condition.meetsCondition', function () {
350
350
  expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeFalsy();
351
351
  });
352
352
  });
353
+ describe('operator !contains', function () {
354
+ var op = '!contains'; // Should match...
355
+
356
+ it('should match a string that is not in the field array', function () {
357
+ var FIELD = ['alpha', 'bravo', 'charlie'];
358
+ var VALUE = 'delta';
359
+ var CONDITION = getCondition(op, VALUE);
360
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
361
+ });
362
+ it('should match any value if the field array is empty', function () {
363
+ var FIELD = [];
364
+ var VALUE = 'alpha';
365
+ var CONDITION = getCondition(op, VALUE);
366
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
367
+ });
368
+ it('should match any field when the value is null', function () {
369
+ var FIELD = ['alpha', 'bravo', 'charlie'];
370
+ var VALUE = null;
371
+ var CONDITION = getCondition(op, VALUE);
372
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
373
+ });
374
+ it('should match any field when the value is undefined', function () {
375
+ var FIELD = ['alpha', 'bravo', 'charlie'];
376
+ var VALUE = undefined;
377
+ var CONDITION = getCondition(op, VALUE);
378
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
379
+ });
380
+ it('should match any value when the field is an empty string', function () {
381
+ var FIELD = '';
382
+ var VALUE = 'alpha';
383
+ var CONDITION = getCondition(op, VALUE);
384
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
385
+ });
386
+ it('should match a number that is missing from the field array', function () {
387
+ var FIELD = [1, 2, 3];
388
+ var VALUE = 4;
389
+ var CONDITION = getCondition(op, VALUE);
390
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
391
+ });
392
+ it('should match a substring that is missing from the field string', function () {
393
+ var FIELD = 'alphabravocharlie';
394
+ var VALUE = 'delta';
395
+ var CONDITION = getCondition(op, VALUE);
396
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
397
+ });
398
+ it('should match any value when the field is null', function () {
399
+ var FIELD = null;
400
+ var VALUE = 'alpha';
401
+ var CONDITION = getCondition(op, VALUE);
402
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
403
+ });
404
+ it('should match any value when the field is undefined', function () {
405
+ var FIELD = undefined;
406
+ var VALUE = 'alpha';
407
+ var CONDITION = getCondition(op, VALUE);
408
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeTruthy();
409
+ }); // Should reject...
410
+
411
+ it('should reject a string that is in the field array', function () {
412
+ var FIELD = ['alpha', 'bravo', 'charlie'];
413
+ var VALUE = 'alpha';
414
+ var CONDITION = getCondition(op, VALUE);
415
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeFalsy();
416
+ });
417
+ it('should reject a sub-string that is in the field array', function () {
418
+ var FIELD = ['alpha', 'bravo', 'charlie'];
419
+ var VALUE = 'alp';
420
+ var CONDITION = getCondition(op, VALUE);
421
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeFalsy();
422
+ });
423
+ it('should reject a number that is in the field array', function () {
424
+ var FIELD = [1, 2, 3];
425
+ var VALUE = 1;
426
+ var CONDITION = getCondition(op, VALUE);
427
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeFalsy();
428
+ });
429
+ it('should reject a sub-string that is in the field string', function () {
430
+ var FIELD = 'alphabravocharlie';
431
+ var VALUE = 'alpha';
432
+ var CONDITION = getCondition(op, VALUE);
433
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeFalsy();
434
+ });
435
+ it('should reject a string that is in the field array regardless of case', function () {
436
+ var FIELD = ['Alpha', 'bravo', 'charlie'];
437
+ var VALUE = 'alpha';
438
+ var CONDITION = getCondition(op, VALUE);
439
+ expect((0, _meetsCondition.default)(CONDITION, FIELD)).toBeFalsy();
440
+ });
441
+ });
353
442
  describe('unknown operator', function () {
354
443
  var op = 'definitely_not_a_real_operator';
355
444
  it('should reject anything regardless of the value', function () {
@@ -35,7 +35,7 @@ var validatePage = function validatePage(page) {
35
35
  if (activeIndex !== null) {
36
36
  var _page$formData$page$c;
37
37
 
38
- data = (_page$formData$page$c = page.formData[page.collection.name]) === null || _page$formData$page$c === void 0 ? void 0 : _page$formData$page$c[activeIndex];
38
+ data = _objectSpread(_objectSpread({}, page.formData), (_page$formData$page$c = page.formData[page.collection.name]) === null || _page$formData$page$c === void 0 ? void 0 : _page$formData$page$c[activeIndex]);
39
39
  }
40
40
  }
41
41
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.22.5",
3
+ "version": "4.24.0",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",
@@ -16,7 +16,7 @@
16
16
  "post-compile": "rimraf dist/*.test.* dist/**/*.test.* dist/**/*.stories.* dist/docs dist/assets"
17
17
  },
18
18
  "dependencies": {
19
- "@ukhomeoffice/cop-react-components": "2.3.4",
19
+ "@ukhomeoffice/cop-react-components": "2.4.0",
20
20
  "axios": "^0.23.0",
21
21
  "dayjs": "^1.11.0",
22
22
  "govuk-frontend": "^4.3.1",