@ukhomeoffice/cop-react-form-renderer 4.22.3 → 4.22.4-charlie

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.
@@ -7,6 +7,8 @@ exports.default = void 0;
7
7
 
8
8
  var _models = require("../../models");
9
9
 
10
+ var _elevateNestedComponents = _interopRequireDefault(require("../Component/elevateNestedComponents"));
11
+
10
12
  var _getCYARow = _interopRequireDefault(require("./getCYARow"));
11
13
 
12
14
  var _getCYARowsForCollection = _interopRequireDefault(require("./getCYARowsForCollection"));
@@ -23,7 +25,8 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
23
25
 
24
26
  var getCYARowsForContainer = function getCYARowsForContainer(page, container, formData, onAction) {
25
27
  if ((0, _showComponentCYA.default)(container, page.formData)) {
26
- return container.components.filter(function (c) {
28
+ var allComponents = (0, _elevateNestedComponents.default)(container.components, formData);
29
+ return allComponents.filter(function (c) {
27
30
  return (0, _showComponentCYA.default)(c, page.formData);
28
31
  }).flatMap(function (component) {
29
32
  var fd = formData ? formData[component.fieldId] : undefined;
@@ -320,4 +320,51 @@ describe('utils.CheckYourAnswers.getCYARowsForContainer', function () {
320
320
  checkTitleRow(1, ROWS[2]);
321
321
  checkValueRow(1, ROWS[3], 'Charlie');
322
322
  });
323
+ it('should get the appropriate number for rows components with nested components inside a container', function () {
324
+ var FORM_DATA = {
325
+ container: {
326
+ a: 'B',
327
+ nested: 'Bravo'
328
+ }
329
+ };
330
+ var PAGE = {
331
+ id: 'page',
332
+ formData: FORM_DATA,
333
+ cya_link: {}
334
+ };
335
+ var NESTED_COMPONENT = {
336
+ id: 'nested',
337
+ fieldId: 'nested',
338
+ type: _models.ComponentTypes.TEXT
339
+ };
340
+ var COMPONENT = {
341
+ type: 'radios',
342
+ id: 'a',
343
+ fieldId: 'a',
344
+ label: 'Alpha',
345
+ data: {
346
+ options: [{
347
+ value: 'B',
348
+ label: 'B',
349
+ nested: [NESTED_COMPONENT]
350
+ }, {
351
+ value: 'A',
352
+ label: 'A'
353
+ }]
354
+ }
355
+ };
356
+ var CONTAINER = {
357
+ id: 'container',
358
+ fieldId: 'container',
359
+ type: _models.ComponentTypes.CONTAINER,
360
+ components: [COMPONENT],
361
+ value: FORM_DATA.container,
362
+ formData: FORM_DATA
363
+ };
364
+
365
+ var ON_ACTION = function ON_ACTION() {};
366
+
367
+ var ROWS = (0, _getCYARowsForContainer.default)(PAGE, CONTAINER, FORM_DATA.container, ON_ACTION);
368
+ expect(ROWS.length).toEqual(2);
369
+ });
323
370
  });
@@ -5,6 +5,10 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
+ var _Data = _interopRequireDefault(require("../Data"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
8
12
  /**
9
13
  * Iterates over an array of components and brings any nested
10
14
  * components that should be visible into the top level array.
@@ -40,7 +44,9 @@ var elevateNestedComponents = function elevateNestedComponents(components, data)
40
44
  (_component$data = component.data) === null || _component$data === void 0 ? void 0 : (_component$data$optio = _component$data.options) === null || _component$data$optio === void 0 ? void 0 : _component$data$optio.forEach(function (option) {
41
45
  // If this option has nested components and is
42
46
  // selected, then add its nested components to the array.
43
- if (Array.isArray(option.nested) && data[component.id] === option.value) {
47
+ var sourceData = (data === null || data === void 0 ? void 0 : data[component.id]) || _Data.default.getSource(data, component.full_path);
48
+
49
+ if (Array.isArray(option.nested) && sourceData === option.value) {
44
50
  allComponents = allComponents.concat(option.nested);
45
51
  }
46
52
  });
@@ -89,4 +89,36 @@ describe('utils.Component.elevateNestedComponents', function () {
89
89
  id: 'nestedTwo'
90
90
  }]));
91
91
  });
92
+ it('should return the original array with elevated nested components when a component has a full_path', function () {
93
+ var COMPONENTS = [{
94
+ id: 'textComp'
95
+ }, {
96
+ id: 'radiosComp',
97
+ data: {
98
+ options: [{
99
+ label: 'Yes',
100
+ value: 'yes',
101
+ nested: [{
102
+ id: 'nestedOne'
103
+ }, {
104
+ id: 'nestedTwo'
105
+ }]
106
+ }, {
107
+ label: 'No',
108
+ value: 'no'
109
+ }]
110
+ },
111
+ full_path: 'component.radiosComp'
112
+ }];
113
+ var DATA = {
114
+ component: {
115
+ radiosComp: 'yes'
116
+ }
117
+ };
118
+ expect((0, _elevateNestedComponents.default)(COMPONENTS, DATA)).toEqual([].concat(COMPONENTS, [{
119
+ id: 'nestedOne'
120
+ }, {
121
+ id: 'nestedTwo'
122
+ }]));
123
+ });
92
124
  });
@@ -237,10 +237,15 @@ var getComponentByType = function getComponentByType(config) {
237
237
 
238
238
 
239
239
  var getChildJsx = function getChildJsx(parent, child) {
240
- if (parent.formData) {
241
- var _parent$formData;
240
+ if (parent.full_path) {
241
+ var pathParts = parent.full_path.split('.');
242
+ pathParts.pop();
243
+ pathParts.push(child.fieldId);
244
+ child.full_path = pathParts.join('.');
245
+ }
242
246
 
243
- child.value = parent !== null && parent !== void 0 && (_parent$formData = parent.formData) !== null && _parent$formData !== void 0 && _parent$formData[child.fieldId] ? parent.formData[child.fieldId] : '';
247
+ if (parent.formData) {
248
+ child.value = parent.formData[child.fieldId] || _Data.default.getSource(parent.formData, child.full_path) || '';
244
249
  }
245
250
 
246
251
  if ('readonly' in child) delete child.readonly;
@@ -112,4 +112,90 @@ describe('utils.Component.get', function () {
112
112
  }
113
113
  }, _callee2);
114
114
  })));
115
+ it('should correctly set the value of the nested component within a container', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
116
+ var CONTAINER_ID, ID, FIELD_ID, LABEL, VALUE, PARENT_CONFIG, NESTED_CONFIG, _renderWithValidation2, container, child, label, input;
117
+
118
+ return regeneratorRuntime.wrap(function _callee3$(_context3) {
119
+ while (1) {
120
+ switch (_context3.prev = _context3.next) {
121
+ case 0:
122
+ CONTAINER_ID = 'container-id';
123
+ ID = 'testId';
124
+ FIELD_ID = 'fieldId';
125
+ LABEL = 'Test label';
126
+ VALUE = 'nestedValue';
127
+ PARENT_CONFIG = {
128
+ onChange: function onChange() {},
129
+ formData: _defineProperty({}, CONTAINER_ID, _defineProperty({}, FIELD_ID, VALUE)),
130
+ full_path: "".concat(CONTAINER_ID, ".").concat(FIELD_ID)
131
+ };
132
+ NESTED_CONFIG = [{
133
+ id: ID,
134
+ fieldId: FIELD_ID,
135
+ label: LABEL,
136
+ type: 'text'
137
+ }];
138
+ _renderWithValidation2 = (0, _setupTests.renderWithValidation)((0, _getComponent.getChildrenJsx)(PARENT_CONFIG, NESTED_CONFIG)), container = _renderWithValidation2.container;
139
+ child = container.childNodes[0];
140
+ expect(child.childNodes.length).toEqual(3);
141
+ expect(child.classList).toContain('govuk-form-group');
142
+ label = child.childNodes[0];
143
+ expect(label).toBeDefined();
144
+ expect(label.innerHTML).toContain(LABEL);
145
+ input = child.childNodes[2];
146
+ expect(input.tagName).toEqual('INPUT');
147
+ expect(input.classList).toContain('govuk-input');
148
+ expect(input.id).toEqual("".concat(CONTAINER_ID, ".").concat(FIELD_ID));
149
+ expect(input.value).toEqual(VALUE);
150
+
151
+ case 19:
152
+ case "end":
153
+ return _context3.stop();
154
+ }
155
+ }
156
+ }, _callee3);
157
+ })));
158
+ it('should correctly set the value of the nested component within a collection', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
159
+ var COLLECTION_ID, ID, FIELD_ID, LABEL, VALUE, PARENT_CONFIG, NESTED_CONFIG, _renderWithValidation3, container, child, label, input;
160
+
161
+ return regeneratorRuntime.wrap(function _callee4$(_context4) {
162
+ while (1) {
163
+ switch (_context4.prev = _context4.next) {
164
+ case 0:
165
+ COLLECTION_ID = 'collection-id';
166
+ ID = 'testId';
167
+ FIELD_ID = 'fieldId';
168
+ LABEL = 'Test label';
169
+ VALUE = 'nestedValue';
170
+ PARENT_CONFIG = {
171
+ onChange: function onChange() {},
172
+ formData: _defineProperty({}, COLLECTION_ID, [_defineProperty({}, FIELD_ID, VALUE)]),
173
+ full_path: "".concat(COLLECTION_ID, "[0].").concat(FIELD_ID)
174
+ };
175
+ NESTED_CONFIG = [{
176
+ id: ID,
177
+ fieldId: FIELD_ID,
178
+ label: LABEL,
179
+ type: 'text'
180
+ }];
181
+ _renderWithValidation3 = (0, _setupTests.renderWithValidation)((0, _getComponent.getChildrenJsx)(PARENT_CONFIG, NESTED_CONFIG)), container = _renderWithValidation3.container;
182
+ child = container.childNodes[0];
183
+ expect(child.childNodes.length).toEqual(3);
184
+ expect(child.classList).toContain('govuk-form-group');
185
+ label = child.childNodes[0];
186
+ expect(label).toBeDefined();
187
+ expect(label.innerHTML).toContain(LABEL);
188
+ input = child.childNodes[2];
189
+ expect(input.tagName).toEqual('INPUT');
190
+ expect(input.classList).toContain('govuk-input');
191
+ expect(input.id).toEqual("".concat(COLLECTION_ID, "[0].").concat(FIELD_ID));
192
+ expect(input.value).toEqual(VALUE);
193
+
194
+ case 19:
195
+ case "end":
196
+ return _context4.stop();
197
+ }
198
+ }
199
+ }, _callee4);
200
+ })));
115
201
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.22.3",
3
+ "version": "4.22.4-charlie",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",