@ukhomeoffice/cop-react-form-renderer 4.87.0 → 4.89.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/FormRenderer/FormRenderer.test.js +7 -1
- package/dist/components/FormRenderer/onPageAction.js +7 -4
- package/dist/components/FormRenderer/onPageAction.test.js +10 -0
- package/dist/context/HooksContext/HooksContext.js +9 -0
- package/dist/utils/Component/getComponent.js +1 -8
- package/dist/utils/Validate/validateCollection.js +2 -2
- package/dist/utils/Validate/validateCollection.test.js +36 -0
- package/package.json +1 -1
|
@@ -273,7 +273,13 @@ describe('components', function () {
|
|
|
273
273
|
}]);
|
|
274
274
|
};
|
|
275
275
|
HOOKS = {
|
|
276
|
-
onSubmit: ON_SUBMIT
|
|
276
|
+
onSubmit: ON_SUBMIT,
|
|
277
|
+
onAction: function onAction(formData, patch) {
|
|
278
|
+
return {
|
|
279
|
+
formData: formData,
|
|
280
|
+
patch: patch
|
|
281
|
+
};
|
|
282
|
+
}
|
|
277
283
|
};
|
|
278
284
|
_context10.next = 5;
|
|
279
285
|
return (0, _testUtils.act)( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9() {
|
|
@@ -27,16 +27,19 @@ var onPageAction = function onPageAction(action, patch, patchLabel, hooks, data,
|
|
|
27
27
|
;
|
|
28
28
|
// Save a copy of data in case submit errors and we need to revert
|
|
29
29
|
var preSubmitData = _objectSpread({}, data);
|
|
30
|
+
var form = formState;
|
|
31
|
+
var update = hooks.onAction(form.page.formData, patch, data, action.customAction);
|
|
32
|
+
form.page.formData = update.formData;
|
|
33
|
+
var _patch = update.patch;
|
|
34
|
+
|
|
30
35
|
// Re-apply the patch to the page's formData.
|
|
31
36
|
// This should normally have no effect but will prevent issues
|
|
32
37
|
// with validation if formData happens to have been wiped.
|
|
33
|
-
|
|
34
|
-
form.page.formData = _objectSpread(_objectSpread({}, form.page.formData), patch);
|
|
38
|
+
form.page.formData = _objectSpread(_objectSpread({}, form.page.formData), _patch);
|
|
35
39
|
// Check to see whether the action is able to proceed, which in
|
|
36
40
|
// in the case of a submission will validate the fields in the page.
|
|
37
41
|
if (_helpers.default.canActionProceed(action, form.page, validate.page)) {
|
|
38
|
-
|
|
39
|
-
_patch = _helpers.default.cleanHiddenNestedData(patch, form.page);
|
|
42
|
+
_patch = _helpers.default.cleanHiddenNestedData(_patch, form.page);
|
|
40
43
|
if (action.addToFormData) {
|
|
41
44
|
var operations = Array.isArray(action.addToFormData) ? action.addToFormData : [action.addToFormData];
|
|
42
45
|
operations.forEach(function (op) {
|
|
@@ -130,6 +130,15 @@ describe('components.FormRenderer.onPageAction', function () {
|
|
|
130
130
|
this.onSubmitArgs = [];
|
|
131
131
|
this.onFormCompleteCalls = 0;
|
|
132
132
|
this.onCancelCalls = 0;
|
|
133
|
+
this.onActionCalls = 0;
|
|
134
|
+
},
|
|
135
|
+
onActionCalls: 0,
|
|
136
|
+
onAction: function onAction(formData, patch) {
|
|
137
|
+
this.onActionCalls += 1;
|
|
138
|
+
return {
|
|
139
|
+
formData: formData,
|
|
140
|
+
patch: patch
|
|
141
|
+
};
|
|
133
142
|
}
|
|
134
143
|
};
|
|
135
144
|
var MOCK_VALIDATE = {
|
|
@@ -260,6 +269,7 @@ describe('components.FormRenderer.onPageAction', function () {
|
|
|
260
269
|
changedFieldValue: argsUsed.patchLabel[firstKey]
|
|
261
270
|
});
|
|
262
271
|
}
|
|
272
|
+
expect(MOCK_HOOKS.onActionCalls).toEqual(1);
|
|
263
273
|
|
|
264
274
|
// Both the success and error paths of the onSubmit hook are tested
|
|
265
275
|
// together to make sure the correct function chains are called for
|
|
@@ -22,6 +22,12 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
|
|
|
22
22
|
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
23
23
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
24
24
|
var DEFAULT_HOOKS = {
|
|
25
|
+
onAction: function onAction(formData, patch) {
|
|
26
|
+
return {
|
|
27
|
+
formData: formData,
|
|
28
|
+
patch: patch
|
|
29
|
+
};
|
|
30
|
+
},
|
|
25
31
|
onChange: function onChange(data) {},
|
|
26
32
|
onFormComplete: function onFormComplete() {},
|
|
27
33
|
onFormLoad: function onFormLoad(setCurrentTask) {},
|
|
@@ -61,6 +67,7 @@ var HooksContextProvider = function HooksContextProvider(_ref) {
|
|
|
61
67
|
var overrides = _ref.overrides,
|
|
62
68
|
children = _ref.children;
|
|
63
69
|
var _useState = (0, _react.useState)({
|
|
70
|
+
onAction: (overrides === null || overrides === void 0 ? void 0 : overrides.onAction) || DEFAULT_HOOKS.onAction,
|
|
64
71
|
onChange: (overrides === null || overrides === void 0 ? void 0 : overrides.onChange) || DEFAULT_HOOKS.onChange,
|
|
65
72
|
onFormComplete: (overrides === null || overrides === void 0 ? void 0 : overrides.onFormComplete) || DEFAULT_HOOKS.onFormComplete,
|
|
66
73
|
onFormLoad: (overrides === null || overrides === void 0 ? void 0 : overrides.onFormLoad) || DEFAULT_HOOKS.onFormLoad,
|
|
@@ -104,6 +111,7 @@ exports.default = _default;
|
|
|
104
111
|
HooksContextProvider.propTypes = {
|
|
105
112
|
children: _propTypes.default.node,
|
|
106
113
|
overrides: _propTypes.default.shape({
|
|
114
|
+
onAction: _propTypes.default.func,
|
|
107
115
|
onCancel: _propTypes.default.func,
|
|
108
116
|
onChange: _propTypes.default.func,
|
|
109
117
|
onFormComplete: _propTypes.default.func,
|
|
@@ -121,6 +129,7 @@ HooksContextProvider.propTypes = {
|
|
|
121
129
|
HooksContextProvider.defaultProps = {
|
|
122
130
|
children: null,
|
|
123
131
|
overrides: {
|
|
132
|
+
onAction: undefined,
|
|
124
133
|
onCancel: undefined,
|
|
125
134
|
onChange: undefined,
|
|
126
135
|
onFormComplete: undefined,
|
|
@@ -22,7 +22,6 @@ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input ==
|
|
|
22
22
|
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
23
23
|
var LIST_CLASS = 'govuk-list';
|
|
24
24
|
var BODY_CLASS = 'govuk-body';
|
|
25
|
-
var INPUT_CLASS = 'govuk-input';
|
|
26
25
|
|
|
27
26
|
/**
|
|
28
27
|
* Separate function for each component type for the sake of
|
|
@@ -159,13 +158,7 @@ var getTextArea = function getTextArea(config) {
|
|
|
159
158
|
};
|
|
160
159
|
var getTextInput = function getTextInput(config) {
|
|
161
160
|
var attrs = (0, _cleanAttributes.default)(config);
|
|
162
|
-
|
|
163
|
-
var className = attrs.fluidWidth ? [attrs.fluidWidth] : undefined;
|
|
164
|
-
return /*#__PURE__*/_react.default.createElement(_copReactComponents.TextInput, _extends({}, attrs, {
|
|
165
|
-
classBlock: INPUT_CLASS,
|
|
166
|
-
classModifiers: classModifiers,
|
|
167
|
-
className: className
|
|
168
|
-
}));
|
|
161
|
+
return /*#__PURE__*/_react.default.createElement(_copReactComponents.TextInput, attrs);
|
|
169
162
|
};
|
|
170
163
|
var getTime = function getTime(config) {
|
|
171
164
|
var attrs = (0, _cleanAttributes.default)(config);
|
|
@@ -32,10 +32,10 @@ var validateCollection = function validateCollection(collection, items, formData
|
|
|
32
32
|
var newComponent = _objectSpread(_objectSpread({}, component), {}, {
|
|
33
33
|
full_path: "".concat(fullPath, ".").concat(component.fieldId)
|
|
34
34
|
});
|
|
35
|
-
if (Array.isArray(component.custom_errors)) {
|
|
35
|
+
if (component.custom_errors && Array.isArray(component.custom_errors)) {
|
|
36
36
|
newComponent.custom_errors = component.custom_errors.map(function (error) {
|
|
37
37
|
return _objectSpread(_objectSpread({}, error), {}, {
|
|
38
|
-
message: _copReactComponents.Utils.interpolateString(error.message, {
|
|
38
|
+
message: _copReactComponents.Utils.interpolateString(index === 0 && error.firstOnlyMessage ? error.firstOnlyMessage : error.message, {
|
|
39
39
|
index: labelCount
|
|
40
40
|
})
|
|
41
41
|
});
|
|
@@ -86,4 +86,40 @@ describe('utils.Validate.Collection', function () {
|
|
|
86
86
|
id: "".concat(ID, "[0].").concat(COLLECTION.type)
|
|
87
87
|
}]);
|
|
88
88
|
});
|
|
89
|
+
it('should correctly display firstOnlyMessage error message', function () {
|
|
90
|
+
var ID = 'container';
|
|
91
|
+
var LABEL = 'field';
|
|
92
|
+
var TYPE = 'autocomplete';
|
|
93
|
+
var ITEM = setup(ID, TYPE, LABEL, true, []);
|
|
94
|
+
ITEM.custom_errors = [{
|
|
95
|
+
"type": "required",
|
|
96
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
97
|
+
"message": "Test to check that ${index} is 1",
|
|
98
|
+
"firstOnlyMessage": "Test for first message"
|
|
99
|
+
}];
|
|
100
|
+
var COLLECTION = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false, [ITEM]);
|
|
101
|
+
COLLECTION.countOffset = 0;
|
|
102
|
+
expect((0, _validateCollection.default)(COLLECTION, ITEM.custom_errors)).toEqual([{
|
|
103
|
+
error: "Test for first message",
|
|
104
|
+
id: "".concat(ID, "[0].").concat(COLLECTION.type)
|
|
105
|
+
}]);
|
|
106
|
+
});
|
|
107
|
+
it('should correctly display message if firstOnlyMessage is undefined', function () {
|
|
108
|
+
var ID = 'container';
|
|
109
|
+
var LABEL = 'field';
|
|
110
|
+
var TYPE = 'autocomplete';
|
|
111
|
+
var ITEM = setup(ID, TYPE, LABEL, true, []);
|
|
112
|
+
ITEM.custom_errors = [{
|
|
113
|
+
"type": "required",
|
|
114
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
115
|
+
"message": "Test to check that ${index} is 1",
|
|
116
|
+
"firstOnlyMessage": undefined
|
|
117
|
+
}];
|
|
118
|
+
var COLLECTION = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false, [ITEM]);
|
|
119
|
+
COLLECTION.countOffset = 0;
|
|
120
|
+
expect((0, _validateCollection.default)(COLLECTION, ITEM.custom_errors)).toEqual([{
|
|
121
|
+
error: "Test to check that 1 is 1",
|
|
122
|
+
id: "".concat(ID, "[0].").concat(COLLECTION.type)
|
|
123
|
+
}]);
|
|
124
|
+
});
|
|
89
125
|
});
|