@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
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
6
|
+
exports.getChildrenJsx = exports.default = void 0;
|
|
7
7
|
|
|
8
8
|
var _copReactComponents = require("@ukhomeoffice/cop-react-components");
|
|
9
9
|
|
|
@@ -17,6 +17,8 @@ var _cleanAttributes = _interopRequireDefault(require("./cleanAttributes"));
|
|
|
17
17
|
|
|
18
18
|
var _isEditable = _interopRequireDefault(require("./isEditable"));
|
|
19
19
|
|
|
20
|
+
var _FormComponent = _interopRequireDefault(require("../../components/FormComponent"));
|
|
21
|
+
|
|
20
22
|
var _wrapInFormGroup = _interopRequireDefault(require("./wrapInFormGroup"));
|
|
21
23
|
|
|
22
24
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -85,11 +87,11 @@ var getRadios = function getRadios(config) {
|
|
|
85
87
|
});
|
|
86
88
|
|
|
87
89
|
options.forEach(function (option) {
|
|
88
|
-
if (!option.nested) {
|
|
90
|
+
if (!Array.isArray(option.nested)) {
|
|
89
91
|
return;
|
|
90
92
|
}
|
|
91
93
|
|
|
92
|
-
option.
|
|
94
|
+
option.children = getChildrenJsx(config, option.nested);
|
|
93
95
|
});
|
|
94
96
|
var attrs = (0, _cleanAttributes.default)(config);
|
|
95
97
|
return /*#__PURE__*/_react.default.createElement(_copReactComponents.Radios, _extends({}, attrs, {
|
|
@@ -156,32 +158,49 @@ var getComponentByType = function getComponentByType(config) {
|
|
|
156
158
|
}
|
|
157
159
|
};
|
|
158
160
|
/**
|
|
159
|
-
* Get
|
|
160
|
-
* @param {*}
|
|
161
|
-
* @param {*}
|
|
161
|
+
* Get single child component for a parent
|
|
162
|
+
* @param {*} parent the parent configuration
|
|
163
|
+
* @param {*} child the child configuration
|
|
162
164
|
*/
|
|
163
165
|
|
|
164
166
|
|
|
165
|
-
var
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if (parentConfig.formData) {
|
|
169
|
-
var _parentConfig$formDat;
|
|
167
|
+
var getChildJsx = function getChildJsx(parent, child) {
|
|
168
|
+
if (parent.formData) {
|
|
169
|
+
var _parent$formData;
|
|
170
170
|
|
|
171
|
-
|
|
171
|
+
child.value = parent !== null && parent !== void 0 && (_parent$formData = parent.formData) !== null && _parent$formData !== void 0 && _parent$formData[child.fieldId] ? parent.formData[child.fieldId] : '';
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
if ('readonly' in
|
|
174
|
+
if ('readonly' in child) delete child.readonly;
|
|
175
175
|
|
|
176
|
-
if (
|
|
177
|
-
|
|
178
|
-
return getComponent(
|
|
176
|
+
if (parent.readonly) {
|
|
177
|
+
child.readonly = parent.readonly;
|
|
178
|
+
return /*#__PURE__*/_react.default.createElement("div", null, getComponent(child, false));
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
return
|
|
181
|
+
return /*#__PURE__*/_react.default.createElement(_FormComponent.default, {
|
|
182
|
+
component: child,
|
|
183
|
+
value: child.value,
|
|
184
|
+
onChange: parent.onChange,
|
|
185
|
+
key: child.key
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* Convert chidlrenConfigs into components
|
|
190
|
+
* @param {*} parentConfig parent component which the childrenConfigs will be under
|
|
191
|
+
* @param {*} childrenConfigs array of configurations for the child components
|
|
192
|
+
*/
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
var getChildrenJsx = function getChildrenJsx(parentConfig, childrenConfigs) {
|
|
196
|
+
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, childrenConfigs.map(function (config) {
|
|
197
|
+
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, {
|
|
198
|
+
key: config.id
|
|
199
|
+
}, getChildJsx(parentConfig, config));
|
|
200
|
+
}));
|
|
182
201
|
};
|
|
183
202
|
|
|
184
|
-
exports.
|
|
203
|
+
exports.getChildrenJsx = getChildrenJsx;
|
|
185
204
|
|
|
186
205
|
/**
|
|
187
206
|
* Get a renderable component, based on a configuration object.
|
|
@@ -205,8 +224,7 @@ var getComponent = function getComponent(config) {
|
|
|
205
224
|
var component = getComponentByType(config);
|
|
206
225
|
|
|
207
226
|
if (component && wrap && (0, _isEditable.default)(config)) {
|
|
208
|
-
|
|
209
|
-
return (0, _wrapInFormGroup.default)(attrs, component);
|
|
227
|
+
return (0, _wrapInFormGroup.default)(config, component);
|
|
210
228
|
}
|
|
211
229
|
|
|
212
230
|
return component;
|
|
@@ -4,6 +4,8 @@ var _react = require("@testing-library/react");
|
|
|
4
4
|
|
|
5
5
|
var _getComponent = require("../getComponent");
|
|
6
6
|
|
|
7
|
+
var _setupTests = require("../../../setupTests");
|
|
8
|
+
|
|
7
9
|
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
10
|
|
|
9
11
|
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); } }
|
|
@@ -11,8 +13,10 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try
|
|
|
11
13
|
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); }); }; }
|
|
12
14
|
|
|
13
15
|
describe('utils.Component.get', function () {
|
|
14
|
-
it('should return
|
|
15
|
-
var
|
|
16
|
+
it('should return components that are nested within another component', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
|
|
17
|
+
var _formData;
|
|
18
|
+
|
|
19
|
+
var ID, FIELD_ID, LABEL, ID2, FIELD_ID2, LABEL2, VALUE, VALUE2, PARENT_CONFIG, NESTED_CONFIG, _renderWithValidation, container, child, label, input, child2, label2, input2;
|
|
16
20
|
|
|
17
21
|
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
18
22
|
while (1) {
|
|
@@ -21,32 +25,53 @@ describe('utils.Component.get', function () {
|
|
|
21
25
|
ID = 'testId';
|
|
22
26
|
FIELD_ID = 'fieldId';
|
|
23
27
|
LABEL = 'Test label';
|
|
28
|
+
ID2 = 'testId2';
|
|
29
|
+
FIELD_ID2 = 'fieldId2';
|
|
30
|
+
LABEL2 = 'Test label 2';
|
|
24
31
|
VALUE = 'nestedValue';
|
|
32
|
+
VALUE2 = '1-2-2022';
|
|
25
33
|
PARENT_CONFIG = {
|
|
26
34
|
onChange: function onChange() {},
|
|
27
|
-
formData:
|
|
28
|
-
};
|
|
29
|
-
NESTED_CONFIG = {
|
|
30
|
-
nested: {
|
|
31
|
-
id: ID,
|
|
32
|
-
fieldId: FIELD_ID,
|
|
33
|
-
label: LABEL,
|
|
34
|
-
type: 'text'
|
|
35
|
-
}
|
|
35
|
+
formData: (_formData = {}, _defineProperty(_formData, FIELD_ID, VALUE), _defineProperty(_formData, FIELD_ID2, VALUE2), _formData)
|
|
36
36
|
};
|
|
37
|
-
|
|
37
|
+
NESTED_CONFIG = [{
|
|
38
|
+
id: ID,
|
|
39
|
+
fieldId: FIELD_ID,
|
|
40
|
+
label: LABEL,
|
|
41
|
+
type: 'text'
|
|
42
|
+
}, {
|
|
43
|
+
id: ID2,
|
|
44
|
+
fieldId: FIELD_ID2,
|
|
45
|
+
label: LABEL2,
|
|
46
|
+
type: 'date'
|
|
47
|
+
}];
|
|
48
|
+
_renderWithValidation = (0, _setupTests.renderWithValidation)((0, _getComponent.getChildrenJsx)(PARENT_CONFIG, NESTED_CONFIG)), container = _renderWithValidation.container;
|
|
38
49
|
child = container.childNodes[0];
|
|
50
|
+
expect(child.childNodes.length).toEqual(3);
|
|
39
51
|
expect(child.classList).toContain('govuk-form-group');
|
|
40
52
|
label = child.childNodes[0];
|
|
41
53
|
expect(label).toBeDefined();
|
|
42
54
|
expect(label.innerHTML).toContain(LABEL);
|
|
43
|
-
input = child.childNodes[
|
|
55
|
+
input = child.childNodes[2];
|
|
44
56
|
expect(input.tagName).toEqual('INPUT');
|
|
45
57
|
expect(input.classList).toContain('govuk-input');
|
|
46
58
|
expect(input.id).toEqual(ID);
|
|
47
59
|
expect(input.value).toEqual(VALUE);
|
|
60
|
+
child2 = container.childNodes[1];
|
|
61
|
+
expect(child2.childNodes.length).toEqual(3);
|
|
62
|
+
expect(child2.classList).toContain('govuk-form-group');
|
|
63
|
+
label2 = child2.childNodes[0];
|
|
64
|
+
expect(label2).toBeDefined();
|
|
65
|
+
expect(label2.innerHTML).toContain(LABEL2);
|
|
66
|
+
input2 = child2.childNodes[2];
|
|
67
|
+
expect(input2.tagName).toEqual('DIV');
|
|
68
|
+
expect(input2.classList).toContain('govuk-date-input');
|
|
69
|
+
expect(input2.id).toEqual(ID2);
|
|
70
|
+
expect(input2.childNodes[0].childNodes[1].value).toEqual('1');
|
|
71
|
+
expect(input2.childNodes[1].childNodes[1].value).toEqual('2');
|
|
72
|
+
expect(input2.childNodes[2].childNodes[1].value).toEqual('2022');
|
|
48
73
|
|
|
49
|
-
case
|
|
74
|
+
case 35:
|
|
50
75
|
case "end":
|
|
51
76
|
return _context.stop();
|
|
52
77
|
}
|
|
@@ -54,7 +79,7 @@ describe('utils.Component.get', function () {
|
|
|
54
79
|
}, _callee);
|
|
55
80
|
})));
|
|
56
81
|
it('should return a read only component that is nested within another readonly component', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
|
|
57
|
-
var ID, FIELD_ID, LABEL, VALUE, PARENT_CONFIG, NESTED_CONFIG,
|
|
82
|
+
var ID, FIELD_ID, LABEL, VALUE, PARENT_CONFIG, NESTED_CONFIG, _render, container, child;
|
|
58
83
|
|
|
59
84
|
return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
60
85
|
while (1) {
|
|
@@ -69,17 +94,15 @@ describe('utils.Component.get', function () {
|
|
|
69
94
|
formData: _defineProperty({}, FIELD_ID, VALUE),
|
|
70
95
|
readonly: true
|
|
71
96
|
};
|
|
72
|
-
NESTED_CONFIG = {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
_render2 = (0, _react.render)((0, _getComponent.getNestedComponent)(PARENT_CONFIG, NESTED_CONFIG)), container = _render2.container;
|
|
82
|
-
child = container.childNodes[0];
|
|
97
|
+
NESTED_CONFIG = [{
|
|
98
|
+
id: ID,
|
|
99
|
+
fieldId: FIELD_ID,
|
|
100
|
+
label: LABEL,
|
|
101
|
+
type: 'text',
|
|
102
|
+
readonly: true
|
|
103
|
+
}];
|
|
104
|
+
_render = (0, _react.render)((0, _getComponent.getChildrenJsx)(PARENT_CONFIG, NESTED_CONFIG)), container = _render.container;
|
|
105
|
+
child = container.childNodes[0].childNodes[0];
|
|
83
106
|
expect(child.classList).toContain('hods-readonly');
|
|
84
107
|
|
|
85
108
|
case 9:
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _react = require("@testing-library/react");
|
|
4
|
+
|
|
5
|
+
var _models = require("../../../models");
|
|
6
|
+
|
|
7
|
+
var _getComponent = _interopRequireDefault(require("../getComponent"));
|
|
8
|
+
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
|
|
11
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
12
|
+
|
|
13
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
14
|
+
|
|
15
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
16
|
+
|
|
17
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
18
|
+
|
|
19
|
+
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
20
|
+
|
|
21
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
22
|
+
|
|
23
|
+
describe('utils.Component.get', function () {
|
|
24
|
+
it('should return an appropriately rendered time component', function () {
|
|
25
|
+
var ID = 'test-id';
|
|
26
|
+
var FIELD_ID = 'field-id';
|
|
27
|
+
var LABEL = 'label';
|
|
28
|
+
var ON_CHANGE_CALLS = [];
|
|
29
|
+
|
|
30
|
+
var ON_CHANGE = function ON_CHANGE(e) {
|
|
31
|
+
ON_CHANGE_CALLS.push(e.target);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
var COMPONENT = {
|
|
35
|
+
type: _models.ComponentTypes.TIME,
|
|
36
|
+
id: ID,
|
|
37
|
+
fieldId: FIELD_ID,
|
|
38
|
+
label: LABEL,
|
|
39
|
+
onChange: ON_CHANGE,
|
|
40
|
+
'data-testid': ID
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
var _render = (0, _react.render)((0, _getComponent.default)(COMPONENT)),
|
|
44
|
+
container = _render.container;
|
|
45
|
+
|
|
46
|
+
var _getAllByTestId = (0, _react.getAllByTestId)(container, ID),
|
|
47
|
+
_getAllByTestId2 = _slicedToArray(_getAllByTestId, 2),
|
|
48
|
+
formGroup = _getAllByTestId2[0],
|
|
49
|
+
timeInput = _getAllByTestId2[1];
|
|
50
|
+
|
|
51
|
+
expect(formGroup.tagName).toEqual('DIV');
|
|
52
|
+
expect(formGroup.classList).toContain('govuk-form-group');
|
|
53
|
+
var label = formGroup.childNodes[0];
|
|
54
|
+
expect(label.innerHTML).toContain(LABEL);
|
|
55
|
+
expect(label.getAttribute('for')).toEqual(ID);
|
|
56
|
+
expect(timeInput.tagName).toEqual('DIV');
|
|
57
|
+
expect(timeInput.classList).toContain('govuk-date-input');
|
|
58
|
+
expect(timeInput.id).toEqual(ID);
|
|
59
|
+
var onChangeCalls = ON_CHANGE_CALLS.length;
|
|
60
|
+
|
|
61
|
+
var _timeInput$childNodes = _slicedToArray(timeInput.childNodes, 2),
|
|
62
|
+
hourItem = _timeInput$childNodes[0],
|
|
63
|
+
minuteItem = _timeInput$childNodes[1];
|
|
64
|
+
|
|
65
|
+
[{
|
|
66
|
+
id: 'hour',
|
|
67
|
+
label: 'Hour',
|
|
68
|
+
item: hourItem,
|
|
69
|
+
value: '5',
|
|
70
|
+
expectedValue: '5:'
|
|
71
|
+
}, {
|
|
72
|
+
id: 'minute',
|
|
73
|
+
label: 'Minute',
|
|
74
|
+
item: minuteItem,
|
|
75
|
+
value: '11',
|
|
76
|
+
expectedValue: '5:11'
|
|
77
|
+
}].forEach(function (part) {
|
|
78
|
+
expect(part.item.tagName).toEqual('DIV');
|
|
79
|
+
expect(part.item.classList).toContain('govuk-date-input__item');
|
|
80
|
+
|
|
81
|
+
var _part$item$childNodes = _slicedToArray(part.item.childNodes, 2),
|
|
82
|
+
label = _part$item$childNodes[0],
|
|
83
|
+
input = _part$item$childNodes[1];
|
|
84
|
+
|
|
85
|
+
expect(label.tagName).toEqual('LABEL');
|
|
86
|
+
expect(label.classList).toContain('govuk-label');
|
|
87
|
+
expect(label.textContent).toEqual(part.label);
|
|
88
|
+
expect(input.tagName).toEqual('INPUT');
|
|
89
|
+
expect(input.id).toEqual("".concat(ID, "-").concat(part.id)); // Put something in the input and make sure it fires.
|
|
90
|
+
|
|
91
|
+
_react.fireEvent.change(input, {
|
|
92
|
+
target: {
|
|
93
|
+
name: "".concat(FIELD_ID, "-").concat(part.id),
|
|
94
|
+
value: part.value
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
onChangeCalls++;
|
|
99
|
+
expect(ON_CHANGE_CALLS.length).toEqual(onChangeCalls);
|
|
100
|
+
expect(ON_CHANGE_CALLS[onChangeCalls - 1]).toMatchObject({
|
|
101
|
+
name: FIELD_ID,
|
|
102
|
+
value: part.expectedValue
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
});
|
|
@@ -11,6 +11,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
11
11
|
|
|
12
12
|
// Local imports
|
|
13
13
|
var showComponent = function showComponent(component, data) {
|
|
14
|
+
var _component$show_when;
|
|
15
|
+
|
|
14
16
|
if (!component) {
|
|
15
17
|
return false;
|
|
16
18
|
}
|
|
@@ -19,6 +21,10 @@ var showComponent = function showComponent(component, data) {
|
|
|
19
21
|
return false;
|
|
20
22
|
}
|
|
21
23
|
|
|
24
|
+
if (((_component$show_when = component.show_when) === null || _component$show_when === void 0 ? void 0 : _component$show_when.type) === "or") {
|
|
25
|
+
return _Condition.default.meetsOne(component, data);
|
|
26
|
+
}
|
|
27
|
+
|
|
22
28
|
return _Condition.default.meetsAll(component, data);
|
|
23
29
|
};
|
|
24
30
|
|
|
@@ -89,6 +89,69 @@ describe('utils', function () {
|
|
|
89
89
|
};
|
|
90
90
|
expect((0, _showComponent.default)(COMPONENT, DATA)).toBeFalsy();
|
|
91
91
|
});
|
|
92
|
+
it('SHOULD be shown when the component has multiple show_when conditions, with type "or" provided and ALL are matched', function () {
|
|
93
|
+
var COMPONENT = {
|
|
94
|
+
show_when: {
|
|
95
|
+
"type": "or",
|
|
96
|
+
"conditions": [{
|
|
97
|
+
field: 'alpha',
|
|
98
|
+
op: '=',
|
|
99
|
+
value: 'Alpha'
|
|
100
|
+
}, {
|
|
101
|
+
field: 'bravo',
|
|
102
|
+
op: '=',
|
|
103
|
+
value: 'Bravo'
|
|
104
|
+
}]
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
var DATA = {
|
|
108
|
+
alpha: 'Alpha',
|
|
109
|
+
bravo: 'Bravo'
|
|
110
|
+
};
|
|
111
|
+
expect((0, _showComponent.default)(COMPONENT, DATA)).toBeTruthy();
|
|
112
|
+
});
|
|
113
|
+
it('SHOULD be shown when the component has multiple show_when conditions, with type "or" provided and at least ONE is matched', function () {
|
|
114
|
+
var COMPONENT = {
|
|
115
|
+
show_when: {
|
|
116
|
+
"type": "or",
|
|
117
|
+
"conditions": [{
|
|
118
|
+
field: 'alpha',
|
|
119
|
+
op: '=',
|
|
120
|
+
value: 'Alpha'
|
|
121
|
+
}, {
|
|
122
|
+
field: 'charlie',
|
|
123
|
+
op: '=',
|
|
124
|
+
value: 'Charlie'
|
|
125
|
+
}]
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
var DATA = {
|
|
129
|
+
alpha: 'Alpha',
|
|
130
|
+
bravo: 'Bravo'
|
|
131
|
+
};
|
|
132
|
+
expect((0, _showComponent.default)(COMPONENT, DATA)).toBeTruthy();
|
|
133
|
+
});
|
|
134
|
+
it('should NOT be shown when the component has multiple show_when conditions, with type "or" provided and NONE are matched', function () {
|
|
135
|
+
var COMPONENT = {
|
|
136
|
+
show_when: {
|
|
137
|
+
"type": "or",
|
|
138
|
+
"conditions": [{
|
|
139
|
+
field: 'alpha',
|
|
140
|
+
op: '!=',
|
|
141
|
+
value: 'Alpha'
|
|
142
|
+
}, {
|
|
143
|
+
field: 'charlie',
|
|
144
|
+
op: '=',
|
|
145
|
+
value: 'Charlie'
|
|
146
|
+
}]
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
var DATA = {
|
|
150
|
+
alpha: 'Alpha',
|
|
151
|
+
bravo: 'Bravo'
|
|
152
|
+
};
|
|
153
|
+
expect((0, _showComponent.default)(COMPONENT, DATA)).toBeFalsy();
|
|
154
|
+
});
|
|
92
155
|
});
|
|
93
156
|
});
|
|
94
157
|
});
|
|
@@ -5,16 +5,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
+
var _copReactComponents = require("@ukhomeoffice/cop-react-components");
|
|
9
|
+
|
|
8
10
|
var _react = _interopRequireDefault(require("react"));
|
|
9
11
|
|
|
10
|
-
var
|
|
12
|
+
var _cleanAttributes = _interopRequireDefault(require("./cleanAttributes"));
|
|
11
13
|
|
|
12
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
15
|
|
|
14
16
|
function _extends() { _extends = Object.assign || 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); }
|
|
15
17
|
|
|
16
18
|
var wrapInFormGroup = function wrapInFormGroup(config, children) {
|
|
17
|
-
|
|
19
|
+
var attrs = (0, _cleanAttributes.default)(config, ['fieldId', 'displayMenu']);
|
|
20
|
+
return /*#__PURE__*/_react.default.createElement(_copReactComponents.FormGroup, _extends({}, attrs, {
|
|
18
21
|
onChange: null
|
|
19
22
|
}), children);
|
|
20
23
|
};
|
|
@@ -9,9 +9,12 @@ var _meetsAllConditions = _interopRequireDefault(require("./meetsAllConditions")
|
|
|
9
9
|
|
|
10
10
|
var _meetsCondition = _interopRequireDefault(require("./meetsCondition"));
|
|
11
11
|
|
|
12
|
+
var _meetsOneCondition = _interopRequireDefault(require("./meetsOneCondition"));
|
|
13
|
+
|
|
12
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
15
|
|
|
14
16
|
var Condition = {
|
|
17
|
+
meetsOne: _meetsOneCondition.default,
|
|
15
18
|
meetsAll: _meetsAllConditions.default,
|
|
16
19
|
met: _meetsCondition.default
|
|
17
20
|
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _Data = _interopRequireDefault(require("../Data"));
|
|
9
|
+
|
|
10
|
+
var _meetsCondition = _interopRequireDefault(require("./meetsCondition"));
|
|
11
|
+
|
|
12
|
+
var _setupConditions = _interopRequireDefault(require("./setupConditions"));
|
|
13
|
+
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
|
|
16
|
+
// Local imports
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Evaluates all condition(s) on a page, container, or component.
|
|
20
|
+
* @param {object} options The container to consider.
|
|
21
|
+
* @param {object} data The top-level form data.
|
|
22
|
+
* @returns Boolean true if at least one conditions is met; false otherwise.
|
|
23
|
+
*/
|
|
24
|
+
var meetsOneCondition = function meetsOneCondition(options, data) {
|
|
25
|
+
var conditions = (0, _setupConditions.default)(options);
|
|
26
|
+
|
|
27
|
+
if (conditions) {
|
|
28
|
+
var arr = Array.isArray(conditions) ? conditions : [conditions];
|
|
29
|
+
return arr.some(function (condition) {
|
|
30
|
+
var sourceDataValue = _Data.default.getSource(data, condition.field);
|
|
31
|
+
|
|
32
|
+
return (0, _meetsCondition.default)(condition, sourceDataValue);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return true;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
var _default = meetsOneCondition;
|
|
40
|
+
exports.default = _default;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _meetsOneCondition = _interopRequireDefault(require("./meetsOneCondition"));
|
|
4
|
+
|
|
5
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
6
|
+
|
|
7
|
+
// Local imports
|
|
8
|
+
describe('utils.Condition.meetsOneCondition', function () {
|
|
9
|
+
var DATA = {
|
|
10
|
+
alpha: 'bravo',
|
|
11
|
+
charlie: 'delta'
|
|
12
|
+
};
|
|
13
|
+
it('should evaluate to true when conditions are null', function () {
|
|
14
|
+
expect((0, _meetsOneCondition.default)(null, DATA)).toBeTruthy();
|
|
15
|
+
});
|
|
16
|
+
it('should evaluate to true when conditions are undefined', function () {
|
|
17
|
+
expect((0, _meetsOneCondition.default)(undefined, DATA)).toBeTruthy();
|
|
18
|
+
});
|
|
19
|
+
it('should evaluate to true when conditions is an empty array', function () {
|
|
20
|
+
var CONDITION = {
|
|
21
|
+
"type": "or",
|
|
22
|
+
"conditions": []
|
|
23
|
+
};
|
|
24
|
+
expect((0, _meetsOneCondition.default)(CONDITION, DATA)).toBeTruthy();
|
|
25
|
+
});
|
|
26
|
+
it('should evaluate to true when one condition is provided and one condition is met', function () {
|
|
27
|
+
var CONDITION = {
|
|
28
|
+
"type": "or",
|
|
29
|
+
"conditions": [{
|
|
30
|
+
field: 'alpha',
|
|
31
|
+
op: 'eq',
|
|
32
|
+
value: 'bravo'
|
|
33
|
+
}]
|
|
34
|
+
};
|
|
35
|
+
expect((0, _meetsOneCondition.default)(CONDITION, DATA)).toBeTruthy();
|
|
36
|
+
});
|
|
37
|
+
it('should evaluate to true when two conditions are provided and one condition is met', function () {
|
|
38
|
+
var CONDITION = {
|
|
39
|
+
"type": "or",
|
|
40
|
+
"conditions": [{
|
|
41
|
+
field: 'alpha',
|
|
42
|
+
op: 'eq',
|
|
43
|
+
value: 'bravo'
|
|
44
|
+
}, {
|
|
45
|
+
field: 'alpha',
|
|
46
|
+
op: 'eq',
|
|
47
|
+
value: "charlie"
|
|
48
|
+
}]
|
|
49
|
+
};
|
|
50
|
+
expect((0, _meetsOneCondition.default)(CONDITION, DATA)).toBeTruthy();
|
|
51
|
+
});
|
|
52
|
+
it('should evaluate to true when three conditions are provided and two conditions are met', function () {
|
|
53
|
+
var CONDITION = {
|
|
54
|
+
"type": "or",
|
|
55
|
+
"conditions": [{
|
|
56
|
+
field: 'alpha',
|
|
57
|
+
op: 'eq',
|
|
58
|
+
value: 'bravo'
|
|
59
|
+
}, {
|
|
60
|
+
field: 'charlie',
|
|
61
|
+
op: 'eq',
|
|
62
|
+
value: 'delta'
|
|
63
|
+
}, {
|
|
64
|
+
field: 'alpha',
|
|
65
|
+
op: 'eq',
|
|
66
|
+
value: "charlie"
|
|
67
|
+
}]
|
|
68
|
+
};
|
|
69
|
+
expect((0, _meetsOneCondition.default)(CONDITION, DATA)).toBeTruthy();
|
|
70
|
+
});
|
|
71
|
+
it('should evaluate to true when all conditions are met', function () {
|
|
72
|
+
var CONDITION = {
|
|
73
|
+
"type": "or",
|
|
74
|
+
"conditions": [{
|
|
75
|
+
field: 'alpha',
|
|
76
|
+
op: 'eq',
|
|
77
|
+
value: 'bravo'
|
|
78
|
+
}, {
|
|
79
|
+
field: 'charlie',
|
|
80
|
+
op: 'eq',
|
|
81
|
+
value: 'delta'
|
|
82
|
+
}]
|
|
83
|
+
};
|
|
84
|
+
expect((0, _meetsOneCondition.default)(CONDITION, DATA)).toBeTruthy();
|
|
85
|
+
});
|
|
86
|
+
it('should evaluate to false when no conditions are met', function () {
|
|
87
|
+
var CONDITION = {
|
|
88
|
+
"type": "or",
|
|
89
|
+
"conditions": [{
|
|
90
|
+
field: 'alpha',
|
|
91
|
+
op: 'eq',
|
|
92
|
+
value: 'delta'
|
|
93
|
+
}, {
|
|
94
|
+
field: 'charlie',
|
|
95
|
+
op: 'eq',
|
|
96
|
+
value: 'bravo'
|
|
97
|
+
}]
|
|
98
|
+
};
|
|
99
|
+
expect((0, _meetsOneCondition.default)(CONDITION, DATA)).toBeTruthy();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -17,12 +17,13 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
|
|
|
17
17
|
|
|
18
18
|
var makeComponentFieldsAbsolute = function makeComponentFieldsAbsolute(component) {
|
|
19
19
|
if (component.show_when) {
|
|
20
|
+
var conditions = component.show_when.type ? component.show_when.conditions : component.show_when;
|
|
20
21
|
var full_path = component.full_path;
|
|
21
|
-
|
|
22
|
-
return
|
|
23
|
-
var field = _Data.default.getDataPath(
|
|
22
|
+
conditions = Array.isArray(conditions) ? conditions : [conditions];
|
|
23
|
+
return conditions.map(function (condition) {
|
|
24
|
+
var field = _Data.default.getDataPath(condition.field, full_path);
|
|
24
25
|
|
|
25
|
-
return _objectSpread(_objectSpread({},
|
|
26
|
+
return _objectSpread(_objectSpread({}, condition), {}, {
|
|
26
27
|
field: field
|
|
27
28
|
});
|
|
28
29
|
});
|
|
@@ -41,6 +41,10 @@ var showContainer = function showContainer(container, data) {
|
|
|
41
41
|
|
|
42
42
|
|
|
43
43
|
if (container.show_when) {
|
|
44
|
+
if (container.show_when.type === "or") {
|
|
45
|
+
return _Condition.default.meetsOne(container, data);
|
|
46
|
+
}
|
|
47
|
+
|
|
44
48
|
return _Condition.default.meetsAll(container, data);
|
|
45
49
|
} // If the container 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.
|