@ukhomeoffice/cop-react-form-renderer 4.89.2 → 4.91.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.
@@ -132,9 +132,11 @@ var Collection = function Collection(_ref) {
132
132
  index: labelCount
133
133
  }));
134
134
  var removeStyle = config.disableAddAndRemove || index === 0 && config.minimumEntries ? '' : (_config$removeLocatio = config.removeLocation) !== null && _config$removeLocatio !== void 0 ? _config$removeLocatio : 'start';
135
+ var addStyle = index !== 0 && config.subsequentItemStyle ? config.subsequentItemStyle : {};
135
136
  return /*#__PURE__*/_react.default.createElement("div", {
136
137
  className: "".concat(classes('item')),
137
- key: item.id
138
+ key: item.id,
139
+ style: addStyle
138
140
  }, /*#__PURE__*/_react.default.createElement(_copReactComponents.Label, {
139
141
  id: item.id,
140
142
  required: true,
@@ -211,6 +213,7 @@ Collection.propTypes = {
211
213
  minimumEntries: _propTypes.default.number,
212
214
  readonly: _propTypes.default.bool,
213
215
  removeLocation: _propTypes.default.string,
216
+ subsequentItemStyle: _propTypes.default.shape({}),
214
217
  required: _propTypes.default.bool
215
218
  }).isRequired,
216
219
  formData: _propTypes.default.shape({}),
@@ -728,4 +728,80 @@ describe('components.FormComponent.Collection', function () {
728
728
  }
729
729
  }, _callee12);
730
730
  })));
731
+ it('should handle the alternative styling with a background and padding on the subsequent item(s) when subsequentItemStyle is provided', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee14() {
732
+ var COLLECTION, container, c, addButton, item, label, secondItem, secondLabel;
733
+ return _regeneratorRuntime().wrap(function _callee14$(_context14) {
734
+ while (1) switch (_context14.prev = _context14.next) {
735
+ case 0:
736
+ COLLECTION = {
737
+ id: ID,
738
+ fieldId: ID,
739
+ type: _models.ComponentTypes.COLLECTION,
740
+ item: [TEXT_COMPONENT],
741
+ subsequentItemStyle: {
742
+ background: 'white',
743
+ padding: '1px'
744
+ }
745
+ };
746
+ container = document.createElement('div');
747
+ document.body.appendChild(container);
748
+ _context14.next = 5;
749
+ return (0, _testUtils.act)( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13() {
750
+ return _regeneratorRuntime().wrap(function _callee13$(_context13) {
751
+ while (1) switch (_context13.prev = _context13.next) {
752
+ case 0:
753
+ (0, _setupTests.renderDomWithValidation)( /*#__PURE__*/_react2.default.createElement(_FormComponent.default, {
754
+ component: COLLECTION
755
+ }), container);
756
+ case 1:
757
+ case "end":
758
+ return _context13.stop();
759
+ }
760
+ }, _callee13);
761
+ })));
762
+ case 5:
763
+ // Check the container itself.
764
+ c = container.childNodes[0];
765
+ expect(c.tagName).toEqual('DIV');
766
+ expect(c.classList).toContain(_Collection.DEFAULT_CLASS);
767
+
768
+ // And now make sure it has no children OTHER than the button to add an item.
769
+ expect(c.childNodes.length).toEqual(1);
770
+
771
+ // Get hold of that "Add another" button and click it.
772
+ addButton = c.childNodes[0].childNodes[0];
773
+ _react.fireEvent.click(addButton, {});
774
+
775
+ // Make sure an item has been added.
776
+ expect(c.childNodes.length).toEqual(2);
777
+ item = c.childNodes[0];
778
+ label = item.childNodes[0];
779
+ expect(label.textContent).toContain(_utils.default.interpolateString(_models.CollectionLabels.item, {
780
+ index: 1
781
+ }));
782
+
783
+ // Check to see if styling has been untouched for the first item
784
+ expect(item.style.background).toEqual('');
785
+ expect(item.style.padding).toEqual('');
786
+
787
+ // Get hold of that "Add another" button and click it.
788
+ _react.fireEvent.click(addButton, {});
789
+
790
+ // Make sure another item has been added.
791
+ expect(c.childNodes.length).toEqual(3);
792
+ secondItem = c.childNodes[1];
793
+ secondLabel = secondItem.childNodes[0];
794
+ expect(secondLabel.textContent).toContain(_utils.default.interpolateString(_models.CollectionLabels.secondItem, {
795
+ index: 2
796
+ }));
797
+
798
+ // Get hold of the second item and check background and padding has been applied.
799
+ expect(secondItem.style.background).toEqual('white');
800
+ expect(secondItem.style.padding).toEqual('1px');
801
+ case 24:
802
+ case "end":
803
+ return _context14.stop();
804
+ }
805
+ }, _callee14);
806
+ })));
731
807
  });
@@ -32,13 +32,26 @@ var setupDefaultValue = function setupDefaultValue(component, data, fullPath) {
32
32
  setupDefaultValue(comp, data, "".concat(componentFullPath, "."));
33
33
  });
34
34
  }
35
- if (component.defaultValue && !(0, _getSourceData.default)(data, componentFullPath)) {
35
+ if ((component.defaultValue || component.defaultObjectValue) && !(0, _getSourceData.default)(data, componentFullPath)) {
36
36
  switch (component.type) {
37
37
  case 'date':
38
38
  setDefaultDateValue(component, data, fullPath);
39
39
  break;
40
40
  default:
41
- (0, _setDataItem.default)(data, componentFullPath, component.defaultValue);
41
+ // defaultValue is expected to be a string, default object value is an object. Keeping defaultValue
42
+ // as a string is required for consuming apps using FormIO, this restriction could be removed in the future.
43
+ {
44
+ if (component.defaultObjectValue) {
45
+ Object.keys(component.defaultObjectValue).forEach(function (value) {
46
+ if (typeof component.defaultObjectValue[value] === 'string' && (0, _getSourceData.default)(data, component.defaultObjectValue[value]) !== undefined) {
47
+ thisComponent.defaultObjectValue[value] = (0, _getSourceData.default)(data, component.defaultObjectValue[value]);
48
+ }
49
+ });
50
+ }
51
+ var defaultToUse = component.defaultObjectValue ? component.defaultObjectValue : component.defaultValue;
52
+ (0, _setDataItem.default)(data, componentFullPath, defaultToUse);
53
+ delete thisComponent.defaultObjectValue;
54
+ }
42
55
  }
43
56
  }
44
57
  if (component.defaultValue) {
@@ -221,6 +221,75 @@ describe('utils', function () {
221
221
  testField: 'VALUE'
222
222
  });
223
223
  });
224
+ it('should handle a default value field with defaultObjectValue', function () {
225
+ var PAGES = [];
226
+ var COMPONENTS = [{
227
+ fieldId: 'testField',
228
+ type: 'autocomplete',
229
+ defaultObjectValue: {
230
+ id: 4,
231
+ name: 'Algeria',
232
+ value: '4',
233
+ label: 'Algeria'
234
+ }
235
+ }];
236
+ var DATA = {};
237
+ var RESULT = (0, _setupFormData.default)(PAGES, COMPONENTS, DATA);
238
+ expect(RESULT.testField).toMatchObject({
239
+ id: 4,
240
+ name: 'Algeria',
241
+ value: '4',
242
+ label: 'Algeria'
243
+ });
244
+ });
245
+ it('should handle a default value field with defaultObjectValue if dot-separated field identifier exists within the data', function () {
246
+ var PAGES = [];
247
+ var COMPONENTS = [{
248
+ fieldId: 'testField',
249
+ type: 'autocomplete',
250
+ defaultObjectValue: {
251
+ id: 'country.id',
252
+ name: 'country.name',
253
+ value: 'country.value',
254
+ label: 'country.name'
255
+ }
256
+ }];
257
+ var DATA = {
258
+ 'country': {
259
+ 'id': 4,
260
+ 'name': 'Algeria',
261
+ 'value': '4'
262
+ }
263
+ };
264
+ var RESULT = (0, _setupFormData.default)(PAGES, COMPONENTS, DATA);
265
+ expect(RESULT.testField).toMatchObject({
266
+ id: 4,
267
+ name: 'Algeria',
268
+ value: '4',
269
+ label: 'Algeria'
270
+ });
271
+ });
272
+ it('should ignore the default value field with defaultObjectValue if dot-separated field identifier doesn\'t exists within the data', function () {
273
+ var PAGES = [];
274
+ var COMPONENTS = [{
275
+ fieldId: 'testField',
276
+ type: 'autocomplete',
277
+ defaultObjectValue: {
278
+ id: 'country.id',
279
+ name: 'country.name',
280
+ value: 'country.value',
281
+ label: 'country.name'
282
+ }
283
+ }];
284
+ var DATA = {};
285
+ var RESULT = (0, _setupFormData.default)(PAGES, COMPONENTS, DATA);
286
+ expect(RESULT.testField).toMatchObject({
287
+ id: 'country.id',
288
+ name: 'country.name',
289
+ value: 'country.value',
290
+ label: 'country.name'
291
+ });
292
+ });
224
293
  it('should ignore a default value when a value already exists in data', function () {
225
294
  var PAGES = [];
226
295
  var COMPONENTS = [{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.89.2",
3
+ "version": "4.91.0",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",