@ukhomeoffice/cop-react-form-renderer 4.13.1-alpha → 4.15.0-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.
@@ -434,7 +434,7 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
434
434
  }
435
435
  } else if (currentTask.state === _models.TaskStates.TYPES.IN_PROGRESS) {
436
436
  var _currentPage = data.formStatus.tasks[currentTask.name].currentPage;
437
- onPageChange(_currentPage || currentTask.pages[0]);
437
+ onPageChange(_models.FormPages.CYA || currentTask.pages[0]);
438
438
  } else {
439
439
  onPageChange(currentTask.pages[0]);
440
440
  }
@@ -25,23 +25,12 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
25
25
 
26
26
  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); }
27
27
 
28
- var setupChildrenJSX = function setupChildrenJSX(options, config) {
29
- options.forEach(function (option) {
30
- if (!Array.isArray(option.nested)) {
31
- return;
32
- }
33
-
34
- option.children = getChildrenJsx(config, option.nested);
35
- });
36
- };
37
28
  /**
38
29
  * Separate function for each component type for the sake of
39
30
  * code clarity - having the additional bits and pieces in the
40
31
  * switch statement increases the cyclomatic complexity and
41
32
  * makes it much harder to follow what's going on.
42
33
  */
43
-
44
-
45
34
  var getAutocomplete = function getAutocomplete(config) {
46
35
  var attrs = (0, _cleanAttributes.default)(config);
47
36
 
@@ -59,7 +48,6 @@ var getCheckboxes = function getCheckboxes(config) {
59
48
  options = val;
60
49
  });
61
50
 
62
- setupChildrenJSX(options, config);
63
51
  var attrs = (0, _cleanAttributes.default)(config);
64
52
  return /*#__PURE__*/_react.default.createElement(_copReactComponents.Checkboxes, _extends({}, attrs, {
65
53
  options: options
@@ -118,7 +106,13 @@ var getRadios = function getRadios(config) {
118
106
  options = val;
119
107
  });
120
108
 
121
- setupChildrenJSX(options, config);
109
+ options.forEach(function (option) {
110
+ if (!Array.isArray(option.nested)) {
111
+ return;
112
+ }
113
+
114
+ option.children = getChildrenJsx(config, option.nested);
115
+ });
122
116
  var attrs = (0, _cleanAttributes.default)(config);
123
117
  return /*#__PURE__*/_react.default.createElement(_copReactComponents.Radios, _extends({}, attrs, {
124
118
  options: options
@@ -11,6 +11,47 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
11
11
 
12
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
13
 
14
+ var getComponentToUse = function getComponentToUse(toUse, formComponents) {
15
+ var path = toUse.use.split('.');
16
+ var firstId = path.shift();
17
+
18
+ var wrapper = _objectSpread({}, formComponents.find(function (fc) {
19
+ return fc.id === firstId;
20
+ }));
21
+
22
+ var parent = wrapper;
23
+
24
+ var _loop = function _loop() {
25
+ var id = path.shift();
26
+
27
+ if (Array.isArray(parent.components)) {
28
+ // Filter the parent components down to just the one referenced
29
+ // by the current id.
30
+ parent.components = parent.components.filter(function (c) {
31
+ return c.id === id;
32
+ });
33
+
34
+ if (parent.components.length > 0) {
35
+ // Make the found child the parent so we can iterate.
36
+ parent = parent.components[0];
37
+ } else {
38
+ // If no child was found, there can be no parent.
39
+ parent = undefined;
40
+ }
41
+ } else {
42
+ // We cannot find the child with this id if there are no child
43
+ // components on this parent.
44
+ parent = undefined;
45
+ }
46
+ };
47
+
48
+ while (parent && path.length > 0) {
49
+ _loop();
50
+ } // If we missed a nested reference along the way, don't return the wrapper.
51
+
52
+
53
+ return parent ? wrapper : undefined;
54
+ };
14
55
  /**
15
56
  * Use an existing component from the formComponents, overriding any properties
16
57
  * where appropriate.
@@ -18,17 +59,19 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
18
59
  * @param {Array} formComponents An array of existing components on the form.
19
60
  * @returns A component configuration object.
20
61
  */
62
+
63
+
21
64
  var useComponent = function useComponent(toUse, formComponents) {
22
- var formComponent = toUse ? formComponents.find(function (fc) {
23
- return fc.id === toUse.use;
24
- }) : undefined;
25
-
26
- if (formComponent) {
27
- var fieldId = toUse.fieldId || formComponent.fieldId;
28
- return _objectSpread(_objectSpread(_objectSpread({}, formComponent), toUse), {}, {
29
- cya_label: formComponent.label || formComponent.cya_label,
30
- fieldId: fieldId
31
- });
65
+ if (toUse) {
66
+ var component = getComponentToUse(toUse, formComponents);
67
+
68
+ if (component) {
69
+ var fieldId = toUse.fieldId || component.fieldId;
70
+ return _objectSpread(_objectSpread(_objectSpread({}, component), toUse), {}, {
71
+ cya_label: component.label || component.cya_label,
72
+ fieldId: fieldId
73
+ });
74
+ }
32
75
  }
33
76
 
34
77
  return _objectSpread({}, toUse);
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
 
3
+ var _models = require("../../models");
4
+
3
5
  var _useComponent = _interopRequireDefault(require("./useComponent"));
4
6
 
5
7
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -13,27 +15,45 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
13
15
  describe('utils', function () {
14
16
  describe('FormPage', function () {
15
17
  describe('useComponent', function () {
18
+ var ZULU = {
19
+ id: 'z',
20
+ fieldId: 'z',
21
+ label: 'Zulu',
22
+ type: _models.ComponentTypes.TEXT
23
+ };
24
+ var CONTAINER = {
25
+ id: 'container',
26
+ fieldId: 'container',
27
+ label: 'Container',
28
+ type: _models.ComponentTypes.CONTAINER,
29
+ components: [{
30
+ id: 'f',
31
+ fieldId: 'f',
32
+ label: 'Foxtrot',
33
+ type: _models.ComponentTypes.TEXT
34
+ }, ZULU]
35
+ };
16
36
  var FORM_COMPONENTS = [{
17
37
  id: 'a',
18
38
  fieldId: 'a',
19
39
  label: 'Alpha',
20
- type: 'text'
40
+ type: _models.ComponentTypes.TEXT
21
41
  }, {
22
42
  id: 'b',
23
43
  fieldId: 'b',
24
44
  label: 'Bravo',
25
- type: 'text'
45
+ type: _models.ComponentTypes.TEXT
26
46
  }, {
27
47
  id: 'c',
28
48
  fieldId: 'c',
29
49
  label: 'Charlie',
30
- type: 'text'
50
+ type: _models.ComponentTypes.TEXT
31
51
  }, {
32
52
  id: 'd',
33
53
  fieldId: 'd',
34
54
  cya_label: 'Delta',
35
- type: 'text'
36
- }];
55
+ type: _models.ComponentTypes.TEXT
56
+ }, CONTAINER];
37
57
  it('should handle a null toUse configuration', function () {
38
58
  var TO_USE = null;
39
59
  expect((0, _useComponent.default)(TO_USE, FORM_COMPONENTS)).toEqual({});
@@ -107,6 +127,43 @@ describe('utils', function () {
107
127
  label: TO_USE.label
108
128
  }));
109
129
  });
130
+ it('should handle a container', function () {
131
+ var TO_USE = {
132
+ use: CONTAINER.id
133
+ };
134
+ expect((0, _useComponent.default)(TO_USE, FORM_COMPONENTS)).toEqual(_objectSpread(_objectSpread({
135
+ use: CONTAINER.id
136
+ }, CONTAINER), {}, {
137
+ cya_label: CONTAINER.label
138
+ }));
139
+ });
140
+ it('should handle a component nested inside a container', function () {
141
+ var ID = "".concat(CONTAINER.id, ".").concat(ZULU.id);
142
+ var TO_USE = {
143
+ use: ID
144
+ };
145
+ expect((0, _useComponent.default)(TO_USE, FORM_COMPONENTS)).toEqual(_objectSpread(_objectSpread({
146
+ use: ID
147
+ }, CONTAINER), {}, {
148
+ cya_label: CONTAINER.label,
149
+ components: [ZULU // Foxtrot is not included
150
+ ]
151
+ }));
152
+ });
153
+ it('should handle an unrecognised container reference', function () {
154
+ var ID = "unrecognised.".concat(ZULU.id);
155
+ var TO_USE = {
156
+ use: ID
157
+ };
158
+ expect((0, _useComponent.default)(TO_USE, FORM_COMPONENTS)).toEqual(TO_USE);
159
+ });
160
+ it('should handle an unrecognised component within a container', function () {
161
+ var ID = "".concat(CONTAINER.id, ".unrecognised");
162
+ var TO_USE = {
163
+ use: ID
164
+ };
165
+ expect((0, _useComponent.default)(TO_USE, FORM_COMPONENTS)).toEqual(TO_USE);
166
+ });
110
167
  });
111
168
  });
112
169
  });
@@ -79,7 +79,6 @@ var validateComponent = function validateComponent(component, outerData, formDat
79
79
  error = message;
80
80
  break;
81
81
 
82
- case _models.ComponentTypes.CHECKBOXES:
83
82
  case _models.ComponentTypes.RADIOS:
84
83
  var nestedErrors = [];
85
84
  (_component$data$optio = component.data.options) === null || _component$data$optio === void 0 ? void 0 : _component$data$optio.forEach(function (option) {
@@ -200,19 +200,19 @@ describe('utils.Validate.Component', function () {
200
200
  });
201
201
  });
202
202
  describe('when the component has a nested component', function () {
203
- it.each([['radios', 'text'], ['checkboxes', 'text']])('should return no error when the radio component contains nested components without errors', function (parentType, nestedType) {
203
+ it('should return no error when the radio component contains nested components without errors', function () {
204
204
  var NESTED_ID = 'nestedId';
205
205
  var NESTED_VALUE = 'nestedValue';
206
206
 
207
207
  var FORMDATA = _defineProperty({}, NESTED_ID, NESTED_VALUE);
208
208
 
209
209
  var COMPONENT = {
210
- type: parentType,
210
+ type: 'radios',
211
211
  id: 'a',
212
212
  data: {
213
213
  options: [{
214
214
  nested: [{
215
- type: nestedType,
215
+ type: 'text',
216
216
  fieldId: NESTED_ID,
217
217
  id: NESTED_ID,
218
218
  required: true
@@ -222,16 +222,16 @@ describe('utils.Validate.Component', function () {
222
222
  };
223
223
  expect((0, _validateComponent.default)(COMPONENT, undefined, FORMDATA)).toBeUndefined();
224
224
  });
225
- it.each([['radios', 'text'], ['checkboxes', 'text']])('should return an error when the radio component contains nested components with errors', function (parentType, nestedType) {
225
+ it('should return an error when the radio component contains nested components with errors', function () {
226
226
  var NESTED_ID = 'nestedId';
227
227
  var FORMDATA = {};
228
228
  var COMPONENT = {
229
- type: parentType,
229
+ type: 'radios',
230
230
  id: 'a',
231
231
  data: {
232
232
  options: [{
233
233
  nested: [{
234
- type: nestedType,
234
+ type: 'text',
235
235
  fieldId: NESTED_ID,
236
236
  id: NESTED_ID,
237
237
  required: true
@@ -244,10 +244,10 @@ describe('utils.Validate.Component', function () {
244
244
  error: "Field is required"
245
245
  }]);
246
246
  });
247
- it.each([['radios'], ['checkboxes']])('should return no error when a non selected radio component contains nested components with errors', function (type) {
247
+ it('should return no error when a non selected radio component contains nested components with errors', function () {
248
248
  var NESTED_ID = 'nestedId';
249
249
  var COMPONENT = {
250
- type: type,
250
+ type: 'radios',
251
251
  id: 'a',
252
252
  data: {
253
253
  options: [{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.13.1-alpha",
3
+ "version": "4.15.0-alpha",
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.2.3-alpha",
19
+ "@ukhomeoffice/cop-react-components": "2.2.2",
20
20
  "axios": "^0.23.0",
21
21
  "dayjs": "^1.11.0",
22
22
  "govuk-frontend": "^4.3.1",