@ukhomeoffice/cop-react-form-renderer 4.15.0-alpha → 4.15.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.
- package/dist/components/CollectionPage/CollectionPage.js +1 -0
- package/dist/components/FormComponent/Collection.js +2 -0
- package/dist/components/FormComponent/Container.js +4 -2
- package/dist/components/FormComponent/Container.test.js +18 -8
- package/dist/components/FormComponent/FormComponent.js +8 -4
- package/dist/components/FormPage/FormPage.js +2 -0
- package/dist/components/FormRenderer/FormRenderer.js +1 -1
- package/dist/utils/CheckYourAnswers/getCYARowsForCollectionPage.js +4 -1
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +4 -1
- package/dist/utils/Component/elevateNestedComponents.js +52 -0
- package/dist/utils/Component/elevateNestedComponents.test.js +92 -0
- package/dist/utils/Component/getComponent.js +12 -6
- package/dist/utils/Component/getComponentTests/getComponent.calculation.test.js +37 -23
- package/dist/utils/Component/index.js +3 -0
- package/dist/utils/Meta/documents/setDocumentForField.js +8 -4
- package/dist/utils/Meta/documents/setDocumentForField.test.js +11 -0
- package/package.json +2 -2
|
@@ -120,6 +120,7 @@ var CollectionPage = function CollectionPage(_ref) {
|
|
|
120
120
|
}),
|
|
121
121
|
onAction: onInnerPageAction,
|
|
122
122
|
onWrapperChange: onInnerPageChange,
|
|
123
|
+
onTopLevelChange: onPageChange,
|
|
123
124
|
classBlock: classBlock,
|
|
124
125
|
classModifiers: classModifiers,
|
|
125
126
|
className: className
|
|
@@ -57,6 +57,7 @@ var Collection = function Collection(_ref) {
|
|
|
57
57
|
_value = _ref.value,
|
|
58
58
|
formData = _ref.formData,
|
|
59
59
|
onChange = _ref.onChange,
|
|
60
|
+
onTopLevelChange = _ref.onTopLevelChange,
|
|
60
61
|
wrap = _ref.wrap;
|
|
61
62
|
|
|
62
63
|
var _useState = (0, _react.useState)(_value),
|
|
@@ -195,6 +196,7 @@ var Collection = function Collection(_ref) {
|
|
|
195
196
|
value: item,
|
|
196
197
|
formData: formData,
|
|
197
198
|
onChange: onCollectionChange,
|
|
199
|
+
onTopLevelChange: onTopLevelChange,
|
|
198
200
|
wrap: wrap
|
|
199
201
|
}));
|
|
200
202
|
}), !config.disableAddAndRemove && /*#__PURE__*/_react.default.createElement(_copReactComponents.ButtonGroup, null, /*#__PURE__*/_react.default.createElement(_copReactComponents.Button, {
|
|
@@ -15,7 +15,7 @@ var _utils = _interopRequireDefault(require("../../utils"));
|
|
|
15
15
|
|
|
16
16
|
var _FormComponent = _interopRequireDefault(require("./FormComponent"));
|
|
17
17
|
|
|
18
|
-
var _excluded = ["container", "value", "formData", "onChange", "wrap"];
|
|
18
|
+
var _excluded = ["container", "value", "formData", "onChange", "wrap", "onTopLevelChange"];
|
|
19
19
|
|
|
20
20
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
21
|
|
|
@@ -40,6 +40,7 @@ var Container = function Container(_ref) {
|
|
|
40
40
|
formData = _ref.formData,
|
|
41
41
|
onChange = _ref.onChange,
|
|
42
42
|
wrap = _ref.wrap,
|
|
43
|
+
onTopLevelChange = _ref.onTopLevelChange,
|
|
43
44
|
attrs = _objectWithoutProperties(_ref, _excluded);
|
|
44
45
|
|
|
45
46
|
var onComponentChange = function onComponentChange(_ref2) {
|
|
@@ -88,7 +89,8 @@ var Container = function Container(_ref) {
|
|
|
88
89
|
formData: formData,
|
|
89
90
|
value: val || defaultValue,
|
|
90
91
|
wrap: wrap,
|
|
91
|
-
onChange: onComponentChange
|
|
92
|
+
onChange: onComponentChange,
|
|
93
|
+
onTopLevelChange: onTopLevelChange
|
|
92
94
|
}));
|
|
93
95
|
}));
|
|
94
96
|
};
|
|
@@ -212,7 +212,7 @@ describe('components.FormComponent.Container', function () {
|
|
|
212
212
|
}, _callee3);
|
|
213
213
|
})));
|
|
214
214
|
it('should handle a change to a file type component appropriately', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
|
|
215
|
-
var FILE_ID, ON_CHANGE_EVENTS, ON_CHANGE, CONTAINER, _renderWithValidation4, container, c, formGroup, input, FILE_EXTENSION, FILE_NAME, FILE_TYPE, str, blob, FILE, containerChangeValue, metaChangeValue;
|
|
215
|
+
var FILE_ID, ON_CHANGE_EVENTS, ON_CHANGE, ON_TOP_LEVEL_CHANGE_EVENTS, ON_TOP_LEVEL_CHANGE, CONTAINER, _renderWithValidation4, container, c, formGroup, input, FILE_EXTENSION, FILE_NAME, FILE_TYPE, str, blob, FILE, containerChangeValue, metaChangeValue;
|
|
216
216
|
|
|
217
217
|
return regeneratorRuntime.wrap(function _callee4$(_context4) {
|
|
218
218
|
while (1) {
|
|
@@ -225,6 +225,12 @@ describe('components.FormComponent.Container', function () {
|
|
|
225
225
|
ON_CHANGE_EVENTS.push(e);
|
|
226
226
|
};
|
|
227
227
|
|
|
228
|
+
ON_TOP_LEVEL_CHANGE_EVENTS = [];
|
|
229
|
+
|
|
230
|
+
ON_TOP_LEVEL_CHANGE = function ON_TOP_LEVEL_CHANGE(e) {
|
|
231
|
+
ON_TOP_LEVEL_CHANGE_EVENTS.push(e);
|
|
232
|
+
};
|
|
233
|
+
|
|
228
234
|
CONTAINER = {
|
|
229
235
|
id: ID,
|
|
230
236
|
fieldId: ID,
|
|
@@ -241,7 +247,8 @@ describe('components.FormComponent.Container', function () {
|
|
|
241
247
|
component: CONTAINER,
|
|
242
248
|
value: FORM_DATA[ID],
|
|
243
249
|
formData: FORM_DATA,
|
|
244
|
-
onChange: ON_CHANGE
|
|
250
|
+
onChange: ON_CHANGE,
|
|
251
|
+
onTopLevelChange: ON_TOP_LEVEL_CHANGE
|
|
245
252
|
})), container = _renderWithValidation4.container; // Get hold of the text input.
|
|
246
253
|
|
|
247
254
|
c = container.childNodes[0];
|
|
@@ -258,22 +265,25 @@ describe('components.FormComponent.Container', function () {
|
|
|
258
265
|
type: FILE_TYPE
|
|
259
266
|
});
|
|
260
267
|
|
|
261
|
-
_userEvent.default.upload(input, FILE); //
|
|
268
|
+
_userEvent.default.upload(input, FILE); // Expect one call for the regular onChange event, this can change
|
|
269
|
+
// depending on whether the Container is on a normal page or regular page.
|
|
262
270
|
|
|
263
271
|
|
|
264
|
-
expect(ON_CHANGE_EVENTS.length).toEqual(
|
|
272
|
+
expect(ON_CHANGE_EVENTS.length).toEqual(1);
|
|
273
|
+
expect(ON_CHANGE_EVENTS[0].target.name).toEqual(ID); // Expect one call for changing the meta data at top level formData.
|
|
274
|
+
// Meta changes should occur at top-level regardless of where the component is.
|
|
265
275
|
|
|
266
|
-
expect(
|
|
267
|
-
expect(
|
|
276
|
+
expect(ON_TOP_LEVEL_CHANGE_EVENTS.length).toEqual(1);
|
|
277
|
+
expect(ON_TOP_LEVEL_CHANGE_EVENTS[0].target.name).toEqual(_constants.META_PROPERTY);
|
|
268
278
|
containerChangeValue = JSON.parse(JSON.stringify(ON_CHANGE_EVENTS[0].target.value));
|
|
269
|
-
metaChangeValue = JSON.parse(JSON.stringify(
|
|
279
|
+
metaChangeValue = JSON.parse(JSON.stringify(ON_TOP_LEVEL_CHANGE_EVENTS[0].target.value));
|
|
270
280
|
expect(metaChangeValue.documents.length).toEqual(2); // Existing one, plus this new one.
|
|
271
281
|
|
|
272
282
|
expect(metaChangeValue.documents[1]).toMatchObject(_objectSpread(_objectSpread({}, containerChangeValue[FILE_ID]), {}, {
|
|
273
283
|
field: "".concat(ID, ".").concat(FILE_ID)
|
|
274
284
|
}));
|
|
275
285
|
|
|
276
|
-
case
|
|
286
|
+
case 25:
|
|
277
287
|
case "end":
|
|
278
288
|
return _context4.stop();
|
|
279
289
|
}
|
|
@@ -25,7 +25,7 @@ var _Container = _interopRequireDefault(require("./Container"));
|
|
|
25
25
|
|
|
26
26
|
var _helpers = require("./helpers");
|
|
27
27
|
|
|
28
|
-
var _excluded = ["component", "value", "formData", "wrap", "onChange"];
|
|
28
|
+
var _excluded = ["component", "value", "formData", "wrap", "onChange", "onTopLevelChange"];
|
|
29
29
|
|
|
30
30
|
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); }
|
|
31
31
|
|
|
@@ -63,6 +63,7 @@ var FormComponent = function FormComponent(_ref) {
|
|
|
63
63
|
formData = _ref.formData,
|
|
64
64
|
wrap = _ref.wrap,
|
|
65
65
|
onChange = _ref.onChange,
|
|
66
|
+
onTopLevelChange = _ref.onTopLevelChange,
|
|
66
67
|
attrs = _objectWithoutProperties(_ref, _excluded);
|
|
67
68
|
|
|
68
69
|
var _useHooks = (0, _hooks.useHooks)(),
|
|
@@ -103,6 +104,7 @@ var FormComponent = function FormComponent(_ref) {
|
|
|
103
104
|
container: component,
|
|
104
105
|
wrap: wrap,
|
|
105
106
|
onChange: onComponentChange,
|
|
107
|
+
onTopLevelChange: onTopLevelChange,
|
|
106
108
|
value: value || _utils.default.Component.defaultValue(component),
|
|
107
109
|
formData: formData
|
|
108
110
|
}));
|
|
@@ -119,16 +121,17 @@ var FormComponent = function FormComponent(_ref) {
|
|
|
119
121
|
config: component,
|
|
120
122
|
wrap: wrap,
|
|
121
123
|
onChange: onComponentChange,
|
|
124
|
+
onTopLevelChange: onTopLevelChange,
|
|
122
125
|
value: value || _utils.default.Component.defaultValue(component),
|
|
123
126
|
formData: formData
|
|
124
127
|
}));
|
|
125
128
|
}
|
|
126
129
|
|
|
127
130
|
var changeMetaDocuments = function changeMetaDocuments(document) {
|
|
128
|
-
|
|
131
|
+
onTopLevelChange({
|
|
129
132
|
target: {
|
|
130
133
|
name: _utils.default.Meta.name,
|
|
131
|
-
value: _utils.default.Meta.documents.setForField(document, formData, component.full_path)
|
|
134
|
+
value: _utils.default.Meta.documents.setForField(document, formData, component.full_path, component.allowMultiple)
|
|
132
135
|
}
|
|
133
136
|
});
|
|
134
137
|
};
|
|
@@ -174,7 +177,8 @@ FormComponent.propTypes = {
|
|
|
174
177
|
value: _propTypes.default.any,
|
|
175
178
|
formData: _propTypes.default.any,
|
|
176
179
|
wrap: _propTypes.default.bool,
|
|
177
|
-
onChange: _propTypes.default.func
|
|
180
|
+
onChange: _propTypes.default.func,
|
|
181
|
+
onTopLevelChange: _propTypes.default.func
|
|
178
182
|
};
|
|
179
183
|
FormComponent.defaultProps = {
|
|
180
184
|
wrap: true
|
|
@@ -56,6 +56,7 @@ var FormPage = function FormPage(_ref) {
|
|
|
56
56
|
var page = _ref.page,
|
|
57
57
|
_onAction = _ref.onAction,
|
|
58
58
|
onWrapperChange = _ref.onWrapperChange,
|
|
59
|
+
onTopLevelChange = _ref.onTopLevelChange,
|
|
59
60
|
classBlock = _ref.classBlock,
|
|
60
61
|
classModifiers = _ref.classModifiers,
|
|
61
62
|
className = _ref.className;
|
|
@@ -102,6 +103,7 @@ var FormPage = function FormPage(_ref) {
|
|
|
102
103
|
key: index,
|
|
103
104
|
component: component,
|
|
104
105
|
onChange: onWrapperChange || onPageChange,
|
|
106
|
+
onTopLevelChange: onTopLevelChange || onPageChange,
|
|
105
107
|
value: page.formData[component.fieldId] || patch[component.fieldId],
|
|
106
108
|
formData: page.formData
|
|
107
109
|
});
|
|
@@ -434,7 +434,7 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
|
|
|
434
434
|
}
|
|
435
435
|
} else if (currentTask.state === _models.TaskStates.TYPES.IN_PROGRESS) {
|
|
436
436
|
var _currentPage = data.formStatus.tasks[currentTask.name].currentPage;
|
|
437
|
-
onPageChange(
|
|
437
|
+
onPageChange(_currentPage || currentTask.pages[0]);
|
|
438
438
|
} else {
|
|
439
439
|
onPageChange(currentTask.pages[0]);
|
|
440
440
|
}
|
|
@@ -7,6 +7,8 @@ exports.default = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _copReactComponents = require("@ukhomeoffice/cop-react-components");
|
|
9
9
|
|
|
10
|
+
var _elevateNestedComponents = _interopRequireDefault(require("../Component/elevateNestedComponents"));
|
|
11
|
+
|
|
10
12
|
var _models = require("../../models");
|
|
11
13
|
|
|
12
14
|
var _getCYARowsForContainer = _interopRequireDefault(require("./getCYARowsForContainer"));
|
|
@@ -24,13 +26,14 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
24
26
|
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; }
|
|
25
27
|
|
|
26
28
|
var getContainerForPage = function getContainerForPage(page, item, labelCount, full_path) {
|
|
29
|
+
var allComponents = (0, _elevateNestedComponents.default)(page.components, item);
|
|
27
30
|
return {
|
|
28
31
|
id: item.id,
|
|
29
32
|
fieldId: item.id,
|
|
30
33
|
type: _models.ComponentTypes.CONTAINER,
|
|
31
34
|
required: true,
|
|
32
35
|
full_path: full_path,
|
|
33
|
-
components:
|
|
36
|
+
components: allComponents.map(function (component) {
|
|
34
37
|
return _objectSpread(_objectSpread({}, component), {}, {
|
|
35
38
|
label: _copReactComponents.Utils.interpolateString(component.label, _objectSpread(_objectSpread({}, item), {}, {
|
|
36
39
|
index: labelCount
|
|
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
+
var _elevateNestedComponents = _interopRequireDefault(require("../Component/elevateNestedComponents"));
|
|
9
|
+
|
|
8
10
|
var _models = require("../../models");
|
|
9
11
|
|
|
10
12
|
var _FormPage = _interopRequireDefault(require("../FormPage"));
|
|
@@ -41,7 +43,8 @@ var getCYARowsForPage = function getCYARowsForPage(page, onAction) {
|
|
|
41
43
|
return (0, _getCYARowsForCollectionPage.default)(page, onAction);
|
|
42
44
|
}
|
|
43
45
|
|
|
44
|
-
var
|
|
46
|
+
var allComponents = (0, _elevateNestedComponents.default)(page.components, page.formData);
|
|
47
|
+
var rows = allComponents.filter(function (c) {
|
|
45
48
|
return (0, _showComponentCYA.default)(c, page.formData);
|
|
46
49
|
}).flatMap(function (component) {
|
|
47
50
|
switch (component.type) {
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Iterates over an array of components and brings any nested
|
|
10
|
+
* components that should be visible into the top level array.
|
|
11
|
+
* The values provided in data are used to determine if any
|
|
12
|
+
* nested components should be visible.
|
|
13
|
+
*
|
|
14
|
+
* Given this structure in the components array:
|
|
15
|
+
* - Radios
|
|
16
|
+
* - Radio Option 1 (Not selected)
|
|
17
|
+
* - Nested 1
|
|
18
|
+
* - Nested 2
|
|
19
|
+
* - Radio Option 2 (Selected)
|
|
20
|
+
* - Nested 3
|
|
21
|
+
* - Text Input
|
|
22
|
+
*
|
|
23
|
+
* The result will be the following array:
|
|
24
|
+
* - Radios
|
|
25
|
+
* - Nested 3
|
|
26
|
+
* - Text Input
|
|
27
|
+
*
|
|
28
|
+
* @param {*} components An array of components.
|
|
29
|
+
* @param {*} data A chunk of form data used to determine if
|
|
30
|
+
* any nested components are visible or not.
|
|
31
|
+
* @returns A new array consisting of components interspersed
|
|
32
|
+
* with any visible nested components.
|
|
33
|
+
*/
|
|
34
|
+
var elevateNestedComponents = function elevateNestedComponents(components, data) {
|
|
35
|
+
var allComponents = [];
|
|
36
|
+
components === null || components === void 0 ? void 0 : components.forEach(function (component) {
|
|
37
|
+
var _component$data, _component$data$optio;
|
|
38
|
+
|
|
39
|
+
allComponents.push(component);
|
|
40
|
+
(_component$data = component.data) === null || _component$data === void 0 ? void 0 : (_component$data$optio = _component$data.options) === null || _component$data$optio === void 0 ? void 0 : _component$data$optio.forEach(function (option) {
|
|
41
|
+
// If this option has nested components and is
|
|
42
|
+
// selected, then add its nested components to the array.
|
|
43
|
+
if (Array.isArray(option.nested) && data[component.id] === option.value) {
|
|
44
|
+
allComponents = allComponents.concat(option.nested);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
return allComponents;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
var _default = elevateNestedComponents;
|
|
52
|
+
exports.default = _default;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _elevateNestedComponents = _interopRequireDefault(require("./elevateNestedComponents"));
|
|
4
|
+
|
|
5
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
6
|
+
|
|
7
|
+
describe('utils.Component.elevateNestedComponents', function () {
|
|
8
|
+
it('should return an empty array if components is undefined', function () {
|
|
9
|
+
var DATA = {
|
|
10
|
+
alpha: 'abc'
|
|
11
|
+
};
|
|
12
|
+
expect((0, _elevateNestedComponents.default)(undefined, DATA)).toEqual([]);
|
|
13
|
+
});
|
|
14
|
+
it('should return an empty array if components is null', function () {
|
|
15
|
+
var DATA = {
|
|
16
|
+
alpha: 'abc'
|
|
17
|
+
};
|
|
18
|
+
expect((0, _elevateNestedComponents.default)(null, DATA)).toEqual([]);
|
|
19
|
+
});
|
|
20
|
+
it('should return an empty array if components is empty', function () {
|
|
21
|
+
var DATA = {
|
|
22
|
+
alpha: 'abc'
|
|
23
|
+
};
|
|
24
|
+
expect((0, _elevateNestedComponents.default)([], DATA)).toEqual([]);
|
|
25
|
+
});
|
|
26
|
+
it('should return the original components if none have nested components', function () {
|
|
27
|
+
var COMPONENTS = [{
|
|
28
|
+
id: 'textComp'
|
|
29
|
+
}, {
|
|
30
|
+
id: 'radiosComp',
|
|
31
|
+
data: {
|
|
32
|
+
options: [{
|
|
33
|
+
label: 'Yes',
|
|
34
|
+
value: 'yes'
|
|
35
|
+
}, {
|
|
36
|
+
label: 'No',
|
|
37
|
+
value: 'no'
|
|
38
|
+
}]
|
|
39
|
+
}
|
|
40
|
+
}];
|
|
41
|
+
var DATA = {
|
|
42
|
+
alpha: 'abc'
|
|
43
|
+
};
|
|
44
|
+
expect((0, _elevateNestedComponents.default)(COMPONENTS, DATA)).toEqual(COMPONENTS);
|
|
45
|
+
});
|
|
46
|
+
it('should return the original components if data is null', function () {
|
|
47
|
+
var COMPONENTS = [{
|
|
48
|
+
id: 'textComp'
|
|
49
|
+
}, {
|
|
50
|
+
id: 'radiosComp',
|
|
51
|
+
data: {
|
|
52
|
+
options: [{
|
|
53
|
+
label: 'Yes',
|
|
54
|
+
value: 'yes'
|
|
55
|
+
}, {
|
|
56
|
+
label: 'No',
|
|
57
|
+
value: 'no'
|
|
58
|
+
}]
|
|
59
|
+
}
|
|
60
|
+
}];
|
|
61
|
+
expect((0, _elevateNestedComponents.default)(COMPONENTS, null)).toEqual(COMPONENTS);
|
|
62
|
+
});
|
|
63
|
+
it('should return the original array with elevated nested components when they are visible', function () {
|
|
64
|
+
var COMPONENTS = [{
|
|
65
|
+
id: 'textComp'
|
|
66
|
+
}, {
|
|
67
|
+
id: 'radiosComp',
|
|
68
|
+
data: {
|
|
69
|
+
options: [{
|
|
70
|
+
label: 'Yes',
|
|
71
|
+
value: 'yes',
|
|
72
|
+
nested: [{
|
|
73
|
+
id: 'nestedOne'
|
|
74
|
+
}, {
|
|
75
|
+
id: 'nestedTwo'
|
|
76
|
+
}]
|
|
77
|
+
}, {
|
|
78
|
+
label: 'No',
|
|
79
|
+
value: 'no'
|
|
80
|
+
}]
|
|
81
|
+
}
|
|
82
|
+
}];
|
|
83
|
+
var DATA = {
|
|
84
|
+
radiosComp: 'yes'
|
|
85
|
+
};
|
|
86
|
+
expect((0, _elevateNestedComponents.default)(COMPONENTS, DATA)).toEqual([].concat(COMPONENTS, [{
|
|
87
|
+
id: 'nestedOne'
|
|
88
|
+
}, {
|
|
89
|
+
id: 'nestedTwo'
|
|
90
|
+
}]));
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -55,16 +55,22 @@ var getCheckboxes = function getCheckboxes(config) {
|
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
var getCalculation = function getCalculation(config) {
|
|
58
|
-
var
|
|
59
|
-
|
|
60
|
-
if (
|
|
61
|
-
config.value
|
|
62
|
-
|
|
58
|
+
var value = _Data.default.applyFormula(config);
|
|
59
|
+
|
|
60
|
+
if (typeof config.onChange === 'function') {
|
|
61
|
+
if (value !== config.value) {
|
|
62
|
+
config.onChange({
|
|
63
|
+
target: {
|
|
64
|
+
name: config.fieldId,
|
|
65
|
+
value: value
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
63
69
|
}
|
|
64
70
|
|
|
65
71
|
var attrs = (0, _cleanAttributes.default)(config, ['formula']);
|
|
66
72
|
return /*#__PURE__*/_react.default.createElement(_copReactComponents.TextInput, _extends({}, attrs, {
|
|
67
|
-
|
|
73
|
+
readonly: true
|
|
68
74
|
}));
|
|
69
75
|
};
|
|
70
76
|
|
|
@@ -26,6 +26,10 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
26
26
|
|
|
27
27
|
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; }
|
|
28
28
|
|
|
29
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
30
|
+
|
|
31
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
32
|
+
|
|
29
33
|
describe('utils.Component.get', function () {
|
|
30
34
|
var ID = 'test-id';
|
|
31
35
|
var FIELD_ID = 'field-id';
|
|
@@ -134,28 +138,39 @@ describe('utils.Component.get', function () {
|
|
|
134
138
|
},
|
|
135
139
|
result: '266.67'
|
|
136
140
|
}].forEach(function (test) {
|
|
137
|
-
|
|
138
|
-
var
|
|
139
|
-
|
|
141
|
+
test.config.onChange = function (_ref) {
|
|
142
|
+
var target = _ref.target;
|
|
143
|
+
COMPONENT.formData[target.name] = target.value;
|
|
144
|
+
};
|
|
140
145
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
formGroup = _getAllByTestId2[0],
|
|
144
|
-
input = _getAllByTestId2[1];
|
|
146
|
+
it("should render 'calculation' component with formula to given calculated value: ".concat(test.result), /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
|
|
147
|
+
var _render, container, _getAllByTestId, _getAllByTestId2, formGroup, input, label;
|
|
145
148
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
149
|
+
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
150
|
+
while (1) {
|
|
151
|
+
switch (_context.prev = _context.next) {
|
|
152
|
+
case 0:
|
|
153
|
+
_render = (0, _react.render)((0, _getComponent.default)(_objectSpread(_objectSpread({}, COMPONENT), test.config))), container = _render.container;
|
|
154
|
+
_getAllByTestId = (0, _react.getAllByTestId)(container, ID), _getAllByTestId2 = _slicedToArray(_getAllByTestId, 2), formGroup = _getAllByTestId2[0], input = _getAllByTestId2[1];
|
|
155
|
+
expect(formGroup.tagName).toEqual('DIV');
|
|
156
|
+
expect(formGroup.classList).toContain('govuk-form-group');
|
|
157
|
+
label = formGroup.childNodes[0];
|
|
158
|
+
expect(label.tagName).toEqual('LABEL');
|
|
159
|
+
expect(label.classList).toContain('govuk-label');
|
|
160
|
+
expect(label.textContent).toContain(LABEL);
|
|
161
|
+
expect(formGroup.classList).toContain('govuk-form-group');
|
|
162
|
+
expect(input.tagName).toEqual('DIV');
|
|
163
|
+
expect(input.classList).toContain('hods-readonly');
|
|
164
|
+
expect(input.id).toEqual(ID);
|
|
165
|
+
expect("".concat(COMPONENT.formData[FIELD_ID])).toEqual(test.result);
|
|
166
|
+
|
|
167
|
+
case 13:
|
|
168
|
+
case "end":
|
|
169
|
+
return _context.stop();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}, _callee);
|
|
173
|
+
})));
|
|
159
174
|
});
|
|
160
175
|
/**
|
|
161
176
|
* Iteratively test collction of negative test data objects each containing:
|
|
@@ -258,10 +273,9 @@ describe('utils.Component.get', function () {
|
|
|
258
273
|
expect(label.classList).toContain('govuk-label');
|
|
259
274
|
expect(label.textContent).toContain(LABEL);
|
|
260
275
|
expect(formGroup.classList).toContain('govuk-form-group');
|
|
261
|
-
expect(input.tagName).toEqual('
|
|
262
|
-
expect(input.classList).toContain('
|
|
276
|
+
expect(input.tagName).toEqual('DIV');
|
|
277
|
+
expect(input.classList).toContain('hods-readonly');
|
|
263
278
|
expect(input.id).toEqual(ID);
|
|
264
|
-
expect(input.getAttribute('readonly')).not.toBeNull();
|
|
265
279
|
expect(error).toBeCalledTimes(1);
|
|
266
280
|
expect(error).toBeCalledWith(test.error);
|
|
267
281
|
});
|
|
@@ -7,6 +7,8 @@ exports.default = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _cleanAttributes = _interopRequireDefault(require("./cleanAttributes"));
|
|
9
9
|
|
|
10
|
+
var _elevateNestedComponents = _interopRequireDefault(require("./elevateNestedComponents"));
|
|
11
|
+
|
|
10
12
|
var _getComponent = _interopRequireDefault(require("./getComponent"));
|
|
11
13
|
|
|
12
14
|
var _getDefaultValue = _interopRequireDefault(require("./getDefaultValue"));
|
|
@@ -23,6 +25,7 @@ var Component = {
|
|
|
23
25
|
clean: _cleanAttributes.default,
|
|
24
26
|
defaultValue: _getDefaultValue.default,
|
|
25
27
|
editable: _isEditable.default,
|
|
28
|
+
elevateNested: _elevateNestedComponents.default,
|
|
26
29
|
get: _getComponent.default,
|
|
27
30
|
show: _showComponent.default,
|
|
28
31
|
wrap: _wrapInFormGroup.default
|
|
@@ -17,10 +17,14 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
17
17
|
|
|
18
18
|
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; }
|
|
19
19
|
|
|
20
|
-
var setDocumentForField = function setDocumentForField(document, formData, field) {
|
|
21
|
-
var documents = (0, _getDocuments.default)(formData)
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
var setDocumentForField = function setDocumentForField(document, formData, field, allowMultiple) {
|
|
21
|
+
var documents = (0, _getDocuments.default)(formData);
|
|
22
|
+
|
|
23
|
+
if (!allowMultiple) {
|
|
24
|
+
documents = documents.filter(function (d) {
|
|
25
|
+
return d.field !== field;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
24
28
|
|
|
25
29
|
if (document) {
|
|
26
30
|
documents.push(_objectSpread(_objectSpread({}, document), {}, {
|
|
@@ -36,6 +36,17 @@ describe('Utils.Meta.documents.add', function () {
|
|
|
36
36
|
|
|
37
37
|
expect((0, _setDocumentForField.default)(NEW_ALPHA, FORM_DATA, NEW_ALPHA.field)).toEqual(_defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [NEW_ALPHA]));
|
|
38
38
|
});
|
|
39
|
+
it('should add a document with the same field if allowMultiple is true', function () {
|
|
40
|
+
var NEW_URL = 'http://replacement.com/files/alpha';
|
|
41
|
+
var NEW_ALPHA = {
|
|
42
|
+
field: ALPHA_DOCUMENT.field,
|
|
43
|
+
url: NEW_URL
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
var FORM_DATA = _defineProperty({}, _constants.META_PROPERTY, _defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [ALPHA_DOCUMENT]));
|
|
47
|
+
|
|
48
|
+
expect((0, _setDocumentForField.default)(NEW_ALPHA, FORM_DATA, NEW_ALPHA.field, true)).toEqual(_defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [ALPHA_DOCUMENT, NEW_ALPHA]));
|
|
49
|
+
});
|
|
39
50
|
it('should handle a null document being passed where a document with that field already exists', function () {
|
|
40
51
|
var FORM_DATA = _defineProperty({}, _constants.META_PROPERTY, _defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [ALPHA_DOCUMENT]));
|
|
41
52
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ukhomeoffice/cop-react-form-renderer",
|
|
3
|
-
"version": "4.15.
|
|
3
|
+
"version": "4.15.1-alpha",
|
|
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.2.
|
|
19
|
+
"@ukhomeoffice/cop-react-components": "2.2.4",
|
|
20
20
|
"axios": "^0.23.0",
|
|
21
21
|
"dayjs": "^1.11.0",
|
|
22
22
|
"govuk-frontend": "^4.3.1",
|