@ukhomeoffice/cop-react-form-renderer 3.3.3 → 3.4.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.
@@ -154,7 +154,7 @@ var CheckYourAnswers = function CheckYourAnswers(_ref) {
154
154
  isGroup(page.id) ? hideActionButtons = true : hideActionButtons = noChangeAction;
155
155
  return /*#__PURE__*/_react.default.createElement(_react.Fragment, {
156
156
  key: pageIndex
157
- }, !hide_page_titles && page.title && !isGroup(page.id) && /*#__PURE__*/_react.default.createElement(_copReactComponents.MediumHeading, null, page.title), isGroup(page.id) && /*#__PURE__*/_react.default.createElement("div", {
157
+ }, !hide_page_titles && page.title && !isGroup(page.id) && /*#__PURE__*/_react.default.createElement(_copReactComponents.MediumHeading, null, _utils.default.interpolateString(page.title, page.formData)), isGroup(page.id) && /*#__PURE__*/_react.default.createElement("div", {
158
158
  className: "group-title"
159
159
  }, /*#__PURE__*/_react.default.createElement(_copReactComponents.MediumHeading, null, currentGroup.title || page.title)), /*#__PURE__*/_react.default.createElement(_SummaryList.default, {
160
160
  className: className,
@@ -495,5 +495,117 @@ describe('components', function () {
495
495
  }
496
496
  }, _callee16);
497
497
  })));
498
+ it('should show page components corrently with interpolated title and cya_label, if label is missing', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee18() {
499
+ var _PAGES, _COMPONENTS, T_PAGES, cya, _cya$childNodes5, cyaTitle, cyaChildNode, names, _names$childNodes3, firstName, surname, _firstName$childNodes, label, value;
500
+
501
+ return regeneratorRuntime.wrap(function _callee18$(_context18) {
502
+ while (1) {
503
+ switch (_context18.prev = _context18.next) {
504
+ case 0:
505
+ _PAGES = [].concat(_userProfile.default.pages); // eslint-disable-next-line no-template-curly-in-string
506
+
507
+ _PAGES[0] = _objectSpread(_objectSpread({}, _PAGES[0]), {}, {
508
+ title: 'Alpha ID: ${businessKey}'
509
+ });
510
+ _COMPONENTS = [].concat(_userProfile.default.components);
511
+ _COMPONENTS[0] = _objectSpread(_objectSpread({}, _COMPONENTS[0]), {}, {
512
+ label: undefined,
513
+ required: true,
514
+ // eslint-disable-next-line no-template-curly-in-string
515
+ cya_label: "Text ${currentUser.familyName}"
516
+ });
517
+ T_PAGES = _utils.default.FormPage.getAll(_PAGES, _COMPONENTS, DATA);
518
+ _context18.next = 7;
519
+ return (0, _testUtils.act)( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee17() {
520
+ return regeneratorRuntime.wrap(function _callee17$(_context17) {
521
+ while (1) {
522
+ switch (_context17.prev = _context17.next) {
523
+ case 0:
524
+ (0, _setupTests.renderDomWithValidation)( /*#__PURE__*/_react.default.createElement(_CheckYourAnswers.default, {
525
+ pages: T_PAGES,
526
+ onRowAction: ON_ROW_ACTION,
527
+ onAction: ON_ACTION
528
+ }), container);
529
+
530
+ case 1:
531
+ case "end":
532
+ return _context17.stop();
533
+ }
534
+ }
535
+ }, _callee17);
536
+ })));
537
+
538
+ case 7:
539
+ cya = checkCYA(container);
540
+ _cya$childNodes5 = _slicedToArray(cya.childNodes, 3), cyaTitle = _cya$childNodes5[1], cyaChildNode = _cya$childNodes5[2];
541
+ expect(cyaTitle.textContent).toEqual('Alpha ID: 123456789');
542
+ names = cyaChildNode.childNodes[0];
543
+ expect(names.tagName).toEqual('DL');
544
+ expect(names.classList).toContain("govuk-!-margin-bottom-".concat(_CheckYourAnswers.DEFAULT_MARGIN_BOTTOM));
545
+ _names$childNodes3 = _slicedToArray(names.childNodes, 2), firstName = _names$childNodes3[0], surname = _names$childNodes3[1];
546
+ _firstName$childNodes = _slicedToArray(firstName.childNodes, 2), label = _firstName$childNodes[0], value = _firstName$childNodes[1];
547
+ expect(label.textContent).toEqual('Text Smith');
548
+ checkRow(surname, 'Last name', 'Smith', false);
549
+
550
+ case 17:
551
+ case "end":
552
+ return _context18.stop();
553
+ }
554
+ }
555
+ }, _callee18);
556
+ })));
557
+ it('should show page components corrently with no label, if label and cya_label are missing', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee20() {
558
+ var _PAGES, _COMPONENTS, T_PAGES, cya, _cya$childNodes6, cyaChildNode, names, _names$childNodes4, firstName, surname, _firstName$childNodes2, label, value;
559
+
560
+ return regeneratorRuntime.wrap(function _callee20$(_context20) {
561
+ while (1) {
562
+ switch (_context20.prev = _context20.next) {
563
+ case 0:
564
+ _PAGES = [].concat(_userProfile.default.pages);
565
+ _COMPONENTS = [].concat(_userProfile.default.components);
566
+ _COMPONENTS[0] = _objectSpread(_objectSpread({}, _COMPONENTS[0]), {}, {
567
+ label: undefined,
568
+ required: true,
569
+ cya_label: undefined
570
+ });
571
+ T_PAGES = _utils.default.FormPage.getAll(_PAGES, _COMPONENTS, _objectSpread({}, DATA));
572
+ _context20.next = 6;
573
+ return (0, _testUtils.act)( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee19() {
574
+ return regeneratorRuntime.wrap(function _callee19$(_context19) {
575
+ while (1) {
576
+ switch (_context19.prev = _context19.next) {
577
+ case 0:
578
+ (0, _setupTests.renderDomWithValidation)( /*#__PURE__*/_react.default.createElement(_CheckYourAnswers.default, {
579
+ pages: T_PAGES,
580
+ onRowAction: ON_ROW_ACTION,
581
+ onAction: ON_ACTION
582
+ }), container);
583
+
584
+ case 1:
585
+ case "end":
586
+ return _context19.stop();
587
+ }
588
+ }
589
+ }, _callee19);
590
+ })));
591
+
592
+ case 6:
593
+ cya = checkCYA(container);
594
+ _cya$childNodes6 = _slicedToArray(cya.childNodes, 2), cyaChildNode = _cya$childNodes6[1];
595
+ names = cyaChildNode.childNodes[0];
596
+ expect(names.tagName).toEqual('DL');
597
+ expect(names.classList).toContain("govuk-!-margin-bottom-".concat(_CheckYourAnswers.DEFAULT_MARGIN_BOTTOM));
598
+ _names$childNodes4 = _slicedToArray(names.childNodes, 2), firstName = _names$childNodes4[0], surname = _names$childNodes4[1];
599
+ _firstName$childNodes2 = _slicedToArray(firstName.childNodes, 2), label = _firstName$childNodes2[0], value = _firstName$childNodes2[1];
600
+ expect(label.textContent).toEqual("");
601
+ checkRow(surname, 'Last name', 'Smith', false);
602
+
603
+ case 15:
604
+ case "end":
605
+ return _context20.stop();
606
+ }
607
+ }
608
+ }, _callee20);
609
+ })));
498
610
  });
499
611
  });
@@ -143,6 +143,7 @@ var FormComponent = function FormComponent(_ref) {
143
143
  label: _utils.default.interpolateString(component.label, formData),
144
144
  content: _utils.default.interpolateString(component.content, formData),
145
145
  hint: _utils.default.interpolateString(component.hint, formData),
146
+ cya_label: _utils.default.interpolateString(component.cya_label, formData),
146
147
  options: options,
147
148
  value: value || _utils.default.Component.defaultValue(component),
148
149
  onChange: onComponentChangeExtended,
@@ -203,5 +203,62 @@ describe('components', function () {
203
203
  }
204
204
  }, _callee5);
205
205
  })));
206
+ it('should render a text component appropriately with interpolated label', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() {
207
+ var ID, VALUE, COMPONENT, DATA, ON_CHANGE, _renderWithValidation6, container, formGroup, label, hint, input;
208
+
209
+ return regeneratorRuntime.wrap(function _callee6$(_context6) {
210
+ while (1) {
211
+ switch (_context6.prev = _context6.next) {
212
+ case 0:
213
+ ID = 'component';
214
+ VALUE = 'Text value'; // eslint-disable-next-line no-template-curly-in-string
215
+
216
+ COMPONENT = {
217
+ id: ID,
218
+ fieldId: ID,
219
+ type: 'text',
220
+ label: '${text} Text component',
221
+ cya_label: '${text} Text component',
222
+ hint: 'Text hint'
223
+ };
224
+ DATA = {
225
+ text: 'Interpolated'
226
+ };
227
+
228
+ ON_CHANGE = function ON_CHANGE() {};
229
+
230
+ _renderWithValidation6 = (0, _setupTests.renderWithValidation)( /*#__PURE__*/_react.default.createElement(_FormComponent.default, {
231
+ "data-testid": ID,
232
+ component: COMPONENT,
233
+ value: VALUE,
234
+ onChange: ON_CHANGE,
235
+ formData: DATA
236
+ })), container = _renderWithValidation6.container; // text components are wrapper in a FormGroup by default.
237
+
238
+ formGroup = container.childNodes[0];
239
+ expect(formGroup.tagName).toEqual('DIV');
240
+ expect(formGroup.classList).toContain('govuk-form-group');
241
+ label = formGroup.childNodes[0];
242
+ expect(label.tagName).toEqual('LABEL');
243
+ expect(label.classList).toContain('govuk-label');
244
+ expect(label.textContent).toEqual('Interpolated Text component (optional)');
245
+ expect(label.getAttribute('for')).toEqual(ID);
246
+ hint = formGroup.childNodes[1];
247
+ expect(hint.tagName).toEqual('SPAN');
248
+ expect(hint.classList).toContain('govuk-hint');
249
+ expect(hint.textContent).toEqual(COMPONENT.hint);
250
+ input = formGroup.childNodes[2];
251
+ expect(input.tagName).toEqual('INPUT');
252
+ expect(input.classList).toContain('govuk-input');
253
+ expect(input.id).toEqual(ID);
254
+ expect(input.value).toEqual(VALUE);
255
+
256
+ case 23:
257
+ case "end":
258
+ return _context6.stop();
259
+ }
260
+ }
261
+ }, _callee6);
262
+ })));
206
263
  });
207
264
  });
@@ -73,7 +73,8 @@ var FormRenderer = function FormRenderer(_ref) {
73
73
  className = _ref.className,
74
74
  hide_title = _ref.hide_title,
75
75
  summaryListClassModifiers = _ref.summaryListClassModifiers,
76
- noChangeAction = _ref.noChangeAction;
76
+ noChangeAction = _ref.noChangeAction,
77
+ newPageId = _ref.newPageId;
77
78
  return /*#__PURE__*/_react.default.createElement(_context.HooksContextProvider, {
78
79
  overrides: hooks
79
80
  }, /*#__PURE__*/_react.default.createElement(_context.ValidationContextProvider, null, /*#__PURE__*/_react.default.createElement(InternalFormRenderer, {
@@ -89,7 +90,8 @@ var FormRenderer = function FormRenderer(_ref) {
89
90
  className: className,
90
91
  hide_title: hide_title,
91
92
  summaryListClassModifiers: summaryListClassModifiers,
92
- noChangeAction: noChangeAction
93
+ noChangeAction: noChangeAction,
94
+ newPageId: newPageId
93
95
  })));
94
96
  };
95
97
 
@@ -109,7 +111,8 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
109
111
  className = _ref2.className,
110
112
  hide_title = _ref2.hide_title,
111
113
  summaryListClassModifiers = _ref2.summaryListClassModifiers,
112
- noChangeAction = _ref2.noChangeAction;
114
+ noChangeAction = _ref2.noChangeAction,
115
+ newPageId = _ref2.newPageId;
113
116
 
114
117
  // Set up the initial states.
115
118
  var _useState = (0, _react.useState)({}),
@@ -177,7 +180,10 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
177
180
 
178
181
  (0, _react.useEffect)(function () {
179
182
  setPages(_utils.default.FormPage.getAll(_pages, components, _objectSpread({}, data)));
180
- }, [components, _pages, data, setPages]); // Setup initial pageId.
183
+ }, [components, _pages, data, setPages]);
184
+ (0, _react.useEffect)(function () {
185
+ if (newPageId !== undefined) onPageChange(newPageId);
186
+ }, [newPageId]); // Setup initial pageId.
181
187
 
182
188
  (0, _react.useEffect)(function () {
183
189
  setPageId(function (prev) {
@@ -9,6 +9,7 @@ var TYPE_CHECKBOXES = 'checkboxes';
9
9
  var TYPE_COLLECTION = 'collection';
10
10
  var TYPE_CONTAINER = 'container';
11
11
  var TYPE_DATE = 'date';
12
+ var TYPE_DETAILS = 'details';
12
13
  var TYPE_EMAIL = 'email';
13
14
  var TYPE_FILE = 'file';
14
15
  var TYPE_HEADING = 'heading';
@@ -26,6 +27,7 @@ var ComponentTypes = {
26
27
  COLLECTION: TYPE_COLLECTION,
27
28
  CONTAINER: TYPE_CONTAINER,
28
29
  DATE: TYPE_DATE,
30
+ DETAILS: TYPE_DETAILS,
29
31
  EMAIL: TYPE_EMAIL,
30
32
  FILE: TYPE_FILE,
31
33
  HEADING: TYPE_HEADING,
@@ -5,12 +5,15 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
+ var _copReactComponents = require("@ukhomeoffice/cop-react-components");
9
+
8
10
  var _Component = _interopRequireDefault(require("../Component"));
9
11
 
10
12
  var _getCYAAction = _interopRequireDefault(require("./getCYAAction"));
11
13
 
12
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
15
 
16
+ // Global imports
14
17
  // Local imports
15
18
 
16
19
  /**
@@ -35,7 +38,7 @@ var getCYARow = function getCYARow(page, component, onAction) {
35
38
  id: component.id,
36
39
  fieldId: component.fieldId,
37
40
  full_path: component.full_path,
38
- key: component.label || component.cya_label,
41
+ key: _copReactComponents.Utils.interpolateString(component.label || component.cya_label, page.formData),
39
42
  required: component.required,
40
43
  component: _Component.default.editable(component) ? component : undefined,
41
44
  value: value || '',
@@ -195,6 +195,62 @@ describe('utils', function () {
195
195
  var ROW = (0, _getCYARow.default)(PAGE, COMPONENT, function () {});
196
196
  expect(ROW.component.data.options[0].nested[0].value).toBeUndefined();
197
197
  });
198
+ it('should get an appropriate row for a readonly text component with no value and interpolated label', function () {
199
+ var PAGE = {
200
+ id: 'page',
201
+ formData: {
202
+ text: 'Smith'
203
+ }
204
+ }; // eslint-disable-next-line no-template-curly-in-string
205
+
206
+ var COMPONENT = {
207
+ type: 'text',
208
+ readonly: true,
209
+ id: 'a',
210
+ fieldId: 'a',
211
+ label: 'Alpha ${text}'
212
+ };
213
+
214
+ var ON_ACTION = function ON_ACTION() {};
215
+
216
+ expect((0, _getCYARow.default)(PAGE, COMPONENT, ON_ACTION)).toEqual({
217
+ pageId: PAGE.id,
218
+ id: COMPONENT.id,
219
+ fieldId: COMPONENT.fieldId,
220
+ key: 'Alpha Smith',
221
+ value: '',
222
+ component: COMPONENT,
223
+ action: null
224
+ });
225
+ });
226
+ it('should use the interolated cya_label where there is no label', function () {
227
+ var PAGE = {
228
+ id: 'page',
229
+ formData: {
230
+ a: 'Bravo'
231
+ }
232
+ }; // eslint-disable-next-line no-template-curly-in-string
233
+
234
+ var COMPONENT = {
235
+ type: 'text',
236
+ readonly: true,
237
+ id: 'a',
238
+ fieldId: 'a',
239
+ cya_label: 'CYA Alpha ${a}'
240
+ };
241
+
242
+ var ON_ACTION = function ON_ACTION() {};
243
+
244
+ var ROW = (0, _getCYARow.default)(PAGE, COMPONENT, ON_ACTION);
245
+ (0, _setupTests.expectObjectLike)(ROW, {
246
+ pageId: PAGE.id,
247
+ fieldId: COMPONENT.fieldId,
248
+ key: 'CYA Alpha Bravo',
249
+ action: null,
250
+ component: COMPONENT,
251
+ value: 'Bravo'
252
+ });
253
+ });
198
254
  });
199
255
  });
200
256
  });
@@ -59,6 +59,12 @@ var getDate = function getDate(config) {
59
59
  return /*#__PURE__*/_react.default.createElement(_copReactComponents.DateInput, attrs);
60
60
  };
61
61
 
62
+ var getDetails = function getDetails(config) {
63
+ var attrs = (0, _cleanAttributes.default)(config);
64
+ var html = getHTML(config);
65
+ return /*#__PURE__*/_react.default.createElement(_copReactComponents.Details, attrs, html);
66
+ };
67
+
62
68
  var getFileUpload = function getFileUpload(config) {
63
69
  var attrs = (0, _cleanAttributes.default)(config);
64
70
  return /*#__PURE__*/_react.default.createElement(_copReactComponents.FileUpload, attrs);
@@ -159,6 +165,9 @@ var getComponentByType = function getComponentByType(config) {
159
165
  case _models.ComponentTypes.WARNING:
160
166
  return getWarningText(config);
161
167
 
168
+ case _models.ComponentTypes.DETAILS:
169
+ return getDetails(config);
170
+
162
171
  default:
163
172
  {
164
173
  return null;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ var _react = require("@testing-library/react");
4
+
5
+ var _models = require("../../../models");
6
+
7
+ var _getComponent = _interopRequireDefault(require("../getComponent"));
8
+
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+
11
+ // Global imports
12
+ // Local imports
13
+ describe('utils.Component.get', function () {
14
+ it('should return a details component containing basic text', function () {
15
+ var ID = 'test-id';
16
+ var CONTENT = 'Details content';
17
+ var SUMMARY = 'Details Summary';
18
+ var COMPONENT = {
19
+ type: _models.ComponentTypes.DETAILS,
20
+ content: CONTENT,
21
+ summary: SUMMARY,
22
+ 'data-testid': ID
23
+ };
24
+
25
+ var _render = (0, _react.render)((0, _getComponent.default)(COMPONENT)),
26
+ container = _render.container;
27
+
28
+ var details = container.querySelector('details');
29
+ expect(details.classList).toContain('hods-details');
30
+ expect(details.childNodes[0].textContent).toEqual(SUMMARY);
31
+ expect(details.childNodes[1].textContent).toEqual(CONTENT);
32
+ });
33
+ it('should return a details component containing multiple html components', function () {
34
+ var ID = 'test-id';
35
+ var CONTENT = '<p>this is the content</p><ol><li>one</li><li>two</li></ol><p>second section of content</p>';
36
+ var SUMMARY = 'Details Summary';
37
+ var COMPONENT = {
38
+ type: _models.ComponentTypes.DETAILS,
39
+ content: CONTENT,
40
+ summary: SUMMARY,
41
+ 'data-testid': ID
42
+ };
43
+
44
+ var _render2 = (0, _react.render)((0, _getComponent.default)(COMPONENT)),
45
+ container = _render2.container;
46
+
47
+ var details = container.querySelector('details');
48
+ expect(details.classList).toContain('hods-details');
49
+ expect(details.childNodes[1].childNodes[0].childNodes[0].textContent).toEqual('this is the content');
50
+ expect(details.childNodes[1].childNodes[0].childNodes[0].tagName).toEqual('P');
51
+ expect(details.childNodes[1].childNodes[0].childNodes[1].childNodes[0].textContent).toEqual('one');
52
+ expect(details.childNodes[1].childNodes[0].childNodes[1].childNodes[1].textContent).toEqual('two');
53
+ expect(details.childNodes[1].childNodes[0].childNodes[1].tagName).toEqual('OL');
54
+ expect(details.childNodes[1].childNodes[0].childNodes[2].textContent).toEqual('second section of content');
55
+ expect(details.childNodes[1].childNodes[0].childNodes[2].tagName).toEqual('P');
56
+ });
57
+ });
@@ -26,7 +26,7 @@ var useComponent = function useComponent(toUse, formComponents) {
26
26
  if (formComponent) {
27
27
  var fieldId = toUse.fieldId || formComponent.fieldId;
28
28
  return _objectSpread(_objectSpread(_objectSpread({}, formComponent), toUse), {}, {
29
- cya_label: formComponent.label,
29
+ cya_label: formComponent.label || formComponent.cya_label,
30
30
  fieldId: fieldId
31
31
  });
32
32
  }
@@ -28,6 +28,11 @@ describe('utils', function () {
28
28
  fieldId: 'c',
29
29
  label: 'Charlie',
30
30
  type: 'text'
31
+ }, {
32
+ id: 'd',
33
+ fieldId: 'd',
34
+ cya_label: 'Delta',
35
+ type: 'text'
31
36
  }];
32
37
  it('should handle a null toUse configuration', function () {
33
38
  var TO_USE = null;
@@ -35,8 +40,8 @@ describe('utils', function () {
35
40
  });
36
41
  it('should handle an unrecognised component reference', function () {
37
42
  var TO_USE = {
38
- use: 'd',
39
- label: 'Delta'
43
+ use: 'e',
44
+ label: 'Echo'
40
45
  };
41
46
  expect((0, _useComponent.default)(TO_USE, FORM_COMPONENTS)).toEqual(TO_USE);
42
47
  });
@@ -77,6 +82,31 @@ describe('utils', function () {
77
82
  label: TO_USE.label
78
83
  }));
79
84
  });
85
+ it('should allow the form component cya_label, if label is empty', function () {
86
+ var TO_USE = {
87
+ use: 'd',
88
+ label: ''
89
+ };
90
+ var A = FORM_COMPONENTS[3];
91
+ expect((0, _useComponent.default)(TO_USE, FORM_COMPONENTS)).toEqual(_objectSpread(_objectSpread({
92
+ use: 'd'
93
+ }, A), {}, {
94
+ cya_label: A.cya_label,
95
+ label: TO_USE.label
96
+ }));
97
+ });
98
+ it('should allow the form component cya_label, if label is not given', function () {
99
+ var TO_USE = {
100
+ use: 'd'
101
+ };
102
+ var A = FORM_COMPONENTS[3];
103
+ expect((0, _useComponent.default)(TO_USE, FORM_COMPONENTS)).toEqual(_objectSpread(_objectSpread({
104
+ use: 'd'
105
+ }, A), {}, {
106
+ cya_label: A.cya_label,
107
+ label: TO_USE.label
108
+ }));
109
+ });
80
110
  });
81
111
  });
82
112
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "3.3.3",
3
+ "version": "3.4.1-alpha",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",