@ukhomeoffice/cop-react-form-renderer 4.73.0 → 4.75.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.
Files changed (107) hide show
  1. package/dist/components/CheckYourAnswers/Answer.js +10 -4
  2. package/dist/components/CheckYourAnswers/CheckYourAnswers.js +51 -27
  3. package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +6 -5
  4. package/dist/components/CollectionPage/CollectionPage.js +7 -5
  5. package/dist/components/FormComponent/Collection.js +25 -17
  6. package/dist/components/FormComponent/Container.js +23 -13
  7. package/dist/components/FormComponent/FormComponent.js +24 -9
  8. package/dist/components/FormComponent/helpers/addLabel.js +4 -3
  9. package/dist/components/FormPage/FormPage.js +30 -19
  10. package/dist/components/FormRenderer/FormRenderer.js +90 -34
  11. package/dist/components/FormRenderer/FormRenderer.test.js +29 -27
  12. package/dist/components/FormRenderer/handlers/index.js +1 -2
  13. package/dist/components/FormRenderer/handlers/navigate.js +3 -1
  14. package/dist/components/FormRenderer/helpers/cleanHiddenNestedData.js +19 -15
  15. package/dist/components/FormRenderer/helpers/cleanHiddenNestedData.test.js +8 -8
  16. package/dist/components/FormRenderer/helpers/getCYA.js +5 -1
  17. package/dist/components/FormRenderer/helpers/getNextPageId.js +13 -7
  18. package/dist/components/FormRenderer/helpers/getRelevantPages.js +10 -2
  19. package/dist/components/FormRenderer/helpers/getRelevantPages.test.js +1 -1
  20. package/dist/components/FormRenderer/helpers/getUpdatedSectionStates.js +38 -24
  21. package/dist/components/FormRenderer/helpers/index.js +1 -2
  22. package/dist/components/FormRenderer/onCYAAction.js +2 -2
  23. package/dist/components/FormRenderer/onCYAAction.test.js +13 -13
  24. package/dist/components/FormRenderer/onPageAction.js +28 -9
  25. package/dist/components/FormRenderer/onPageAction.test.js +54 -18
  26. package/dist/components/FormRenderer/onTaskAction.js +14 -11
  27. package/dist/components/PageActions/ActionButton.js +0 -3
  28. package/dist/components/PageActions/PageActions.js +2 -2
  29. package/dist/components/SummaryList/SummaryList.js +13 -8
  30. package/dist/components/SummaryList/SummaryListHeadingRow.js +6 -2
  31. package/dist/components/SummaryList/SummaryListRow.js +5 -2
  32. package/dist/components/SummaryList/helpers/getGroupActionAttributes.js +2 -1
  33. package/dist/components/SummaryList/helpers/getRowActionAttributes.js +2 -1
  34. package/dist/components/TaskList/Task.js +1 -1
  35. package/dist/components/TaskList/TaskList.js +21 -12
  36. package/dist/components/TaskList/TaskState.js +2 -2
  37. package/dist/components/index.js +7 -0
  38. package/dist/context/HooksContext/HooksContext.js +33 -1
  39. package/dist/context/ValidationContext/ValidationContext.js +8 -1
  40. package/dist/context/ValidationContext/ValidationContext.test.js +8 -2
  41. package/dist/hooks/useGetRequest.js +3 -2
  42. package/dist/hooks/useRefData.js +1 -0
  43. package/dist/index.js +6 -0
  44. package/dist/utils/CheckYourAnswers/getCYAAction.js +5 -5
  45. package/dist/utils/CheckYourAnswers/getCYAAction.test.js +34 -34
  46. package/dist/utils/CheckYourAnswers/getCYACollectionChangeAction.js +2 -1
  47. package/dist/utils/CheckYourAnswers/getCYACollectionChangeAction.test.js +4 -4
  48. package/dist/utils/CheckYourAnswers/getCYACollectionDeleteAction.js +2 -1
  49. package/dist/utils/CheckYourAnswers/getCYACollectionDeleteAction.test.js +2 -2
  50. package/dist/utils/CheckYourAnswers/getCYARow.js +13 -11
  51. package/dist/utils/CheckYourAnswers/getCYARowsForCollection.js +8 -8
  52. package/dist/utils/CheckYourAnswers/getCYARowsForCollectionPage.js +25 -22
  53. package/dist/utils/CheckYourAnswers/getCYARowsForContainer.js +1 -0
  54. package/dist/utils/CheckYourAnswers/showComponentCYA.js +1 -1
  55. package/dist/utils/CollectionPage/duplicateCollectionPageActiveEntry.js +1 -0
  56. package/dist/utils/CollectionPage/mergeCollectionPages.js +7 -6
  57. package/dist/utils/Component/applyToComponentTree.js +4 -2
  58. package/dist/utils/Component/cleanAttributes.js +7 -1
  59. package/dist/utils/Component/cleanAttributes.test.js +4 -4
  60. package/dist/utils/Component/getComponent.js +8 -6
  61. package/dist/utils/Component/getComponentTests/getComponent.autocomplete.test.js +4 -4
  62. package/dist/utils/Component/getComponentTests/getComponent.calculation.test.js +1 -0
  63. package/dist/utils/Component/getComponentTests/getComponent.checkboxes.test.js +3 -3
  64. package/dist/utils/Component/getComponentTests/getComponent.email.test.js +1 -1
  65. package/dist/utils/Component/getComponentTests/getComponent.file.test.js +1 -1
  66. package/dist/utils/Component/getComponentTests/getComponent.multifile.test.js +1 -1
  67. package/dist/utils/Component/getComponentTests/getComponent.phoneNumber.test.js +1 -1
  68. package/dist/utils/Component/getComponentTests/getComponent.radios.test.js +3 -3
  69. package/dist/utils/Component/getComponentTests/getComponent.text.test.js +1 -1
  70. package/dist/utils/Component/getComponentTests/getComponent.time.test.js +5 -5
  71. package/dist/utils/Component/setupContainerComponentsPath.js +2 -0
  72. package/dist/utils/Condition/meetsCondition.js +2 -1
  73. package/dist/utils/Condition/setupConditions.js +2 -2
  74. package/dist/utils/Container/setupNesting.js +5 -1
  75. package/dist/utils/Data/applyFormula.js +16 -10
  76. package/dist/utils/Data/getAutocompleteSource.js +3 -1
  77. package/dist/utils/Data/getOptions.js +3 -2
  78. package/dist/utils/Data/setupFormData.js +4 -2
  79. package/dist/utils/FormPage/getFormPage.js +1 -1
  80. package/dist/utils/FormPage/getPageActions.js +5 -3
  81. package/dist/utils/FormPage/showFormPage.js +2 -2
  82. package/dist/utils/FormPage/showFormPageCYA.js +1 -0
  83. package/dist/utils/FormPage/useComponent.js +1 -0
  84. package/dist/utils/Format/formatDataForComponent.js +2 -1
  85. package/dist/utils/Meta/index.js +1 -2
  86. package/dist/utils/Operate/getFirstOf.test.js +2 -1
  87. package/dist/utils/Operate/persistValueInFormData.js +1 -0
  88. package/dist/utils/Operate/persistValueInFormData.test.js +1 -1
  89. package/dist/utils/Operate/setValueInFormData.test.js +1 -1
  90. package/dist/utils/Operate/shouldRun.js +16 -13
  91. package/dist/utils/Validate/additional/index.js +2 -1
  92. package/dist/utils/Validate/additional/mustBeEarlierDateTime.js +1 -1
  93. package/dist/utils/Validate/additional/mustBeEarlierDateTime.test.js +2 -2
  94. package/dist/utils/Validate/additional/mustBeInTheFuture.js +1 -1
  95. package/dist/utils/Validate/additional/mustBeInThePast.js +5 -5
  96. package/dist/utils/Validate/additional/mustEnterAtLeastOne.js +1 -0
  97. package/dist/utils/Validate/additional/utils.js +16 -16
  98. package/dist/utils/Validate/validateCollection.js +3 -3
  99. package/dist/utils/Validate/validateComponent.js +26 -20
  100. package/dist/utils/Validate/validateContainer.js +2 -2
  101. package/dist/utils/Validate/validateDate.js +2 -1
  102. package/dist/utils/Validate/validateEmail.js +1 -0
  103. package/dist/utils/Validate/validatePage.js +6 -3
  104. package/dist/utils/Validate/validatePage.test.js +9 -18
  105. package/dist/utils/Validate/validateRegex.js +2 -5
  106. package/dist/utils/Validate/validateRequired.js +2 -4
  107. package/package.json +3 -2
@@ -4,9 +4,11 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _getOptions = _interopRequireDefault(require("./getOptions"));
8
7
  var _copReactComponents = require("@ukhomeoffice/cop-react-components");
8
+ var _getOptions = _interopRequireDefault(require("./getOptions"));
9
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ // Global Imports
11
+
10
12
  // Local imports
11
13
 
12
14
  var getAutocompleteSource = function getAutocompleteSource(config) {
@@ -36,11 +36,12 @@ var getOptions = function getOptions(config, callback) {
36
36
  if (config) {
37
37
  if (config.options) {
38
38
  return callback(interpolateOptions(config, config.options));
39
- } else if (config.data && config.data.options) {
39
+ }
40
+ if (config.data && config.data.options) {
40
41
  return callback(interpolateOptions(config, config.data.options));
41
42
  }
42
43
  }
43
- callback([]);
44
+ return callback([]);
44
45
  };
45
46
  var _default = getOptions;
46
47
  exports.default = _default;
@@ -24,6 +24,7 @@ var setDefaultDateValue = function setDefaultDateValue(date, data, fullPath) {
24
24
  * If support is required it will need to be added.
25
25
  */
26
26
  var setupDefaultValue = function setupDefaultValue(component, data, fullPath) {
27
+ var thisComponent = component;
27
28
  var componentFullPath = "".concat(fullPath).concat(component.fieldId);
28
29
  if (component.type === 'container') {
29
30
  var _component$components;
@@ -45,7 +46,7 @@ var setupDefaultValue = function setupDefaultValue(component, data, fullPath) {
45
46
  // both a 'value' and 'defaultValue' prop set.
46
47
  // defaultValue is safe to delete once we've tried
47
48
  // to use it.
48
- delete component.defaultValue;
49
+ delete thisComponent.defaultValue;
49
50
  }
50
51
  };
51
52
  var setupDefaultValuesForComponents = function setupDefaultValuesForComponents(components, data) {
@@ -63,8 +64,9 @@ var setupPageDefaultValues = function setupPageDefaultValues(pages, data) {
63
64
  });
64
65
  };
65
66
  var setupComponentSourceData = function setupComponentSourceData(component, data) {
67
+ var componentSourceData = data;
66
68
  if (component.source) {
67
- data[component.fieldId] = (0, _getSourceData.default)(data, component.source.field);
69
+ componentSourceData[component.fieldId] = (0, _getSourceData.default)(componentSourceData, component.source.field);
68
70
  }
69
71
  };
70
72
  var setupSourceDataForComponents = function setupSourceDataForComponents(components, data) {
@@ -31,7 +31,7 @@ var getFormPage = function getFormPage(pageOptions, formComponents, formData) {
31
31
  if (typeof componentOptions === 'string') {
32
32
  return (0, _getParagraphFromText.default)(componentOptions);
33
33
  }
34
- var ret = undefined;
34
+ var ret;
35
35
  if (componentOptions.use) {
36
36
  ret = (0, _useComponent.default)(componentOptions, formComponents);
37
37
  } else {
@@ -43,10 +43,12 @@ var getPageActions = function getPageActions(page) {
43
43
  }
44
44
  if (a && _typeof(a) === 'object') {
45
45
  var _PageAction$DEFAULTS$;
46
- if ((_PageAction$DEFAULTS$ = _models.PageAction.DEFAULTS[a.type]) !== null && _PageAction$DEFAULTS$ !== void 0 && _PageAction$DEFAULTS$.validate && !a.hasOwnProperty('validate')) {
47
- a.validate = true;
46
+ var object = a;
47
+ // eslint-disable-next-line no-prototype-builtins
48
+ if ((_PageAction$DEFAULTS$ = _models.PageAction.DEFAULTS[object.type]) !== null && _PageAction$DEFAULTS$ !== void 0 && _PageAction$DEFAULTS$.validate && !object.hasOwnProperty('validate')) {
49
+ object.validate = true;
48
50
  }
49
- return standardiseAction(a);
51
+ return standardiseAction(object);
50
52
  }
51
53
  return undefined;
52
54
  }).filter(function (a) {
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _Component = _interopRequireDefault(require("../Component"));
7
+ var _showComponent = _interopRequireDefault(require("../Component/showComponent"));
8
8
  var _Condition = _interopRequireDefault(require("../Condition"));
9
9
  var _Container = _interopRequireDefault(require("../Container"));
10
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -18,7 +18,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
18
18
  */
19
19
  var showEditableComponent = function showEditableComponent(editableComponents, data) {
20
20
  return editableComponents.some(function (component) {
21
- return _Component.default.show(component, data);
21
+ return (0, _showComponent.default)(component, data);
22
22
  });
23
23
  };
24
24
 
@@ -13,6 +13,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
13
13
  * @returns Boolean true if the page should be shown; false if not.
14
14
  */
15
15
  var showFormPageCYA = function showFormPageCYA(page, data) {
16
+ // eslint-disable-next-line no-prototype-builtins
16
17
  if (page !== null && page !== void 0 && page.hasOwnProperty('show_on_cya') && !page.show_on_cya) {
17
18
  return false;
18
19
  }
@@ -29,6 +29,7 @@ var getComponentToUse = function getComponentToUse(toUse, formComponents) {
29
29
  });
30
30
  if (parent.components.length > 0) {
31
31
  // Make the found child the parent so we can iterate.
32
+ // eslint-disable-next-line prefer-destructuring
32
33
  component = parent.components[0];
33
34
  } else {
34
35
  // If no child was found, there can be no parent.
@@ -15,8 +15,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
15
15
  * @param {string} eventType What type of event initiated this call.
16
16
  */
17
17
  var formatDataForComponent = function formatDataForComponent(component, data, eventType) {
18
+ var componentData = data;
18
19
  if (component.format && component.format.on === eventType) {
19
- data[component.fieldId] = (0, _formatData.default)(component.format, data[component.fieldId]);
20
+ componentData[component.fieldId] = (0, _formatData.default)(component.format, componentData[component.fieldId]);
20
21
  }
21
22
  };
22
23
 
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = exports.Meta = void 0;
6
+ exports.default = void 0;
7
7
  var _constants = require("./constants");
8
8
  var _documents = _interopRequireDefault(require("./documents"));
9
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -13,6 +13,5 @@ var Meta = {
13
13
  documents: _documents.default,
14
14
  name: _constants.META_PROPERTY
15
15
  };
16
- exports.Meta = Meta;
17
16
  var _default = Meta;
18
17
  exports.default = _default;
@@ -2,7 +2,7 @@
2
2
 
3
3
  var _getFirstOf = _interopRequireDefault(require("./getFirstOf"));
4
4
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
5
- describe('Utils.Operate.addToFormData', function () {
5
+ describe('Utils.Operate.getFirstOf', function () {
6
6
  var DATA = {
7
7
  name: 'sam',
8
8
  id: 1,
@@ -28,6 +28,7 @@ describe('Utils.Operate.addToFormData', function () {
28
28
  expect(result).toEqual(CONFIG.value);
29
29
  });
30
30
  it('Should return the fallback if the requested fields are not found', function () {
31
+ // eslint-disable-next-line no-template-curly-in-string
31
32
  var CONFIG = {
32
33
  value: '2',
33
34
  fields: ['nonExistent'],
@@ -26,6 +26,7 @@ var persistValueInFormData = function persistValueInFormData(config, data, onCha
26
26
  } else {
27
27
  value = config === null || config === void 0 ? void 0 : config.value;
28
28
  }
29
+ /* eslint-disable object-shorthand */
29
30
  if (value != null && config.name && data[config.name] !== value) {
30
31
  onChange({
31
32
  target: {
@@ -12,7 +12,7 @@ describe('Utils.Operate.persistValueInFormData', function () {
12
12
  var ON_CHANGE = function ON_CHANGE(_ref) {
13
13
  var target = _ref.target;
14
14
  DATA[target.name] = target.value;
15
- ON_CHANGE_COUNT++;
15
+ ON_CHANGE_COUNT += 1;
16
16
  };
17
17
  beforeEach(function () {
18
18
  DATA = {
@@ -2,7 +2,7 @@
2
2
 
3
3
  var _setValueInFormData = _interopRequireDefault(require("./setValueInFormData"));
4
4
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
5
- describe('Utils.Operate.addToFormData', function () {
5
+ describe('Utils.Operate.setValueInFormData', function () {
6
6
  var DATA = {
7
7
  a: '1',
8
8
  b: ['2', '3'],
@@ -20,24 +20,27 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
20
20
  */
21
21
  var shouldRun = function shouldRun(config, data) {
22
22
  if (config !== null && config !== void 0 && config.condition) {
23
- var fieldPath = _copReactComponents.Utils.interpolateString(config.field, data);
23
+ var configuration = config;
24
+ var fieldPath = _copReactComponents.Utils.interpolateString(configuration.field, data);
24
25
  var fieldValue = (0, _getSourceData.default)(data, fieldPath);
25
- switch (config.condition) {
26
+ switch (configuration.condition) {
26
27
  // Only run the operation if a given field's value has changed.
27
28
  case 'changes':
28
- var lastPath = "".concat(fieldPath, "LastValue");
29
- var lastValue = config[lastPath] || null;
30
- if (lastValue === null) {
31
- // If there isn't a last value then we assume
32
- // that this is the first render.
33
- config[lastPath] = fieldValue;
29
+ {
30
+ var lastPath = "".concat(fieldPath, "LastValue");
31
+ var lastValue = configuration[lastPath] || null;
32
+ if (lastValue === null) {
33
+ // If there isn't a last value then we assume
34
+ // that this is the first render.
35
+ configuration[lastPath] = fieldValue;
36
+ return false;
37
+ }
38
+ if (fieldValue !== lastValue) {
39
+ configuration[lastPath] = fieldValue;
40
+ return true;
41
+ }
34
42
  return false;
35
43
  }
36
- if (fieldValue !== lastValue) {
37
- config[lastPath] = fieldValue;
38
- return true;
39
- }
40
- return false;
41
44
  // Only run the operation if a given field's value is truthy.
42
45
  case 'isTruthy':
43
46
  return !!fieldValue;
@@ -46,9 +46,10 @@ var additionalValidation = function additionalValidation(value, config, componen
46
46
  };
47
47
  var runAdditionalComponentValidation = function runAdditionalComponentValidation(component, value) {
48
48
  // We only care when we have a value - if we don't have one but want one, set `required: true`.
49
+ // eslint-disable-next-line no-extra-boolean-cast
49
50
  if (!!value) {
50
51
  if (Array.isArray(component.additionalValidation)) {
51
- var error = undefined;
52
+ var error;
52
53
  component.additionalValidation.forEach(function (config) {
53
54
  // If we've already encountered an error, don't run any more validators.
54
55
  if (!error) {
@@ -11,7 +11,7 @@ var _utils = require("./utils");
11
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
12
  // Global Imports
13
13
 
14
- //Local imports
14
+ // Local imports
15
15
 
16
16
  _dayjs.default.extend(_customParseFormat.default);
17
17
 
@@ -95,13 +95,13 @@ describe('utils.Validate.additional.mustBeEarlierDateTime', function () {
95
95
  expect(result).toEqual(true);
96
96
  });
97
97
  test('should return false if date & time are not in the past', function () {
98
- var DATA = {
98
+ var futureDATA = {
99
99
  testContainer: {
100
100
  testDate: '01-01-5000',
101
101
  testTime: '00:00'
102
102
  }
103
103
  };
104
- var result = (0, _mustBeEarlierDateTime.default)(DATA, CONFIG, CONTAINER);
104
+ var result = (0, _mustBeEarlierDateTime.default)(futureDATA, CONFIG, CONTAINER);
105
105
  expect(result).toEqual(false);
106
106
  });
107
107
  });
@@ -11,7 +11,7 @@ var _utils = require("./utils");
11
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
12
  // Global Imports
13
13
 
14
- //Local imports
14
+ // Local imports
15
15
 
16
16
  _dayjs.default.extend(_customParseFormat.default);
17
17
  _dayjs.default.extend(_isToday.default);
@@ -11,7 +11,7 @@ var _utils = require("./utils");
11
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
12
  // Global imports
13
13
 
14
- //Local imports
14
+ // Local imports
15
15
 
16
16
  _dayjs.default.extend(_customParseFormat.default);
17
17
  _dayjs.default.extend(_isToday.default);
@@ -25,12 +25,12 @@ _dayjs.default.extend(_isToday.default);
25
25
  */
26
26
  var mustBeInThePast = function mustBeInThePast(date, config) {
27
27
  var beforeToday = (0, _dayjs.default)((0, _utils.formatString)(date), _utils.DATE_FORMAT).isBefore((0, _dayjs.default)());
28
- var isToday = (0, _dayjs.default)((0, _utils.formatString)(date), _utils.DATE_FORMAT).isToday();
29
- //dayjs classifies the current day as in the past so exclude it here
30
- if (beforeToday && !isToday) {
28
+ var dateIsToday = (0, _dayjs.default)((0, _utils.formatString)(date), _utils.DATE_FORMAT).isToday();
29
+ // dayjs classifies the current day as in the past so exclude it here
30
+ if (beforeToday && !dateIsToday) {
31
31
  return true;
32
32
  }
33
- return isToday && !!(config !== null && config !== void 0 && config.todayAllowed);
33
+ return dateIsToday && !!(config !== null && config !== void 0 && config.todayAllowed);
34
34
  };
35
35
  var _default = mustBeInThePast;
36
36
  exports.default = _default;
@@ -11,6 +11,7 @@ exports.default = void 0;
11
11
  * @returns true if the user has entered data into at least one component within the collection
12
12
  * otherwise returns false
13
13
  */
14
+ // eslint-disable-next-line arrow-body-style
14
15
  var mustEnterAtLeastOne = function mustEnterAtLeastOne(data, _, component) {
15
16
  var _component$components;
16
17
  return (_component$components = component.components) === null || _component$components === void 0 ? void 0 : _component$components.some(function (inner) {
@@ -13,6 +13,22 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
13
13
  var isNumeric = function isNumeric(value) {
14
14
  return /^-?\d+$/.test(value);
15
15
  };
16
+
17
+ /**
18
+
19
+ /**
20
+ * Prepends a leading zero if the 'date component' parameter contains only a single digit.
21
+ * if the parameter contains more than one digit it is returned unchanged.
22
+ * If the parameter contains a non-numeric value it returned as an empty string.
23
+ * @param {string} dateComponent - the value representing a day or month.
24
+ * @returns the value (appended with a leading zero, if the value passed in was a single digit)
25
+ */
26
+ var formatInTwoDigits = function formatInTwoDigits(dateComponent) {
27
+ if (isNumeric(dateComponent)) {
28
+ return dateComponent.padStart(2, '0');
29
+ }
30
+ return '';
31
+ };
16
32
  var formatString = function formatString(date) {
17
33
  var _date$split = date.split('-'),
18
34
  _date$split2 = _slicedToArray(_date$split, 3),
@@ -29,22 +45,6 @@ var formattedTime = function formattedTime(time) {
29
45
  minute = _time$split2[1];
30
46
  return "".concat(formatInTwoDigits(hour), ":").concat(formatInTwoDigits(minute));
31
47
  };
32
-
33
- /**
34
-
35
- /**
36
- * Prepends a leading zero if the 'date component' parameter contains only a single digit.
37
- * if the parameter contains more than one digit it is returned unchanged.
38
- * If the parameter contains a non-numeric value it returned as an empty string.
39
- * @param {string} dateComponent - the value representing a day or month.
40
- * @returns the value (appended with a leading zero, if the value passed in was a single digit)
41
- */
42
48
  exports.formattedTime = formattedTime;
43
- var formatInTwoDigits = function formatInTwoDigits(dateComponent) {
44
- if (isNumeric(dateComponent)) {
45
- return dateComponent.padStart(2, '0');
46
- }
47
- return '';
48
- };
49
49
  var DATE_FORMAT = 'DD-MM-YYYY';
50
50
  exports.DATE_FORMAT = DATE_FORMAT;
@@ -23,12 +23,12 @@ var validateCollection = function validateCollection(collection, items, formData
23
23
  var errors = [];
24
24
  if (collection && Array.isArray(collection.item) && Array.isArray(items)) {
25
25
  items.forEach(function (item, index) {
26
- var full_path = "".concat(collection.full_path || collection.fieldId, "[").concat(index, "]");
26
+ var fullPath = "".concat(collection.full_path || collection.fieldId, "[").concat(index, "]");
27
27
  var container = {
28
- full_path: full_path,
28
+ full_path: fullPath,
29
29
  components: collection.item.map(function (component) {
30
30
  return _objectSpread(_objectSpread({}, component), {}, {
31
- full_path: "".concat(full_path, ".").concat(component.fieldId)
31
+ full_path: "".concat(fullPath, ".").concat(component.fieldId)
32
32
  });
33
33
  })
34
34
  };
@@ -27,7 +27,6 @@ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" =
27
27
  * @returns The first encountered error with the component.
28
28
  */
29
29
  var validateComponent = function validateComponent(component, outerData, formData) {
30
- var _component$data$optio;
31
30
  var fd = formData || outerData;
32
31
  if (!(0, _showComponent.default)(component, fd)) {
33
32
  return undefined;
@@ -35,8 +34,8 @@ var validateComponent = function validateComponent(component, outerData, formDat
35
34
  if (component.type === _models.ComponentTypes.CONTAINER) {
36
35
  return (0, _validateContainer.default)(component, outerData, fd);
37
36
  }
38
- var error = undefined;
39
- var properties = undefined;
37
+ var error;
38
+ var properties;
40
39
  var data = outerData && _typeof(outerData) === 'object' ? outerData : {};
41
40
  var value = data[component.fieldId];
42
41
  if (component.required) {
@@ -51,27 +50,34 @@ var validateComponent = function validateComponent(component, outerData, formDat
51
50
  break;
52
51
  case _models.ComponentTypes.DATE:
53
52
  case _models.ComponentTypes.TIME:
54
- var validator = component.type === _models.ComponentTypes.DATE ? _validateDate.default : _validateTime.default;
55
- var _validator = validator(value),
56
- message = _validator.message,
57
- propsInError = _validator.propsInError;
58
- properties = propsInError;
59
- error = message;
60
- break;
53
+ {
54
+ var validator = component.type === _models.ComponentTypes.DATE ? _validateDate.default : _validateTime.default;
55
+ var _validator = validator(value),
56
+ message = _validator.message,
57
+ propsInError = _validator.propsInError;
58
+ properties = propsInError;
59
+ error = message;
60
+ break;
61
+ }
62
+ ;
61
63
  case _models.ComponentTypes.CHECKBOXES:
62
64
  case _models.ComponentTypes.RADIOS:
63
- var nestedErrors = [];
64
- (_component$data$optio = component.data.options) === null || _component$data$optio === void 0 ? void 0 : _component$data$optio.forEach(function (option) {
65
- if (option.nested && (0, _optionIsSelected.default)(formData[component.id], option)) {
66
- nestedErrors = nestedErrors.concat((0, _validateContainer.default)({
67
- components: option.nested
68
- }, formData));
65
+ {
66
+ var _component$data$optio;
67
+ var nestedErrors = [];
68
+ (_component$data$optio = component.data.options) === null || _component$data$optio === void 0 ? void 0 : _component$data$optio.forEach(function (option) {
69
+ if (option.nested && (0, _optionIsSelected.default)(formData[component.id], option)) {
70
+ nestedErrors = nestedErrors.concat((0, _validateContainer.default)({
71
+ components: option.nested
72
+ }, formData));
73
+ }
74
+ });
75
+ if (nestedErrors.length > 0) {
76
+ return nestedErrors;
69
77
  }
70
- });
71
- if (nestedErrors.length > 0) {
72
- return nestedErrors;
78
+ break;
73
79
  }
74
- break;
80
+ ;
75
81
  case _models.ComponentTypes.MULTI_FILE:
76
82
  error = (0, _validateMultifile.default)(value, component.custom_errors);
77
83
  break;
@@ -39,9 +39,9 @@ var validateContainer = function validateContainer(container, outerData, formDat
39
39
  // It is possible that the container being passed in is a
40
40
  // dummy container just for validation. In that scenario we want
41
41
  // to ignore the full_path and just use the component's fieldId.
42
- var full_path = container.full_path || container.fieldId ? "".concat(container.full_path || container.fieldId, ".").concat(component.fieldId) : component.fieldId;
42
+ var fullPath = container.full_path || container.fieldId ? "".concat(container.full_path || container.fieldId, ".").concat(component.fieldId) : component.fieldId;
43
43
  errors.push((0, _validateComponent.default)(_objectSpread(_objectSpread({}, component), {}, {
44
- full_path: full_path
44
+ full_path: fullPath
45
45
  }), containerData, fd));
46
46
  });
47
47
  }
@@ -22,7 +22,8 @@ _dayjs.default.extend(_isLeapYear.default);
22
22
  var maxMonthDays = function maxMonthDays(month, year) {
23
23
  if (month === '02') {
24
24
  return (0, _dayjs.default)().year(year).isLeapYear() ? 29 : 28;
25
- } else if (['04', '06', '09', '11'].includes(month)) {
25
+ }
26
+ if (['04', '06', '09', '11'].includes(month)) {
26
27
  return 30;
27
28
  }
28
29
  return 31;
@@ -22,6 +22,7 @@ var HODS_EMAIL_REGEX = /^[a-z0-9._-]+@(digital\.)?homeoffice.gov.uk$/i;
22
22
  var validateEmail = function validateEmail(value) {
23
23
  var label = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
24
24
  var customErrors = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
25
+ // eslint-disable-next-line no-extra-boolean-cast
25
26
  if (!!value) {
26
27
  var name = label ? label.toLowerCase() : 'email address';
27
28
  if (typeof value === 'string') {
@@ -33,9 +33,12 @@ var validatePage = function validatePage(page) {
33
33
  var errs = page.components.reduce(function (errors, component) {
34
34
  var componentErrors = (0, _validateComponent.default)(component, data, data);
35
35
  return errors.concat(componentErrors).flat().map(function (err) {
36
- return !!err ? _objectSpread(_objectSpread({}, err), {}, {
37
- error: _copReactComponents.Utils.interpolateString(err.error, data)
38
- }) : err;
36
+ if (err) {
37
+ return _objectSpread(_objectSpread({}, err), {}, {
38
+ error: _copReactComponents.Utils.interpolateString(err.error, data)
39
+ });
40
+ }
41
+ return err;
39
42
  });
40
43
  }, []).filter(function (e) {
41
44
  return !!e;
@@ -10,16 +10,16 @@ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key i
10
10
  function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
11
11
  function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
12
12
  describe('utils.Validate.Page', function () {
13
- describe('with FormPage', function () {
14
- var setup = function setup(id, type, label, required) {
15
- return {
16
- id: id,
17
- fieldId: id,
18
- type: type,
19
- label: label,
20
- required: required
21
- };
13
+ var setup = function setup(id, type, label, required) {
14
+ return {
15
+ id: id,
16
+ fieldId: id,
17
+ type: type,
18
+ label: label,
19
+ required: required
22
20
  };
21
+ };
22
+ describe('with FormPage', function () {
23
23
  it('should return no error when the components array is null', function () {
24
24
  var PAGE = {
25
25
  components: null,
@@ -270,15 +270,6 @@ describe('utils.Validate.Page', function () {
270
270
  var COLLECTION = {
271
271
  name: 'pageCollection'
272
272
  };
273
- var setup = function setup(id, type, label, required) {
274
- return {
275
- id: id,
276
- fieldId: id,
277
- type: type,
278
- label: label,
279
- required: required
280
- };
281
- };
282
273
  describe('when the form data is fully valid', function () {
283
274
  var _DATA;
284
275
  var DATA = (_DATA = {}, _defineProperty(_DATA, "".concat(COLLECTION.name, "ActiveId"), '01'), _defineProperty(_DATA, COLLECTION.name, [{
@@ -15,10 +15,7 @@ exports.default = void 0;
15
15
  * @param {array} customErrors The array of custom errors for the component.
16
16
  * @returns An error if the value doesn't match the regex pattern.
17
17
  */
18
- var validateRegex = function validateRegex(value) {
19
- var label = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
20
- var pattern = arguments.length > 2 ? arguments[2] : undefined;
21
- var customErrors = arguments.length > 3 ? arguments[3] : undefined;
18
+ var validateRegex = function validateRegex(value, label, pattern, customErrors) {
22
19
  if (!value) {
23
20
  return undefined;
24
21
  }
@@ -37,7 +34,7 @@ var validateRegex = function validateRegex(value) {
37
34
  }
38
35
  }
39
36
  }
40
- if (label === '') {
37
+ if (!label) {
41
38
  return 'Component failed regex validation';
42
39
  }
43
40
  return "".concat(label, " failed regex validation");
@@ -11,9 +11,7 @@ exports.default = void 0;
11
11
  * @param {string} label The label to use in any error message.
12
12
  * @returns An error if the value is nullish.
13
13
  */
14
- var validateRequired = function validateRequired(value) {
15
- var label = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
16
- var customErrors = arguments.length > 2 ? arguments[2] : undefined;
14
+ var validateRequired = function validateRequired(value, label, customErrors) {
17
15
  var hasValue = false;
18
16
  if (!!value || value === false || value === 0) {
19
17
  hasValue = true;
@@ -32,7 +30,7 @@ var validateRequired = function validateRequired(value) {
32
30
  return result[0].message;
33
31
  }
34
32
  }
35
- var name = label ? label : 'Field';
33
+ var name = label || 'Field';
36
34
  return "".concat(name, " is required");
37
35
  }
38
36
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.73.0",
3
+ "version": "4.75.0",
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.14.4",
19
+ "@ukhomeoffice/cop-react-components": "^2.14.5",
20
20
  "axios": "^0.23.0",
21
21
  "dayjs": "^1.11.0",
22
22
  "govuk-frontend": "^4.3.1",
@@ -46,6 +46,7 @@
46
46
  "cross-env": "^7.0.3",
47
47
  "html-react-parser": "^0.10.5",
48
48
  "node-sass": "^6.0.1",
49
+ "prop-types": "^15.8.1",
49
50
  "react": "^16.13.1",
50
51
  "react-dom": "^16.13.1",
51
52
  "react-scripts": "4.0.3",