@ukhomeoffice/cop-react-form-renderer 5.93.0 → 5.94.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.
@@ -7,6 +7,7 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.getChildrenJsx = exports.default = void 0;
8
8
  var _copReactComponents = require("@ukhomeoffice/cop-react-components");
9
9
  var _react = _interopRequireDefault(require("react"));
10
+ var _Condition = _interopRequireDefault(require("../Condition"));
10
11
  var _models = require("../../models");
11
12
  var _Data = _interopRequireDefault(require("../Data"));
12
13
  var _cleanAttributes = _interopRequireDefault(require("./cleanAttributes"));
@@ -265,11 +266,14 @@ var getChildJsx = function getChildJsx(parent, paramChild) {
265
266
  * @param {*} childrenConfigs array of configurations for the child components
266
267
  */
267
268
  var getChildrenJsx = exports.getChildrenJsx = function getChildrenJsx(parentConfig, childrenConfigs) {
268
- return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, childrenConfigs.map(function (config) {
269
+ var validChildren = childrenConfigs.filter(function (config) {
270
+ return !(config.show_when && !_Condition.default.meetsAll(config.show_when, parentConfig.formData));
271
+ }).map(function (config) {
269
272
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, {
270
273
  key: config.id
271
274
  }, getChildJsx(parentConfig, config));
272
- }));
275
+ });
276
+ return validChildren.length > 0 ? validChildren : null;
273
277
  };
274
278
  /**
275
279
  * Get a renderable component, based on a configuration object.
@@ -180,4 +180,56 @@ describe('utils.Component.get', function () {
180
180
  }
181
181
  }, _callee4);
182
182
  })));
183
+ it('should correctly show nested component with show_when', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
184
+ var FIELD_ID, VALUE, VisibleComponent, HiddenComponent, PARENT_CONFIG, _renderWithValidation4, container, child, label, input;
185
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
186
+ while (1) switch (_context5.prev = _context5.next) {
187
+ case 0:
188
+ FIELD_ID = 'fieldId';
189
+ VALUE = 'nestedValue';
190
+ VisibleComponent = {
191
+ id: 'testId',
192
+ fieldId: FIELD_ID,
193
+ label: 'Test label',
194
+ type: 'text',
195
+ show_when: [{
196
+ field: FIELD_ID,
197
+ op: "=",
198
+ value: VALUE
199
+ }]
200
+ };
201
+ HiddenComponent = {
202
+ id: 'hiddenID',
203
+ fieldId: "hiddenFeild",
204
+ label: 'Hidden label',
205
+ type: 'text',
206
+ show_when: [{
207
+ field: FIELD_ID,
208
+ op: "!=",
209
+ value: VALUE
210
+ }]
211
+ };
212
+ PARENT_CONFIG = {
213
+ onChange: function onChange() {},
214
+ formData: _defineProperty({}, FIELD_ID, VALUE)
215
+ };
216
+ _renderWithValidation4 = (0, _setupTests.renderWithValidation)((0, _getComponent.getChildrenJsx)(PARENT_CONFIG, [VisibleComponent, HiddenComponent])), container = _renderWithValidation4.container;
217
+ expect(container.childNodes.length).toEqual(1);
218
+ child = container.childNodes[0];
219
+ expect(child.childNodes[0].childNodes.length).toEqual(2);
220
+ expect(child.classList).toContain('govuk-form-group');
221
+ label = child.childNodes[0];
222
+ expect(label).toBeDefined();
223
+ expect(label.innerHTML).toContain(VisibleComponent.label);
224
+ input = child.childNodes[2];
225
+ expect(input.tagName).toEqual('INPUT');
226
+ expect(input.classList).toContain('govuk-input');
227
+ expect(input.id).toEqual("".concat(VisibleComponent.id));
228
+ expect(input.value).toEqual(VALUE);
229
+ case 18:
230
+ case "end":
231
+ return _context5.stop();
232
+ }
233
+ }, _callee5);
234
+ })));
183
235
  });
@@ -26,6 +26,9 @@ var interpolateOptions = function interpolateOptions(config, options) {
26
26
  if (typeof opt === 'string') {
27
27
  return opt;
28
28
  }
29
+ if (!opt.value) {
30
+ return opt.label;
31
+ }
29
32
  if (config.alternativeHintField) {
30
33
  // eslint-disable-next-line no-param-reassign
31
34
  opt.hint = opt[config.alternativeHintField];
@@ -28,6 +28,16 @@ describe('utils', function () {
28
28
  expect(options).toEqual(CONFIG.options);
29
29
  });
30
30
  });
31
+ it('should handle stings in option and if no value is supplied treat it as a string', function () {
32
+ var CONFIG = {
33
+ options: ["or", {
34
+ label: "and"
35
+ }]
36
+ };
37
+ (0, _getOptions.default)(CONFIG, function (options) {
38
+ expect(options).toEqual(["or", "and"]);
39
+ });
40
+ });
31
41
  it('should get any specified options from the data property of the config', function () {
32
42
  var CONFIG = {
33
43
  data: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "5.93.0",
3
+ "version": "5.94.0",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",