@ukhomeoffice/cop-react-form-renderer 5.2.1 → 5.5.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.
@@ -92,7 +92,12 @@ var CollectionPage = function CollectionPage(_ref) {
92
92
  };
93
93
  return /*#__PURE__*/_react.default.createElement(_FormPage.default, {
94
94
  page: _objectSpread(_objectSpread({}, page), {}, {
95
- formData: _objectSpread(_objectSpread(_objectSpread({}, page.formData), data[activeIndex]), {}, _defineProperty({}, "".concat(page.collection.name, "ActiveIndex"), activeIndex))
95
+ formData: _objectSpread(_objectSpread(_objectSpread({}, page.formData), data[activeIndex]), {}, _defineProperty({}, "".concat(page.collection.name, "ActiveIndex"), activeIndex)),
96
+ components: page.components.map(function (component) {
97
+ return _objectSpread(_objectSpread({}, component), {}, {
98
+ full_path: "".concat(page.collection.name, "[").concat(activeIndex, "].").concat(component.full_path || component.fieldId)
99
+ });
100
+ })
96
101
  }),
97
102
  onAction: onInnerPageAction,
98
103
  onWrapperChange: onInnerPageChange,
@@ -141,7 +141,7 @@ describe('components.CollectionPage', function () {
141
141
  expect(label.tagName).toEqual('LABEL');
142
142
  expect(label.classList).toContain('govuk-label');
143
143
  expect(label.textContent).toEqual(lbl);
144
- expect(label.getAttribute('for')).toEqual(fieldId);
144
+ expect(label.getAttribute('for')).toEqual("".concat(COLLECTION_NAME, "[0].").concat(fieldId));
145
145
  var hint = formGroup.childNodes[1];
146
146
  expect(hint.tagName).toEqual('DIV');
147
147
  expect(hint.classList).toContain('govuk-hint');
@@ -149,7 +149,7 @@ describe('components.CollectionPage', function () {
149
149
  var input = formGroup.childNodes[2];
150
150
  expect(input.tagName).toEqual('INPUT');
151
151
  expect(input.classList).toContain('govuk-input');
152
- expect(input.id).toEqual(fieldId);
152
+ expect(input.id).toEqual("".concat(COLLECTION_NAME, "[0].").concat(fieldId));
153
153
  expect(input.value).toEqual(val);
154
154
  return input;
155
155
  };
@@ -160,7 +160,7 @@ describe('components.CollectionPage', function () {
160
160
  expect(label.tagName).toEqual('LABEL');
161
161
  expect(label.classList).toContain('govuk-label');
162
162
  expect(label.textContent).toEqual(lbl);
163
- expect(label.getAttribute('for')).toEqual(fieldId);
163
+ expect(label.getAttribute('for')).toEqual("".concat(COLLECTION_NAME, "[0].").concat(fieldId));
164
164
  var hint = formGroup.childNodes[1];
165
165
  expect(hint.tagName).toEqual('DIV');
166
166
  expect(hint.classList).toContain('govuk-hint');
@@ -174,7 +174,7 @@ describe('components.CollectionPage', function () {
174
174
  })[0];
175
175
  expect(input.classList).toContain('hods-autocomplete__input');
176
176
  expect(input.tagName).toEqual('INPUT');
177
- expect(input.id).toEqual(fieldId);
177
+ expect(input.id).toEqual("".concat(COLLECTION_NAME, "[0].").concat(fieldId));
178
178
  expect(input.value).toEqual(val);
179
179
  return input;
180
180
  };
@@ -6,8 +6,11 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
8
8
  var setDataItem = function setDataItem(data, fieldId, value) {
9
+ var handleArrayIndices = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
9
10
  if (fieldId && data) {
10
- var parts = fieldId.split('.');
11
+ var parts = handleArrayIndices ?
12
+ // Replace all occurrences of '[' with '.' and ']' with ''.
13
+ fieldId.replace(/\[/g, '.').replace(/\]/g, '').split('.') : fieldId.split('.');
11
14
  var leaf = parts.pop();
12
15
  var node = data;
13
16
  parts.forEach(function (part) {
@@ -107,4 +107,29 @@ describe('utils.Data.setDataItem', function () {
107
107
  }
108
108
  });
109
109
  });
110
+ it('should handle array indices in fieldPath if handleArrayIndices is true', function () {
111
+ var DATA = {
112
+ alpha: {
113
+ bravo: [{
114
+ charlie: '123'
115
+ }]
116
+ }
117
+ };
118
+ var FIELD_ID = 'alpha.bravo[0].charlie';
119
+ var VALUE = '456';
120
+ expect((0, _setDataItem.default)(DATA, FIELD_ID, VALUE, true)).toMatchObject({
121
+ alpha: {
122
+ bravo: [{
123
+ charlie: VALUE
124
+ }]
125
+ }
126
+ });
127
+ expect(DATA).toMatchObject({
128
+ alpha: {
129
+ bravo: [{
130
+ charlie: VALUE
131
+ }]
132
+ }
133
+ });
134
+ });
110
135
  });
@@ -15,7 +15,9 @@ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key i
15
15
  function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
16
16
  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); }
17
17
  var setDefaultDateValue = function setDefaultDateValue(date, data, fullPath) {
18
- var val = date.defaultValue === 'today' ? (0, _dayjs.default)().format('DD-MM-YYYY') : date.defaultValue;
18
+ var dateFormat = /^(\d{1,2})-\d{1,2}-(\d{4})$/;
19
+ var defaultDate = dateFormat.test(date.defaultValue) ? date.defaultValue : (0, _getSourceData.default)(data, date.defaultValue);
20
+ var val = date.defaultValue === 'today' ? (0, _dayjs.default)().format('DD-MM-YYYY') : defaultDate;
19
21
  (0, _setDataItem.default)(data, "".concat(fullPath).concat(date.fieldId), val);
20
22
  };
21
23
 
@@ -405,6 +405,29 @@ describe('utils', function () {
405
405
  }
406
406
  });
407
407
  });
408
+ it('should handle default values given it is a date with a dot-separated field identifier within pages', function () {
409
+ var PAGES = [{
410
+ components: [{
411
+ fieldId: 'pageFieldA',
412
+ type: 'date',
413
+ defaultValue: 'test.a'
414
+ }, {
415
+ fieldId: 'pageFieldB',
416
+ type: 'date',
417
+ defaultValue: 'test.b'
418
+ }]
419
+ }];
420
+ var COMPONENTS = [];
421
+ var DATA = {
422
+ 'test': {
423
+ 'a': '11-04-2023',
424
+ 'b': '12-5-2023'
425
+ }
426
+ };
427
+ var RESULT = (0, _setupFormData.default)(PAGES, COMPONENTS, DATA);
428
+ expect(RESULT.pageFieldA).toEqual('11-04-2023');
429
+ expect(RESULT.pageFieldB).toEqual('12-5-2023');
430
+ });
408
431
  });
409
432
  });
410
433
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "5.2.1",
3
+ "version": "5.5.0",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",