@ukhomeoffice/cop-react-form-renderer 3.23.4 → 3.25.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.
- package/dist/components/FormComponent/FormComponent.js +9 -1
- package/dist/components/SummaryList/helpers/getGroupActionAttributes.js +7 -1
- package/dist/utils/Validate/additional/index.js +7 -4
- package/dist/utils/Validate/additional/mustEnterAtLeastOne.js +30 -0
- package/dist/utils/Validate/additional/mustEnterAtLeastOne.test.js +39 -0
- package/dist/utils/Validate/validateContainer.js +16 -1
- package/dist/utils/Validate/validateContainer.test.js +13 -0
- package/package.json +2 -2
|
@@ -17,6 +17,8 @@ var _models = require("../../models");
|
|
|
17
17
|
|
|
18
18
|
var _utils = _interopRequireDefault(require("../../utils"));
|
|
19
19
|
|
|
20
|
+
var _wrapInFormGroup = _interopRequireDefault(require("../../utils/Component/wrapInFormGroup"));
|
|
21
|
+
|
|
20
22
|
var _Collection = _interopRequireDefault(require("./Collection"));
|
|
21
23
|
|
|
22
24
|
var _Container = _interopRequireDefault(require("./Container"));
|
|
@@ -97,13 +99,19 @@ var FormComponent = function FormComponent(_ref) {
|
|
|
97
99
|
};
|
|
98
100
|
|
|
99
101
|
if (component.type === _models.ComponentTypes.CONTAINER) {
|
|
100
|
-
|
|
102
|
+
var container = /*#__PURE__*/_react.default.createElement(_Container.default, _extends({}, attrs, {
|
|
101
103
|
container: component,
|
|
102
104
|
wrap: wrap,
|
|
103
105
|
onChange: onComponentChange,
|
|
104
106
|
value: value || _utils.default.Component.defaultValue(component),
|
|
105
107
|
formData: formData
|
|
106
108
|
}));
|
|
109
|
+
|
|
110
|
+
if (component.additionalValidation) {
|
|
111
|
+
container = (0, _wrapInFormGroup.default)(_objectSpread(_objectSpread({}, component), (0, _helpers.getComponentError)(component, validation === null || validation === void 0 ? void 0 : validation.errors)), container);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return container;
|
|
107
115
|
}
|
|
108
116
|
|
|
109
117
|
if (component.type === _models.ComponentTypes.COLLECTION) {
|
|
@@ -11,7 +11,13 @@ var getGroupActionAttributes = function getGroupActionAttributes(groupRow) {
|
|
|
11
11
|
return {
|
|
12
12
|
onClick: function onClick() {
|
|
13
13
|
return groupRow.action.onAction(groupRow);
|
|
14
|
-
}
|
|
14
|
+
},
|
|
15
|
+
onKeyPress: function onKeyPress(event) {
|
|
16
|
+
if (event.key === 'Enter') {
|
|
17
|
+
groupRow.action.onAction(groupRow);
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
tabIndex: '0'
|
|
15
21
|
};
|
|
16
22
|
} else if (groupRow.action.page) {
|
|
17
23
|
return {
|
|
@@ -17,6 +17,8 @@ var _mustBeLongerThan = _interopRequireDefault(require("./mustBeLongerThan"));
|
|
|
17
17
|
|
|
18
18
|
var _mustBeShorterThan = _interopRequireDefault(require("./mustBeShorterThan"));
|
|
19
19
|
|
|
20
|
+
var _mustEnterAtLeastOne = _interopRequireDefault(require("./mustEnterAtLeastOne"));
|
|
21
|
+
|
|
20
22
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
23
|
|
|
22
24
|
// Local imports
|
|
@@ -26,14 +28,15 @@ var functions = {
|
|
|
26
28
|
mustBeInThePast: _mustBeInThePast.default,
|
|
27
29
|
mustBeInTheFuture: _mustBeInTheFuture.default,
|
|
28
30
|
mustBeLongerThan: _mustBeLongerThan.default,
|
|
29
|
-
mustBeShorterThan: _mustBeShorterThan.default
|
|
31
|
+
mustBeShorterThan: _mustBeShorterThan.default,
|
|
32
|
+
mustEnterAtLeastOne: _mustEnterAtLeastOne.default
|
|
30
33
|
};
|
|
31
34
|
|
|
32
|
-
var additionalValidation = function additionalValidation(value, config) {
|
|
35
|
+
var additionalValidation = function additionalValidation(value, config, component) {
|
|
33
36
|
var fn = functions[config.function];
|
|
34
37
|
|
|
35
38
|
if (typeof fn === 'function') {
|
|
36
|
-
return fn(value, config) ? undefined : config.message;
|
|
39
|
+
return fn(value, config, component) ? undefined : config.message;
|
|
37
40
|
}
|
|
38
41
|
|
|
39
42
|
return undefined;
|
|
@@ -47,7 +50,7 @@ var runAdditionalComponentValidation = function runAdditionalComponentValidation
|
|
|
47
50
|
component.additionalValidation.forEach(function (config) {
|
|
48
51
|
// If we've already encountered an error, don't run any more validators.
|
|
49
52
|
if (!error) {
|
|
50
|
-
error = additionalValidation(value, config);
|
|
53
|
+
error = additionalValidation(value, config, component);
|
|
51
54
|
}
|
|
52
55
|
});
|
|
53
56
|
return error;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Additional validator for the Container component type.
|
|
10
|
+
* @param {*} data the form data
|
|
11
|
+
* @param {*} component the container component
|
|
12
|
+
* @returns true if the user has entered data into at least one component within the collection
|
|
13
|
+
* otherwise returns false
|
|
14
|
+
*/
|
|
15
|
+
var mustEnterAtLeastOne = function mustEnterAtLeastOne(data, _, component) {
|
|
16
|
+
var _component$components;
|
|
17
|
+
|
|
18
|
+
return (_component$components = component.components) === null || _component$components === void 0 ? void 0 : _component$components.some(function (inner) {
|
|
19
|
+
var _data$component$id;
|
|
20
|
+
|
|
21
|
+
if ((_data$component$id = data[component.id]) !== null && _data$component$id !== void 0 && _data$component$id[inner.id]) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return false;
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
var _default = mustEnterAtLeastOne;
|
|
30
|
+
exports.default = _default;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _mustEnterAtLeastOne = _interopRequireDefault(require("./mustEnterAtLeastOne"));
|
|
4
|
+
|
|
5
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
6
|
+
|
|
7
|
+
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; }
|
|
8
|
+
|
|
9
|
+
describe('utils', function () {
|
|
10
|
+
describe('Validate', function () {
|
|
11
|
+
describe('additional', function () {
|
|
12
|
+
describe('mustEnterAtLeastOne', function () {
|
|
13
|
+
var CONTAINER_ID = 'containerId';
|
|
14
|
+
var COMPONENT = {
|
|
15
|
+
id: CONTAINER_ID,
|
|
16
|
+
type: 'container',
|
|
17
|
+
components: [{
|
|
18
|
+
id: 'alpha'
|
|
19
|
+
}, {
|
|
20
|
+
id: 'bravo'
|
|
21
|
+
}, {
|
|
22
|
+
id: 'charlie'
|
|
23
|
+
}]
|
|
24
|
+
};
|
|
25
|
+
test('should return false if all components are empty', function () {
|
|
26
|
+
var DATA = {};
|
|
27
|
+
expect((0, _mustEnterAtLeastOne.default)(DATA, {}, COMPONENT)).toEqual(false);
|
|
28
|
+
});
|
|
29
|
+
test('should return true if one component has data', function () {
|
|
30
|
+
var DATA = _defineProperty({}, CONTAINER_ID, {
|
|
31
|
+
alpha: 'text'
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
expect((0, _mustEnterAtLeastOne.default)(DATA, {}, COMPONENT)).toEqual(true);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -7,6 +7,8 @@ exports.default = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _validateComponent = _interopRequireDefault(require("./validateComponent"));
|
|
9
9
|
|
|
10
|
+
var _additional = _interopRequireDefault(require("./additional"));
|
|
11
|
+
|
|
10
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
13
|
|
|
12
14
|
// Local imports
|
|
@@ -19,10 +21,23 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
19
21
|
* @returns Errors for all components within the container.
|
|
20
22
|
*/
|
|
21
23
|
var validateContainer = function validateContainer(container, outerData, formData) {
|
|
24
|
+
if (!container) {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
|
|
22
28
|
var fd = formData || outerData;
|
|
29
|
+
var error = (0, _additional.default)(container, fd);
|
|
30
|
+
|
|
31
|
+
if (error) {
|
|
32
|
+
return {
|
|
33
|
+
id: container.id,
|
|
34
|
+
error: error
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
23
38
|
var errors = [];
|
|
24
39
|
|
|
25
|
-
if (
|
|
40
|
+
if (Array.isArray(container.components)) {
|
|
26
41
|
var containerData = outerData && container.fieldId ? outerData[container.fieldId] : outerData;
|
|
27
42
|
container.components.forEach(function (component) {
|
|
28
43
|
errors.push((0, _validateComponent.default)(component, containerData, fd));
|
|
@@ -65,4 +65,17 @@ describe('utils.Validate.Container', function () {
|
|
|
65
65
|
error: "Enter ".concat(EMAIL_LABEL.toLowerCase(), " in the correct format, like jane.doe@homeoffice.gov.uk")
|
|
66
66
|
}]);
|
|
67
67
|
});
|
|
68
|
+
it('should apply optional validators when specified', function () {
|
|
69
|
+
var ID = 'container';
|
|
70
|
+
var DATA = {};
|
|
71
|
+
var ADDITIONAL_VALIDATION = [{
|
|
72
|
+
function: 'mustEnterAtLeastOne',
|
|
73
|
+
message: 'Must enter at least one'
|
|
74
|
+
}];
|
|
75
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.DATE, null, false, ADDITIONAL_VALIDATION);
|
|
76
|
+
expect((0, _validateContainer.default)(COMPONENT, DATA)).toEqual({
|
|
77
|
+
error: 'Must enter at least one',
|
|
78
|
+
id: ID
|
|
79
|
+
});
|
|
80
|
+
});
|
|
68
81
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ukhomeoffice/cop-react-form-renderer",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.25.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": "1.13.
|
|
19
|
+
"@ukhomeoffice/cop-react-components": "1.13.4",
|
|
20
20
|
"axios": "^0.23.0",
|
|
21
21
|
"dayjs": "^1.11.0",
|
|
22
22
|
"govuk-frontend": "^3.13.0",
|