@ukhomeoffice/cop-react-form-renderer 4.90.0 → 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.
@@ -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.90.0",
3
+ "version": "4.91.0",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",