@ukhomeoffice/cop-react-form-renderer 4.69.2 → 4.70.1

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.
@@ -86,6 +86,12 @@ var Collection = function Collection(_ref) {
86
86
  return _ref3.apply(this, arguments);
87
87
  };
88
88
  }();
89
+ (0, _react.useEffect)(function () {
90
+ if (config.minimumEntries && !value.length) {
91
+ onAddAnother();
92
+ }
93
+ // eslint-disable-next-line react-hooks/exhaustive-deps
94
+ }, [config.minimumEntries, value.length]);
89
95
  var onRemoveItem = /*#__PURE__*/function () {
90
96
  var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(item) {
91
97
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
@@ -123,7 +129,7 @@ var Collection = function Collection(_ref) {
123
129
  var removeLabel = _utils.default.interpolateString(labels.remove, _objectSpread(_objectSpread({}, item), {}, {
124
130
  index: labelCount
125
131
  }));
126
- var removeStyle = config.disableAddAndRemove ? '' : (_config$removeLocatio = config.removeLocation) !== null && _config$removeLocatio !== void 0 ? _config$removeLocatio : 'start';
132
+ var removeStyle = config.disableAddAndRemove || index === 0 && config.minimumEntries ? '' : (_config$removeLocatio = config.removeLocation) !== null && _config$removeLocatio !== void 0 ? _config$removeLocatio : 'start';
127
133
  return /*#__PURE__*/_react.default.createElement("div", {
128
134
  className: "".concat(classes('item')),
129
135
  key: item.id
@@ -398,4 +398,57 @@ describe('components.FormComponent.Collection', function () {
398
398
  }
399
399
  }, _callee9);
400
400
  })));
401
+ it('should render a collection component with minimumEntries appropriately', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10() {
402
+ var COLLECTION, _renderWithValidation6, container, c, item, _item$childNodes3, title, itemContainer, formGroup, label, hint, input;
403
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
404
+ while (1) switch (_context10.prev = _context10.next) {
405
+ case 0:
406
+ COLLECTION = {
407
+ id: ID,
408
+ fieldId: ID,
409
+ type: _models.ComponentTypes.COLLECTION,
410
+ item: [TEXT_COMPONENT],
411
+ minimumEntries: 1
412
+ };
413
+ _renderWithValidation6 = (0, _setupTests.renderWithValidation)( /*#__PURE__*/_react2.default.createElement(_FormComponent.default, {
414
+ component: COLLECTION,
415
+ value: [],
416
+ formData: {}
417
+ })), container = _renderWithValidation6.container; // Check the container itself.
418
+ c = container.childNodes[0];
419
+ expect(c.tagName).toEqual('DIV');
420
+ expect(c.classList).toContain(_Collection.DEFAULT_CLASS);
421
+
422
+ // And now check the single text component within it.
423
+ item = c.childNodes[0];
424
+ expect(item.tagName).toEqual('DIV');
425
+ expect(item.classList).toContain("".concat(_Collection.DEFAULT_CLASS, "__item"));
426
+ _item$childNodes3 = _slicedToArray(item.childNodes, 2), title = _item$childNodes3[0], itemContainer = _item$childNodes3[1];
427
+ expect(title.tagName).toEqual('LABEL');
428
+ expect(title.classList).toContain("".concat(_Collection.DEFAULT_CLASS, "__item-title"));
429
+ expect(itemContainer.tagName).toEqual('DIV');
430
+ expect(itemContainer.classList).toContain(_Container.DEFAULT_CLASS);
431
+ formGroup = itemContainer.childNodes[0];
432
+ expect(formGroup.tagName).toEqual('DIV');
433
+ expect(formGroup.classList).toContain('govuk-form-group');
434
+ label = formGroup.childNodes[0].childNodes[0].childNodes[0];
435
+ expect(label.tagName).toEqual('LABEL');
436
+ expect(label.classList).toContain('govuk-label');
437
+ expect(label.textContent).toEqual("".concat(TEXT_COMPONENT.label, " (optional)"));
438
+ expect(label.getAttribute('for')).toEqual("".concat(ID, "[0].").concat(TEXT_ID));
439
+ hint = formGroup.childNodes[0].childNodes[1];
440
+ expect(hint.tagName).toEqual('DIV');
441
+ expect(hint.classList).toContain('govuk-hint');
442
+ expect(hint.textContent).toEqual(TEXT_COMPONENT.hint);
443
+ input = formGroup.childNodes[0].childNodes[2];
444
+ expect(input.tagName).toEqual('INPUT');
445
+ expect(input.classList).toContain('govuk-input');
446
+ expect(input.id).toEqual("".concat(ID, "[0].").concat(TEXT_ID));
447
+ expect(input.value).toEqual('');
448
+ case 30:
449
+ case "end":
450
+ return _context10.stop();
451
+ }
452
+ }, _callee10);
453
+ })));
401
454
  });
@@ -13,7 +13,7 @@ var _models = require("../../models");
13
13
  var _Task = _interopRequireDefault(require("./Task"));
14
14
  var _showComponent = _interopRequireDefault(require("../../utils/Component/showComponent"));
15
15
  require("./TaskList.scss");
16
- var _excluded = ["refTitle", "refNumber", "notes", "incompleteTitle", "sections", "fieldId", "onTaskAction", "classBlock", "classModifiers", "className"];
16
+ var _excluded = ["id", "refTitle", "refNumber", "notes", "incompleteTitle", "sections", "fieldId", "onTaskAction", "classBlock", "classModifiers", "className", "formData"];
17
17
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
18
18
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
19
19
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -31,7 +31,8 @@ exports.DEFAULT_CLASS = DEFAULT_CLASS;
31
31
  var DEFAULT_INCOMPLETE_TITLE = 'Incomplete form';
32
32
  exports.DEFAULT_INCOMPLETE_TITLE = DEFAULT_INCOMPLETE_TITLE;
33
33
  var TaskList = function TaskList(_ref) {
34
- var refTitle = _ref.refTitle,
34
+ var id = _ref.id,
35
+ refTitle = _ref.refTitle,
35
36
  refNumber = _ref.refNumber,
36
37
  notes = _ref.notes,
37
38
  incompleteTitle = _ref.incompleteTitle,
@@ -41,6 +42,7 @@ var TaskList = function TaskList(_ref) {
41
42
  classBlock = _ref.classBlock,
42
43
  classModifiers = _ref.classModifiers,
43
44
  className = _ref.className,
45
+ formData = _ref.formData,
44
46
  attrs = _objectWithoutProperties(_ref, _excluded);
45
47
  var classes = _copReactComponents.Utils.classBuilder(classBlock, classModifiers, className);
46
48
 
@@ -58,7 +60,7 @@ var TaskList = function TaskList(_ref) {
58
60
  _sections$filter$redu2 = _slicedToArray(_sections$filter$redu, 2),
59
61
  completeSections = _sections$filter$redu2[0],
60
62
  totalSections = _sections$filter$redu2[1];
61
- var notesId = "".concat(attrs.id, "Notes");
63
+ var notesId = "".concat(id, "Notes");
62
64
  var _ref2 = notes ? notes : {},
63
65
  notesTitle = _ref2.title,
64
66
  notesText = _ref2.text;
@@ -67,7 +69,9 @@ var TaskList = function TaskList(_ref) {
67
69
  onTaskAction(task);
68
70
  }
69
71
  };
70
- return /*#__PURE__*/_react.default.createElement("div", _extends({}, attrs, {
72
+ return /*#__PURE__*/_react.default.createElement("div", _extends({
73
+ id: id
74
+ }, attrs, {
71
75
  className: classes()
72
76
  }), refNumber && refTitle && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("p", {
73
77
  className: "govuk-body govuk-!-margin-bottom-0"
@@ -81,15 +85,15 @@ var TaskList = function TaskList(_ref) {
81
85
  className: "tasklist-summary"
82
86
  }, incompleteTitle)), /*#__PURE__*/_react.default.createElement("p", {
83
87
  className: "govuk-body govuk-!-margin-bottom-7"
84
- }, "You have completed ".concat(completeSections, " of ").concat(totalSections, " sections")), notesTitle && notesText && (0, _showComponent.default)(notes, attrs.formData) && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("p", {
88
+ }, "You have completed ".concat(completeSections, " of ").concat(totalSections, " sections")), notesTitle && notesText && (0, _showComponent.default)(notes, formData) && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("p", {
85
89
  className: "govuk-body govuk-!-margin-bottom-0"
86
90
  }, /*#__PURE__*/_react.default.createElement("strong", {
87
91
  className: "tasklist-summary"
88
- }, _copReactComponents.Utils.interpolateString(notesTitle, attrs.formData))), /*#__PURE__*/_react.default.createElement(_copReactComponents.TextArea, {
92
+ }, _copReactComponents.Utils.interpolateString(notesTitle, formData))), /*#__PURE__*/_react.default.createElement(_copReactComponents.TextArea, {
89
93
  id: notesId,
90
94
  fieldId: notesId,
91
95
  readOnly: true,
92
- value: _copReactComponents.Utils.interpolateString(notesText, attrs.formData)
96
+ value: _copReactComponents.Utils.interpolateString(notesText, formData)
93
97
  })), sections.filter(function (section) {
94
98
  return !section.skipped;
95
99
  }).map(function (section, index) {
@@ -111,6 +115,7 @@ var TaskList = function TaskList(_ref) {
111
115
  }));
112
116
  };
113
117
  TaskList.propTypes = {
118
+ id: _propTypes.default.string,
114
119
  refTitle: _propTypes.default.string,
115
120
  refNumber: _propTypes.default.string,
116
121
  notes: _propTypes.default.shape({
@@ -130,7 +135,8 @@ TaskList.propTypes = {
130
135
  })).isRequired,
131
136
  classBlock: _propTypes.default.string,
132
137
  classModifiers: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.arrayOf(_propTypes.default.string)]),
133
- className: _propTypes.default.string
138
+ className: _propTypes.default.string,
139
+ formData: _propTypes.default.shape({})
134
140
  };
135
141
  TaskList.defaultProps = {
136
142
  classBlock: DEFAULT_CLASS,
@@ -326,10 +326,11 @@ describe('components', function () {
326
326
  container = _render6.container;
327
327
  var childNodes = container.childNodes;
328
328
  expect(childNodes.length).toEqual(1);
329
- expect(childNodes[0].childNodes.length).toEqual(4);
330
- expect(childNodes[0].childNodes[0].textContent).toEqual(INCOMPLETE_TITLE);
331
- expect(childNodes[0].childNodes[1].textContent).toEqual('You have completed 1 of 3 sections');
332
- expect(childNodes[0].childNodes[2].textContent).toEqual("1. ".concat(sections[1].name));
329
+ var children = childNodes[0].childNodes;
330
+ expect(children.length).toEqual(4);
331
+ expect(children[0].textContent).toEqual(INCOMPLETE_TITLE);
332
+ expect(children[1].textContent).toEqual('You have completed 1 of 3 sections');
333
+ expect(children[2].textContent).toEqual("1. ".concat(sections[1].name));
333
334
  });
334
335
  });
335
336
  it('should pass the selected task to the given onTaskAction function', function () {
@@ -13,6 +13,7 @@ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input ==
13
13
  var createMasterPage = function createMasterPage(page) {
14
14
  return {
15
15
  id: page.id,
16
+ name: page.name,
16
17
  collection: _objectSpread({
17
18
  masterPage: true
18
19
  }, page.collection),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.69.2",
3
+ "version": "4.70.1",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",