@ukhomeoffice/cop-react-form-renderer 5.61.0 → 5.62.1

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.
@@ -31,6 +31,7 @@ var DEFAULT_CLASS = exports.DEFAULT_CLASS = _FormPage2.DEFAULT_CLASS;
31
31
  var CollectionPage = function CollectionPage(_ref) {
32
32
  var _page$collection, _objectSpread4;
33
33
  var page = _ref.page,
34
+ pages = _ref.pages,
34
35
  onCollectionChange = _ref.onCollectionChange,
35
36
  onAction = _ref.onAction,
36
37
  hashLink = _ref.hashLink,
@@ -127,6 +128,7 @@ var CollectionPage = function CollectionPage(_ref) {
127
128
  });
128
129
  })
129
130
  }),
131
+ pages: pages,
130
132
  onAction: onInnerPageAction,
131
133
  onWrapperChange: onInnerPageChange,
132
134
  onTopLevelChange: onTopLevelChange,
@@ -149,6 +151,7 @@ CollectionPage.propTypes = {
149
151
  actions: _propTypes.default.arrayOf(_propTypes.default.oneOfType([_propTypes.default.shape({}), _propTypes.default.string])),
150
152
  formData: _propTypes.default.shape({}).isRequired
151
153
  }).isRequired,
154
+ pages: _propTypes.default.arrayOf(_propTypes.default.shape({})).isRequired,
152
155
  onCollectionChange: _propTypes.default.func,
153
156
  onAction: _propTypes.default.func.isRequired,
154
157
  hashLink: _propTypes.default.bool,
@@ -67,6 +67,17 @@ var FormComponent = function FormComponent(_ref) {
67
67
  // eslint-disable-next-line no-param-reassign
68
68
  target.value = target.value.trim();
69
69
  }
70
+ if (component.type === _models.ComponentTypes.CHECKBOXES && Array.isArray(target.value)) {
71
+ target.value.forEach(function (item) {
72
+ if (item.children && item.nested) {
73
+ // eslint-disable-next-line no-param-reassign
74
+ delete item.children;
75
+ // eslint-disable-next-line no-param-reassign
76
+ delete item.nested;
77
+ }
78
+ });
79
+ }
80
+
70
81
  // eslint-disable-next-line no-param-reassign
71
82
  target = (0, _helpers.addLabel)(target, component, data);
72
83
  onChange({
@@ -366,6 +366,7 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
366
366
  submitting: submitting
367
367
  }), formState.page && !formState.cya && formState.page.collection && /*#__PURE__*/_react.default.createElement(_CollectionPage.default, {
368
368
  page: formState.page,
369
+ pages: pages,
369
370
  onCollectionChange: onChange,
370
371
  onAction: function onAction(action, patch, patchLabel) {
371
372
  (0, _onPageAction.default)(action, patch, patchLabel, hooks, data, formState, validate, onPageChange, type, pages, components, pageId, setPagePoint, currentTask, setData, hubDetails, setSubmitted, addErrors, submitting, setSubmitting, errors);
@@ -29,7 +29,7 @@ var STATUS_COMPLETE = exports.STATUS_COMPLETE = 'complete';
29
29
  var getRefDataUrl = function getRefDataUrl(component) {
30
30
  // eslint-disable-next-line prefer-destructuring
31
31
  var data = component.data;
32
- if (data && !data.options) {
32
+ if (data && !data.options || data !== null && data !== void 0 && data.url) {
33
33
  return _copReactComponents.Utils.interpolateString(data.url, component.formData);
34
34
  }
35
35
  return undefined;
@@ -17,7 +17,6 @@ exports.default = void 0;
17
17
  */
18
18
  var addCollectionPageEntry = function addCollectionPageEntry(collectionName, formData) {
19
19
  var fieldName = collectionName.split('.').pop();
20
- formData["".concat(fieldName, "ActiveId")] = '0';
21
- formData["".concat(fieldName, "NumberedIndex")] = Number(formData["".concat(fieldName, "ActiveId")]) + 1;
20
+ formData["".concat(fieldName, "ActiveId")] = null;
22
21
  };
23
22
  var _default = exports.default = addCollectionPageEntry;
@@ -3,34 +3,14 @@
3
3
  var _addCollectionPageEntry = _interopRequireDefault(require("./addCollectionPageEntry"));
4
4
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
5
5
  describe('Utils.CollectionPage.addCollectionPageEntry', function () {
6
- it('should correctly add a new entry for a top-level collection', function () {
6
+ it('should set active ID as null for a top-level collection', function () {
7
7
  var FORM_DATA = {};
8
8
  (0, _addCollectionPageEntry.default)('testCollection', FORM_DATA);
9
- expect(FORM_DATA.testCollectionActiveId).toBeTruthy();
9
+ expect(FORM_DATA.testCollectionActiveId).toBeFalsy();
10
10
  });
11
- it('should correctly add a new entry for a nested collection', function () {
11
+ it('should set a active ID as null for a nested collection', function () {
12
12
  var FORM_DATA = {};
13
13
  (0, _addCollectionPageEntry.default)('parent.child.grandchild', FORM_DATA);
14
- expect(FORM_DATA.grandchildActiveId).toBeTruthy();
15
- });
16
- it('should correctly set the active ID for a top-level collection', function () {
17
- var FORM_DATA = {};
18
- (0, _addCollectionPageEntry.default)('testCollection', FORM_DATA);
19
- expect(FORM_DATA.testCollectionActiveId).toBe('0');
20
- });
21
- it('should correctly set the active ID for a nested collection', function () {
22
- var FORM_DATA = {};
23
- (0, _addCollectionPageEntry.default)('parent.child.grandchild', FORM_DATA);
24
- expect(FORM_DATA.grandchildActiveId).toBe('0');
25
- });
26
- it('should correctly increment the numbered index for a top-level collection', function () {
27
- var FORM_DATA = {};
28
- (0, _addCollectionPageEntry.default)('testCollection', FORM_DATA);
29
- expect(FORM_DATA.testCollectionNumberedIndex).toBe(1);
30
- });
31
- it('should correctly increment the numbered index for a nested collection', function () {
32
- var FORM_DATA = {};
33
- (0, _addCollectionPageEntry.default)('parent.child.grandchild', FORM_DATA);
34
- expect(FORM_DATA.grandchildNumberedIndex).toBe(1);
14
+ expect(FORM_DATA.grandchildActiveId).toBeFalsy();
35
15
  });
36
16
  });
@@ -28,15 +28,20 @@ var setCollectionPageData = function setCollectionPageData(collectionName, newDa
28
28
  var data = formData;
29
29
  nameParts.some(function (name) {
30
30
  var _data$name;
31
- // Current collection doesn't exist, so create it,
32
- // add an entry and set the active id.
31
+ // Current collection doesn't exist, so create it
33
32
  if (!data[name]) {
33
+ data[name] = [];
34
+ }
35
+ // If the entry matching the active ID doesn't exist in the collection, create it.
36
+ if (!data[name].find(function (e) {
37
+ return e.id === formData["".concat(name, "ActiveId")];
38
+ })) {
34
39
  var newEntryId = Date.now().toString();
35
40
  // eslint-disable-next-line no-param-reassign
36
41
  formData["".concat(name, "ActiveId")] = formData["".concat(name, "ActiveId")] || newEntryId;
37
- data[name] = [{
42
+ data[name].push({
38
43
  id: formData["".concat(name, "ActiveId")]
39
- }];
44
+ });
40
45
  }
41
46
  // We're at the target collection's level, so
42
47
  // set the new data.
@@ -160,4 +160,22 @@ describe('Utils.CollectionPage.setCollectionPageData', function () {
160
160
  value: 'newValue'
161
161
  });
162
162
  });
163
+ it('creates a new entry and updates active ID if no matching active ID is found', function () {
164
+ var formData = {};
165
+ var collectionName = 'testCollection';
166
+ var newData = [{
167
+ id: 'dataId',
168
+ value: 'testValue'
169
+ }];
170
+ (0, _setCollectionPageData.default)(collectionName, newData, formData);
171
+
172
+ // Check if formData has been updated
173
+ expect(formData["".concat(collectionName, "ActiveId")]).toBeDefined();
174
+
175
+ // Check if the new entry was added to the collection
176
+ expect(formData[collectionName]).toBeDefined();
177
+ expect(formData[collectionName].length).toBe(1);
178
+ expect(formData[collectionName][0].id).toBe(newData[0].id);
179
+ expect(formData[collectionName][0]).toEqual(expect.objectContaining(newData[0]));
180
+ });
163
181
  });
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  var _copReactComponents = require("@ukhomeoffice/cop-react-components");
8
8
  var _Condition = _interopRequireDefault(require("../Condition"));
9
+ var _nestInRefdataOptions = _interopRequireDefault(require("./nestInRefdataOptions"));
9
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
11
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
11
12
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
@@ -35,6 +36,11 @@ var interpolateOptions = function interpolateOptions(config, options) {
35
36
  };
36
37
  var getOptions = function getOptions(config, callback) {
37
38
  if (config) {
39
+ var _config$data, _config$data2;
40
+ if (config.options && config !== null && config !== void 0 && (_config$data = config.data) !== null && _config$data !== void 0 && _config$data.options && config !== null && config !== void 0 && (_config$data2 = config.data) !== null && _config$data2 !== void 0 && _config$data2.url) {
41
+ var combinedOptions = (0, _nestInRefdataOptions.default)(config.options, config.data.options);
42
+ return callback(interpolateOptions(config, combinedOptions));
43
+ }
38
44
  if (config.options) {
39
45
  return callback(interpolateOptions(config, config.options));
40
46
  }
@@ -135,6 +135,61 @@ describe('utils', function () {
135
135
  expect(options).toEqual([CONFIG.options[0]]);
136
136
  });
137
137
  });
138
+ it('should should use both data from refdata url and options if both present', function () {
139
+ var CONFIG = {
140
+ data: {
141
+ options: [{
142
+ refdata_match: {
143
+ id: 2
144
+ },
145
+ nested: [{
146
+ value: 'a',
147
+ label: 'Alpha'
148
+ }]
149
+ }],
150
+ url: 'refdata.url'
151
+ },
152
+ options: [{
153
+ id: 1,
154
+ description: 'First refdata item',
155
+ value: 'firstValue',
156
+ label: 'First label'
157
+ }, {
158
+ id: 2,
159
+ description: 'Second refdata item',
160
+ value: 'secondValue',
161
+ label: 'Second label'
162
+ }, {
163
+ id: 3,
164
+ description: 'Third refdata item',
165
+ value: 'thirdValue',
166
+ label: 'Third label'
167
+ }]
168
+ };
169
+ var MERGED_CONFIG = [{
170
+ id: 1,
171
+ description: 'First refdata item',
172
+ value: 'firstValue',
173
+ label: 'First label'
174
+ }, {
175
+ id: 2,
176
+ description: 'Second refdata item',
177
+ value: 'secondValue',
178
+ nested: [{
179
+ value: 'a',
180
+ label: 'Alpha'
181
+ }],
182
+ label: 'Second label'
183
+ }, {
184
+ id: 3,
185
+ description: 'Third refdata item',
186
+ value: 'thirdValue',
187
+ label: 'Third label'
188
+ }];
189
+ (0, _getOptions.default)(CONFIG, function (options) {
190
+ expect(options).toEqual(MERGED_CONFIG);
191
+ });
192
+ });
138
193
  });
139
194
  });
140
195
  });
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
8
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
9
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
10
+ 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; }
11
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
12
+ 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); }
13
+ /**
14
+ * Combines refdata options with custom options with the use of `from_refdata` in the custom option
15
+ * Allowing for nesting of components within refdata lists of checkboxes or radios
16
+ * @param {Array} refdataOptions The list of options coming from refdata
17
+ * @param {Array} options The list of options being defined in the form
18
+ * @returns A combined list of refdata options and custom options to be rendered
19
+ */
20
+ var nestInRefdataOptions = function nestInRefdataOptions(refdataOptions, options) {
21
+ var nestedRefdataOptions = refdataOptions;
22
+ options.forEach(function (option) {
23
+ if (option.refdata_match) {
24
+ var refdataMatch = option.refdata_match;
25
+ var refdataKey = Object.keys(refdataMatch)[0];
26
+ var refdataIndex = refdataOptions.findIndex(function (refdataOption) {
27
+ return refdataOption[refdataKey] === refdataMatch[refdataKey];
28
+ });
29
+ if (refdataIndex >= 0) {
30
+ nestedRefdataOptions[refdataIndex] = _objectSpread(_objectSpread({}, refdataOptions[refdataIndex]), {}, {
31
+ nested: option.nested
32
+ });
33
+ }
34
+ }
35
+ });
36
+ return nestedRefdataOptions;
37
+ };
38
+ var _default = exports.default = nestInRefdataOptions;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+
3
+ var _nestInRefdataOptions = _interopRequireDefault(require("./nestInRefdataOptions"));
4
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
5
+ describe("utils.Data.combineOptions", function () {
6
+ it("should combine refdata options with a custom option", function () {
7
+ var refdataOptions = [{
8
+ id: 1,
9
+ description: "First refdata item",
10
+ value: "firstValue",
11
+ label: "First label"
12
+ }, {
13
+ id: 2,
14
+ description: "Second refdata item",
15
+ value: "secondValue",
16
+ label: "Second label"
17
+ }, {
18
+ id: 3,
19
+ description: "Third refdata item",
20
+ value: "thirdValue",
21
+ label: "Third label"
22
+ }];
23
+ var customOptions = [{
24
+ refdata_match: {
25
+ id: 2
26
+ },
27
+ nested: [{
28
+ id: "nestedComponent",
29
+ fieldId: "nestedComponent",
30
+ type: "text",
31
+ label: "Nested Component"
32
+ }]
33
+ }];
34
+ expect((0, _nestInRefdataOptions.default)(refdataOptions, customOptions)).toEqual([{
35
+ description: "First refdata item",
36
+ id: 1,
37
+ label: "First label",
38
+ value: "firstValue"
39
+ }, {
40
+ description: "Second refdata item",
41
+ id: 2,
42
+ label: "Second label",
43
+ nested: [{
44
+ id: "nestedComponent",
45
+ fieldId: "nestedComponent",
46
+ type: "text",
47
+ label: "Nested Component"
48
+ }],
49
+ value: "secondValue"
50
+ }, {
51
+ description: "Third refdata item",
52
+ id: 3,
53
+ label: "Third label",
54
+ value: "thirdValue"
55
+ }]);
56
+ });
57
+ it("should not combine options if it can't find a matching refdata option", function () {
58
+ var refdataOptions = [{
59
+ id: 1,
60
+ description: "First refdata item",
61
+ value: "firstValue",
62
+ label: "First label"
63
+ }, {
64
+ id: 2,
65
+ description: "Second refdata item",
66
+ value: "secondValue",
67
+ label: "Second label"
68
+ }, {
69
+ id: 3,
70
+ description: "Third refdata item",
71
+ value: "thirdValue",
72
+ label: "Third label"
73
+ }];
74
+ var customOptions = [{
75
+ refdata_match: {
76
+ id: 4
77
+ },
78
+ nested: [{
79
+ id: "nestedComponent",
80
+ fieldId: "nestedComponent",
81
+ type: "text",
82
+ label: "Nested Component"
83
+ }]
84
+ }];
85
+ expect((0, _nestInRefdataOptions.default)(refdataOptions, customOptions)).toEqual([{
86
+ description: "First refdata item",
87
+ id: 1,
88
+ label: "First label",
89
+ value: "firstValue"
90
+ }, {
91
+ description: "Second refdata item",
92
+ id: 2,
93
+ label: "Second label",
94
+ value: "secondValue"
95
+ }, {
96
+ description: "Third refdata item",
97
+ id: 3,
98
+ label: "Third label",
99
+ value: "thirdValue"
100
+ }]);
101
+ });
102
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "5.61.0",
3
+ "version": "5.62.1",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",