@ukhomeoffice/cop-react-form-renderer 4.13.0 → 4.14.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.
@@ -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
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.13.0",
3
+ "version": "4.14.0",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",