@ukhomeoffice/cop-react-form-renderer 4.15.1 → 4.16.1-alpha

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.
@@ -65,11 +65,17 @@ var Container = function Container(_ref) {
65
65
  };
66
66
 
67
67
  var shouldShow = function shouldShow(options) {
68
+ var full_path = "".concat(container.full_path || container.fieldId, ".").concat(options.fieldId);
69
+
68
70
  if (options.type === _models.ComponentTypes.CONTAINER) {
69
- return _utils.default.Container.show(options, formData);
71
+ return _utils.default.Container.show(_objectSpread(_objectSpread({}, options), {}, {
72
+ full_path: full_path
73
+ }), formData);
70
74
  }
71
75
 
72
- return _utils.default.Component.show(options, formData);
76
+ return _utils.default.Component.show(_objectSpread(_objectSpread({}, options), {}, {
77
+ full_path: full_path
78
+ }), formData);
73
79
  };
74
80
 
75
81
  var classes = _utils.default.classBuilder(DEFAULT_CLASS, [], container.className);
@@ -80,10 +86,12 @@ var Container = function Container(_ref) {
80
86
  }), container.components && container.components.filter(shouldShow).map(function (component, index) {
81
87
  var defaultValue = component.type === _models.ComponentTypes.FILE ? {} : '';
82
88
  var val = value ? value[component.fieldId] : defaultValue;
89
+ var full_path = "".concat(container.full_path || container.fieldId, ".").concat(component.fieldId);
83
90
  return /*#__PURE__*/_react.default.createElement(_FormComponent.default, _extends({
84
91
  key: index
85
92
  }, attrs, {
86
93
  component: _objectSpread(_objectSpread({}, component), {}, {
94
+ full_path: full_path,
87
95
  readonly: container.readonly ? true : component.readonly
88
96
  }),
89
97
  formData: formData,
@@ -80,7 +80,7 @@ describe('components.FormComponent.Container', function () {
80
80
  expect(label.tagName).toEqual('LABEL');
81
81
  expect(label.classList).toContain('govuk-label');
82
82
  expect(label.textContent).toEqual("".concat(TEXT_COMPONENT.label, " (optional)"));
83
- expect(label.getAttribute('for')).toEqual(TEXT_ID);
83
+ expect(label.getAttribute('for')).toEqual("".concat(ID, ".").concat(TEXT_ID));
84
84
  hint = formGroup.childNodes[1];
85
85
  expect(hint.tagName).toEqual('SPAN');
86
86
  expect(hint.classList).toContain('govuk-hint');
@@ -88,7 +88,7 @@ describe('components.FormComponent.Container', function () {
88
88
  input = formGroup.childNodes[2];
89
89
  expect(input.tagName).toEqual('INPUT');
90
90
  expect(input.classList).toContain('govuk-input');
91
- expect(input.id).toEqual(TEXT_ID);
91
+ expect(input.id).toEqual("".concat(ID, ".").concat(TEXT_ID));
92
92
  expect(input.value).toEqual(TEXT_VALUE);
93
93
 
94
94
  case 22:
@@ -137,7 +137,7 @@ describe('components.FormComponent.Container', function () {
137
137
  expect(label.tagName).toEqual('LABEL');
138
138
  expect(label.classList).toContain('govuk-label');
139
139
  expect(label.textContent).toEqual("".concat(TEXT_COMPONENT.label, " (optional)"));
140
- expect(label.getAttribute('for')).toEqual(TEXT_ID);
140
+ expect(label.getAttribute('for')).toEqual("".concat(ID, ".").concat(NESTED_ID, ".").concat(TEXT_ID));
141
141
  hint = formGroup.childNodes[1];
142
142
  expect(hint.tagName).toEqual('SPAN');
143
143
  expect(hint.classList).toContain('govuk-hint');
@@ -145,7 +145,7 @@ describe('components.FormComponent.Container', function () {
145
145
  input = formGroup.childNodes[2];
146
146
  expect(input.tagName).toEqual('INPUT');
147
147
  expect(input.classList).toContain('govuk-input');
148
- expect(input.id).toEqual(TEXT_ID);
148
+ expect(input.id).toEqual("".concat(ID, ".").concat(NESTED_ID, ".").concat(TEXT_ID));
149
149
  expect(input.value).toEqual(NESTED_TEXT_VALUE);
150
150
 
151
151
  case 25:
@@ -318,7 +318,7 @@ describe('components.FormComponent.Container', function () {
318
318
  expect(label.tagName).toEqual('LABEL');
319
319
  expect(label.classList).toContain('govuk-label');
320
320
  expect(label.textContent).toEqual("".concat(TEXT_COMPONENT.label, " (optional)"));
321
- expect(label.getAttribute('for')).toEqual(TEXT_ID);
321
+ expect(label.getAttribute('for')).toEqual("".concat(ID, ".").concat(TEXT_ID));
322
322
  hint = formGroup.childNodes[1];
323
323
  expect(hint.tagName).toEqual('SPAN');
324
324
  expect(hint.classList).toContain('govuk-hint');
@@ -326,7 +326,7 @@ describe('components.FormComponent.Container', function () {
326
326
  input = formGroup.childNodes[2];
327
327
  expect(input.tagName).toEqual('INPUT');
328
328
  expect(input.classList).toContain('govuk-input');
329
- expect(input.id).toEqual(TEXT_ID);
329
+ expect(input.id).toEqual("".concat(ID, ".").concat(TEXT_ID));
330
330
  expect(input.value).toEqual('');
331
331
 
332
332
  case 22:
@@ -400,7 +400,7 @@ describe('components.FormComponent.Container', function () {
400
400
  expect(label.tagName).toEqual('LABEL');
401
401
  expect(label.classList).toContain('govuk-label');
402
402
  expect(label.textContent).toEqual("".concat(TEXT_COMPONENT.label, " (optional)"));
403
- expect(label.getAttribute('for')).toEqual(TEXT_ID);
403
+ expect(label.getAttribute('for')).toEqual("".concat(ID, ".").concat(TEXT_ID));
404
404
  hint = formGroup.childNodes[1];
405
405
  expect(hint.tagName).toEqual('SPAN');
406
406
  expect(hint.classList).toContain('govuk-hint');
@@ -410,7 +410,7 @@ describe('components.FormComponent.Container', function () {
410
410
 
411
411
  expect(input.classList).toContain('hods-readonly'); // Now readonly, so hods-readonly
412
412
 
413
- expect(input.id).toEqual(TEXT_ID);
413
+ expect(input.id).toEqual("".concat(ID, ".").concat(TEXT_ID));
414
414
  expect(input.textContent).toEqual(TEXT_VALUE);
415
415
 
416
416
  case 22:
@@ -90,6 +90,7 @@ var FormComponent = function FormComponent(_ref) {
90
90
  var target = _ref2.target;
91
91
 
92
92
  if (typeof onChange === 'function') {
93
+ target = (0, _helpers.addLabel)(target, component, data);
93
94
  onChange({
94
95
  target: target
95
96
  });
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ 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); }
9
+
10
+ /**
11
+ * Adds label to target for given value.
12
+ * @param {object} target The target to be updated.
13
+ * @returns The updated target.
14
+ */
15
+ var addLabel = function addLabel(target, component, data) {
16
+ // target.value can be an object in which case use value in object attribute
17
+ var value = _typeof(target.value) === 'object' ? target.value.value : target.value; // find the reference data item using the value
18
+
19
+ var item = data.find(function (e) {
20
+ return e.id === value;
21
+ }); // if item is null it means that the target.value can be used as a label otherwise use item.label
22
+
23
+ target['label'] = item === undefined ? target.value : item.label;
24
+ target['component'] = component.cya_label;
25
+ return target;
26
+ };
27
+
28
+ var _default = addLabel;
29
+ exports.default = _default;
@@ -3,6 +3,12 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ Object.defineProperty(exports, "addLabel", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _addLabel.default;
10
+ }
11
+ });
6
12
  Object.defineProperty(exports, "getComponentDisabled", {
7
13
  enumerable: true,
8
14
  get: function get() {
@@ -20,4 +26,6 @@ var _getComponentDisabled = _interopRequireDefault(require("./getComponentDisabl
20
26
 
21
27
  var _getComponentError = _interopRequireDefault(require("./getComponentError"));
22
28
 
29
+ var _addLabel = _interopRequireDefault(require("./addLabel"));
30
+
23
31
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -68,6 +68,11 @@ var FormPage = function FormPage(_ref) {
68
68
 
69
69
  var _useValidation = (0, _hooks.useValidation)(),
70
70
  errors = _useValidation.errors;
71
+
72
+ var _useState3 = (0, _react.useState)({}),
73
+ _useState4 = _slicedToArray(_useState3, 2),
74
+ patchLabel = _useState4[0],
75
+ setPatchLabel = _useState4[1];
71
76
  /**
72
77
  * Handle the state of the data directly within the page.
73
78
  * This is so that the overall form data isn't affected until such
@@ -81,6 +86,9 @@ var FormPage = function FormPage(_ref) {
81
86
  setPatch(function (prev) {
82
87
  return _objectSpread(_objectSpread({}, prev), {}, _defineProperty({}, target.name, target.value));
83
88
  });
89
+ setPatchLabel(function (prev) {
90
+ return _objectSpread(_objectSpread({}, prev), {}, _defineProperty({}, target.component, target.label));
91
+ });
84
92
  };
85
93
 
86
94
  var classes = _utils.default.classBuilder(classBlock, classModifiers, className);
@@ -112,7 +120,7 @@ var FormPage = function FormPage(_ref) {
112
120
  return _utils.default.Component.show(action, page.formData);
113
121
  }),
114
122
  onAction: function onAction(action) {
115
- return _onAction(action, patch);
123
+ return _onAction(action, patch, patchLabel);
116
124
  }
117
125
  }));
118
126
  };
@@ -292,8 +292,11 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
292
292
  (0, _react.useEffect)(function () {
293
293
  if (newPageId !== undefined) setPageId(newPageId);
294
294
  }, [newPageId]); // Handle actions from pages.
295
+ // (patch captures payload ready field name and value,
296
+ // patchLabel captures non ID values
297
+ // for display purposes after submission.)
295
298
 
296
- var onPageAction = function onPageAction(action, patch) {
299
+ var onPageAction = function onPageAction(action, patch, patchLabel) {
297
300
  if (action.type === _models.PageAction.TYPES.CANCEL) {
298
301
  hooks.onCancel();
299
302
  return;
@@ -370,15 +373,16 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
370
373
 
371
374
  if (patch) {
372
375
  setData(submissionData);
373
- } // In case of hub-and-spoke if patch has changed then save field name and value to variables for call to onSubmit hook
376
+ } // In case of hub-and-spoke if patchLabel has changed then
377
+ // save name and value to variables for call to onSubmit hook
374
378
 
375
379
 
376
- var changedFieldName = undefined;
377
- var changedFieldValue = undefined;
380
+ var changedFieldName;
381
+ var changedFieldValue;
378
382
 
379
- if (type === _models.FormTypes.HUB && Object.keys(patch).length > 0) {
380
- changedFieldName = formState.page.components[0].cya_label;
381
- changedFieldValue = Object.values(patch)[0];
383
+ if (type === _models.FormTypes.HUB && Object.keys(patchLabel).length > 0) {
384
+ changedFieldName = Object.keys(patchLabel)[0];
385
+ changedFieldValue = Object.values(patchLabel)[0];
382
386
  } // Now submit the data to the backend...
383
387
 
384
388
 
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ /**
9
+ * Iterates over an array of errors and brings any nested
10
+ * error that should be displayed into the top level array.
11
+ *
12
+ * e.g.
13
+ * [
14
+ * null,
15
+ * null,
16
+ * [
17
+ * { error },
18
+ * { error },
19
+ * { error },
20
+ * ]
21
+ * ]
22
+ *
23
+ * This allows us to have containers components, whose components are children
24
+ * containers and those children container components are also made up of
25
+ * containers.
26
+ *
27
+ * e.g.
28
+ * {
29
+ * "type": "container",
30
+ * "components": [
31
+ * {
32
+ * "type": "any-type"
33
+ * },
34
+ * {
35
+ * "type": "container",
36
+ * "components": [
37
+ * {
38
+ * "type": "any-type"
39
+ * },
40
+ * {
41
+ * "type": "container",
42
+ * "components": [
43
+ * {
44
+ * "type": "container",
45
+ * "components":[]
46
+ * }
47
+ * ]
48
+ * }
49
+ * ]
50
+ * }
51
+ * ]
52
+ * }
53
+ *
54
+ *
55
+ * @param {*} errors A array of nested arrays containing errors.
56
+ * @returns An array containing error objects or an empty array when no errors are found.
57
+ */
58
+ var elevateErrors = function elevateErrors(errors) {
59
+ var nestedErrors = [];
60
+
61
+ var getError = function getError(errors) {
62
+ errors.forEach(function (error) {
63
+ if (error) {
64
+ if (Array.isArray(error)) {
65
+ getError(error);
66
+ } else {
67
+ // Must be an error if not an array
68
+ nestedErrors.push(error);
69
+ }
70
+ }
71
+ });
72
+ };
73
+
74
+ getError(errors);
75
+ return nestedErrors;
76
+ };
77
+
78
+ var _default = elevateErrors;
79
+ exports.default = _default;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ var _elevateErrors = _interopRequireDefault(require("./elevateErrors"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ describe('utils.Validate.ElevateErrors', function () {
8
+ it('should get nested errors wrapped within multiple arrays', function () {
9
+ var nestedErrors = [[undefined, undefined, [[{
10
+ "type": 'This Alpha error is required'
11
+ }, [{
12
+ "type": 'This Bravo error is required'
13
+ }, [[], {
14
+ "type": 'This Charlie error is required'
15
+ }]]]]]];
16
+ expect((0, _elevateErrors.default)(nestedErrors)).toHaveLength(3);
17
+ expect((0, _elevateErrors.default)(nestedErrors)).toMatchObject([{
18
+ "type": 'This Alpha error is required'
19
+ }, {
20
+ "type": 'This Bravo error is required'
21
+ }, {
22
+ "type": 'This Charlie error is required'
23
+ }]);
24
+ });
25
+ it('should get the single error within an array', function () {
26
+ var nestedErrors = [[{
27
+ "type": 'This Alpha error is required'
28
+ }]];
29
+ expect((0, _elevateErrors.default)(nestedErrors)).toHaveLength(1);
30
+ expect((0, _elevateErrors.default)(nestedErrors)).toMatchObject([{
31
+ "type": 'This Alpha error is required'
32
+ }]);
33
+ });
34
+ });
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _elevateErrors = _interopRequireDefault(require("./elevateErrors"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ var Errors = {
13
+ elevate: _elevateErrors.default
14
+ };
15
+ var _default = Errors;
16
+ exports.default = _default;
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
+ var _elevateErrors = _interopRequireDefault(require("./errors/elevateErrors"));
9
+
8
10
  var _validateComponent = _interopRequireDefault(require("./validateComponent"));
9
11
 
10
12
  var _additional = _interopRequireDefault(require("./additional"));
@@ -44,9 +46,9 @@ var validateContainer = function validateContainer(container, outerData, formDat
44
46
  });
45
47
  }
46
48
 
47
- return errors.filter(function (e) {
49
+ return (0, _elevateErrors.default)(errors.filter(function (e) {
48
50
  return !!e;
49
- });
51
+ }));
50
52
  };
51
53
 
52
54
  var _default = validateContainer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.15.1",
3
+ "version": "4.16.1-alpha",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",