@ukhomeoffice/cop-react-form-renderer 3.0.3 → 3.2.2
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/Collection.js +1 -5
- package/dist/components/FormComponent/Collection.test.js +322 -0
- package/dist/components/FormRenderer/FormRenderer.js +36 -13
- package/dist/components/FormRenderer/helpers/cleanHiddenNestedData.js +38 -0
- package/dist/components/FormRenderer/helpers/cleanHiddenNestedData.test.js +50 -0
- package/dist/components/FormRenderer/helpers/index.js +3 -0
- package/dist/context/HooksContext/HooksContext.js +8 -2
- package/dist/utils/CheckYourAnswers/getCYARow.js +3 -1
- package/dist/utils/CheckYourAnswers/getCYARow.test.js +3 -3
- package/dist/utils/Component/cleanAttributes.js +1 -1
- package/dist/utils/Component/getComponent.js +38 -20
- package/dist/utils/Component/getComponentTests/getComponent.nested.test.js +49 -26
- package/dist/utils/Component/getComponentTests/getComponent.time.test.js +106 -0
- package/dist/utils/Component/showComponent.js +6 -0
- package/dist/utils/Component/showComponent.test.js +63 -0
- package/dist/utils/Component/wrapInFormGroup.js +5 -2
- package/dist/utils/Condition/index.js +3 -0
- package/dist/utils/Condition/meetsOneCondition.js +40 -0
- package/dist/utils/Condition/meetsOneCondition.test.js +101 -0
- package/dist/utils/Condition/setupConditions.js +5 -4
- package/dist/utils/Container/showContainer.js +4 -0
- package/dist/utils/Container/showContainer.test.js +51 -0
- package/dist/utils/FormPage/showFormPage.js +4 -0
- package/dist/utils/FormPage/showFormPage.test.js +51 -0
- package/dist/utils/Validate/validateComponent.js +8 -15
- package/dist/utils/Validate/validateComponent.test.js +13 -14
- package/package.json +2 -2
|
@@ -125,4 +125,55 @@ describe('utils.Container.showContainer', function () {
|
|
|
125
125
|
};
|
|
126
126
|
expect((0, _showContainer.default)(CONTAINER, DATA)).toBeTruthy();
|
|
127
127
|
});
|
|
128
|
+
it('SHOULD be shown when the container has multiple show_when conditions, with type "or" provided and ALL are matched', function () {
|
|
129
|
+
var CONTAINER = {
|
|
130
|
+
show_when: {
|
|
131
|
+
"type": "or",
|
|
132
|
+
"conditions": [{
|
|
133
|
+
field: 'alpha',
|
|
134
|
+
op: '=',
|
|
135
|
+
value: 'Alpha'
|
|
136
|
+
}, {
|
|
137
|
+
field: 'bravo',
|
|
138
|
+
op: '=',
|
|
139
|
+
value: 'Bravo'
|
|
140
|
+
}]
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
expect((0, _showContainer.default)(CONTAINER, DATA)).toBeTruthy();
|
|
144
|
+
});
|
|
145
|
+
it('SHOULD be shown when the container has multiple show_when conditions, with type "or" provided and at least ONE is matched', function () {
|
|
146
|
+
var CONTAINER = {
|
|
147
|
+
show_when: {
|
|
148
|
+
"type": "or",
|
|
149
|
+
"conditions": [{
|
|
150
|
+
field: 'alpha',
|
|
151
|
+
op: '=',
|
|
152
|
+
value: 'Alpha'
|
|
153
|
+
}, {
|
|
154
|
+
field: 'charlie',
|
|
155
|
+
op: '=',
|
|
156
|
+
value: 'Charlie'
|
|
157
|
+
}]
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
expect((0, _showContainer.default)(CONTAINER, DATA)).toBeTruthy();
|
|
161
|
+
});
|
|
162
|
+
it('should NOT be shown when the container has multiple show_when conditions, with type "or" provided and NONE are matched', function () {
|
|
163
|
+
var CONTAINER = {
|
|
164
|
+
show_when: {
|
|
165
|
+
"type": "or",
|
|
166
|
+
"conditions": [{
|
|
167
|
+
field: 'alpha',
|
|
168
|
+
op: '!=',
|
|
169
|
+
value: 'Alpha'
|
|
170
|
+
}, {
|
|
171
|
+
field: 'charlie',
|
|
172
|
+
op: '=',
|
|
173
|
+
value: 'Charlie'
|
|
174
|
+
}]
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
expect((0, _showContainer.default)(CONTAINER, DATA)).toBeFalsy();
|
|
178
|
+
});
|
|
128
179
|
});
|
|
@@ -41,6 +41,10 @@ var showFormPage = function showFormPage(page, data) {
|
|
|
41
41
|
|
|
42
42
|
|
|
43
43
|
if (page.show_when) {
|
|
44
|
+
if (page.show_when.type === "or") {
|
|
45
|
+
return _Condition.default.meetsOne(page, data);
|
|
46
|
+
}
|
|
47
|
+
|
|
44
48
|
return _Condition.default.meetsAll(page, data);
|
|
45
49
|
} // If the page itself doesn't have a show_when, we need to make sure that if it
|
|
46
50
|
// contains ANY editable components, at least one of them is shown.
|
|
@@ -126,6 +126,57 @@ describe('utils', function () {
|
|
|
126
126
|
};
|
|
127
127
|
expect((0, _showFormPage.default)(PAGE, DATA)).toBeTruthy();
|
|
128
128
|
});
|
|
129
|
+
it('SHOULD be shown when the page has multiple show_when conditions, with type "or" provided and ALL are matched', function () {
|
|
130
|
+
var PAGE = {
|
|
131
|
+
show_when: {
|
|
132
|
+
"type": "or",
|
|
133
|
+
"conditions": [{
|
|
134
|
+
field: 'alpha',
|
|
135
|
+
op: '=',
|
|
136
|
+
value: 'Alpha'
|
|
137
|
+
}, {
|
|
138
|
+
field: 'bravo',
|
|
139
|
+
op: '=',
|
|
140
|
+
value: 'Bravo'
|
|
141
|
+
}]
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
expect((0, _showFormPage.default)(PAGE, DATA)).toBeTruthy();
|
|
145
|
+
});
|
|
146
|
+
it('SHOULD be shown when the page has multiple show_when conditions, with type "or" provided and at least ONE is matched', function () {
|
|
147
|
+
var PAGE = {
|
|
148
|
+
show_when: {
|
|
149
|
+
"type": "or",
|
|
150
|
+
"conditions": [{
|
|
151
|
+
field: 'alpha',
|
|
152
|
+
op: '=',
|
|
153
|
+
value: 'Alpha'
|
|
154
|
+
}, {
|
|
155
|
+
field: 'charlie',
|
|
156
|
+
op: '=',
|
|
157
|
+
value: 'Charlie'
|
|
158
|
+
}]
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
expect((0, _showFormPage.default)(PAGE, DATA)).toBeTruthy();
|
|
162
|
+
});
|
|
163
|
+
it('should NOT be shown when the page has multiple show_when conditions, with type "or" provided and NONE are matched', function () {
|
|
164
|
+
var PAGE = {
|
|
165
|
+
show_when: {
|
|
166
|
+
"type": "or",
|
|
167
|
+
"conditions": [{
|
|
168
|
+
field: 'alpha',
|
|
169
|
+
op: '!=',
|
|
170
|
+
value: 'Alpha'
|
|
171
|
+
}, {
|
|
172
|
+
field: 'charlie',
|
|
173
|
+
op: '=',
|
|
174
|
+
value: 'Charlie'
|
|
175
|
+
}]
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
expect((0, _showFormPage.default)(PAGE, DATA)).toBeFalsy();
|
|
179
|
+
});
|
|
129
180
|
});
|
|
130
181
|
});
|
|
131
182
|
});
|
|
@@ -50,7 +50,6 @@ var validateComponent = function validateComponent(component, outerData, formDat
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
var error = undefined;
|
|
53
|
-
var nestedId = undefined;
|
|
54
53
|
var properties = undefined;
|
|
55
54
|
var data = outerData && _typeof(outerData) === 'object' ? outerData : {};
|
|
56
55
|
var value = data[component.fieldId];
|
|
@@ -81,21 +80,15 @@ var validateComponent = function validateComponent(component, outerData, formDat
|
|
|
81
80
|
break;
|
|
82
81
|
|
|
83
82
|
case _models.ComponentTypes.RADIOS:
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (nestedError) {
|
|
91
|
-
error = nestedError.error;
|
|
92
|
-
nestedId = nestedError.id;
|
|
93
|
-
}
|
|
83
|
+
var nestedErrors = [];
|
|
84
|
+
(_component$data$optio = component.data.options) === null || _component$data$optio === void 0 ? void 0 : _component$data$optio.forEach(function (option) {
|
|
85
|
+
if (option.nested && formData[component.id] === option.value) {
|
|
86
|
+
nestedErrors = nestedErrors.concat((0, _validateContainer.default)({
|
|
87
|
+
components: option.nested
|
|
88
|
+
}, formData));
|
|
94
89
|
}
|
|
95
|
-
|
|
96
|
-
return nestedError;
|
|
97
90
|
});
|
|
98
|
-
|
|
91
|
+
return nestedErrors;
|
|
99
92
|
|
|
100
93
|
default:
|
|
101
94
|
break;
|
|
@@ -120,7 +113,7 @@ var validateComponent = function validateComponent(component, outerData, formDat
|
|
|
120
113
|
|
|
121
114
|
if (error) {
|
|
122
115
|
return {
|
|
123
|
-
id:
|
|
116
|
+
id: component.full_path || component.id,
|
|
124
117
|
error: error,
|
|
125
118
|
properties: properties
|
|
126
119
|
};
|
|
@@ -211,17 +211,16 @@ describe('utils.Validate.Component', function () {
|
|
|
211
211
|
id: 'a',
|
|
212
212
|
data: {
|
|
213
213
|
options: [{
|
|
214
|
-
nested: {
|
|
214
|
+
nested: [{
|
|
215
215
|
type: 'text',
|
|
216
216
|
fieldId: NESTED_ID,
|
|
217
217
|
id: NESTED_ID,
|
|
218
|
-
required: true
|
|
219
|
-
|
|
220
|
-
}
|
|
218
|
+
required: true
|
|
219
|
+
}]
|
|
221
220
|
}]
|
|
222
221
|
}
|
|
223
222
|
};
|
|
224
|
-
expect((0, _validateComponent.default)(COMPONENT, FORMDATA)).
|
|
223
|
+
expect((0, _validateComponent.default)(COMPONENT, undefined, FORMDATA)).toEqual([]);
|
|
225
224
|
});
|
|
226
225
|
it('should return an error when the radio component contains nested components with errors', function () {
|
|
227
226
|
var NESTED_ID = 'nestedId';
|
|
@@ -231,20 +230,19 @@ describe('utils.Validate.Component', function () {
|
|
|
231
230
|
id: 'a',
|
|
232
231
|
data: {
|
|
233
232
|
options: [{
|
|
234
|
-
nested: {
|
|
233
|
+
nested: [{
|
|
235
234
|
type: 'text',
|
|
236
235
|
fieldId: NESTED_ID,
|
|
237
236
|
id: NESTED_ID,
|
|
238
|
-
required: true
|
|
239
|
-
|
|
240
|
-
}
|
|
237
|
+
required: true
|
|
238
|
+
}]
|
|
241
239
|
}]
|
|
242
240
|
}
|
|
243
241
|
};
|
|
244
|
-
expect((0, _validateComponent.default)(COMPONENT, FORMDATA)).toEqual({
|
|
242
|
+
expect((0, _validateComponent.default)(COMPONENT, undefined, FORMDATA)).toEqual([{
|
|
245
243
|
id: NESTED_ID,
|
|
246
244
|
error: "Field is required"
|
|
247
|
-
});
|
|
245
|
+
}]);
|
|
248
246
|
});
|
|
249
247
|
it('should return no error when a non selected radio component contains nested components with errors', function () {
|
|
250
248
|
var NESTED_ID = 'nestedId';
|
|
@@ -253,16 +251,17 @@ describe('utils.Validate.Component', function () {
|
|
|
253
251
|
id: 'a',
|
|
254
252
|
data: {
|
|
255
253
|
options: [{
|
|
256
|
-
|
|
254
|
+
value: 'optionValue',
|
|
255
|
+
nested: [{
|
|
257
256
|
type: 'text',
|
|
258
257
|
fieldId: NESTED_ID,
|
|
259
258
|
id: NESTED_ID,
|
|
260
259
|
required: true
|
|
261
|
-
}
|
|
260
|
+
}]
|
|
262
261
|
}]
|
|
263
262
|
}
|
|
264
263
|
};
|
|
265
|
-
expect((0, _validateComponent.default)(COMPONENT, {})).
|
|
264
|
+
expect((0, _validateComponent.default)(COMPONENT, undefined, {})).toEqual([]);
|
|
266
265
|
});
|
|
267
266
|
});
|
|
268
267
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ukhomeoffice/cop-react-form-renderer",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.2",
|
|
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.7.
|
|
19
|
+
"@ukhomeoffice/cop-react-components": "1.7.4",
|
|
20
20
|
"axios": "^0.21.1",
|
|
21
21
|
"dayjs": "^1.11.0",
|
|
22
22
|
"govuk-frontend": "^3.13.0",
|