@ukhomeoffice/cop-react-form-renderer 5.6.0 → 5.8.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.
@@ -95,7 +95,7 @@ var CollectionPage = function CollectionPage(_ref) {
95
95
  formData: _objectSpread(_objectSpread(_objectSpread({}, page.formData), data[activeIndex]), {}, _defineProperty({}, "".concat(page.collection.name, "ActiveIndex"), activeIndex)),
96
96
  components: page.components.map(function (component) {
97
97
  return _objectSpread(_objectSpread({}, component), {}, {
98
- full_path: "".concat(page.collection.name, "[").concat(activeIndex, "].").concat(component.full_path || component.fieldId)
98
+ pageCollection: _objectSpread({}, page.collection)
99
99
  });
100
100
  })
101
101
  }),
@@ -141,7 +141,7 @@ describe('components.CollectionPage', function () {
141
141
  expect(label.tagName).toEqual('LABEL');
142
142
  expect(label.classList).toContain('govuk-label');
143
143
  expect(label.textContent).toEqual(lbl);
144
- expect(label.getAttribute('for')).toEqual("".concat(COLLECTION_NAME, "[0].").concat(fieldId));
144
+ expect(label.getAttribute('for')).toEqual(fieldId);
145
145
  var hint = formGroup.childNodes[1];
146
146
  expect(hint.tagName).toEqual('DIV');
147
147
  expect(hint.classList).toContain('govuk-hint');
@@ -149,7 +149,7 @@ describe('components.CollectionPage', function () {
149
149
  var input = formGroup.childNodes[2];
150
150
  expect(input.tagName).toEqual('INPUT');
151
151
  expect(input.classList).toContain('govuk-input');
152
- expect(input.id).toEqual("".concat(COLLECTION_NAME, "[0].").concat(fieldId));
152
+ expect(input.id).toEqual(fieldId);
153
153
  expect(input.value).toEqual(val);
154
154
  return input;
155
155
  };
@@ -160,7 +160,7 @@ describe('components.CollectionPage', function () {
160
160
  expect(label.tagName).toEqual('LABEL');
161
161
  expect(label.classList).toContain('govuk-label');
162
162
  expect(label.textContent).toEqual(lbl);
163
- expect(label.getAttribute('for')).toEqual("".concat(COLLECTION_NAME, "[0].").concat(fieldId));
163
+ expect(label.getAttribute('for')).toEqual(fieldId);
164
164
  var hint = formGroup.childNodes[1];
165
165
  expect(hint.tagName).toEqual('DIV');
166
166
  expect(hint.classList).toContain('govuk-hint');
@@ -174,7 +174,7 @@ describe('components.CollectionPage', function () {
174
174
  })[0];
175
175
  expect(input.classList).toContain('hods-autocomplete__input');
176
176
  expect(input.tagName).toEqual('INPUT');
177
- expect(input.id).toEqual("".concat(COLLECTION_NAME, "[0].").concat(fieldId));
177
+ expect(input.id).toEqual(fieldId);
178
178
  expect(input.value).toEqual(val);
179
179
  return input;
180
180
  };
@@ -96,10 +96,16 @@ var FormComponent = function FormComponent(_ref) {
96
96
  }));
97
97
  }
98
98
  var changeMetaDocuments = function changeMetaDocuments(document) {
99
+ var componentPath = component.full_path || component.fieldId;
100
+ if (component.pageCollection) {
101
+ var activeIndex = _utils.default.CollectionPage.getActiveIndex(component.pageCollection.name, formData);
102
+ var pathPrefix = "".concat(component.pageCollection.name, "[").concat(activeIndex, "]");
103
+ componentPath = "".concat(pathPrefix, ".").concat(componentPath);
104
+ }
99
105
  onTopLevelChange({
100
106
  target: {
101
107
  name: _utils.default.Meta.name,
102
- value: _utils.default.Meta.documents.setForField(document, formData, component.full_path, component.allowMultiple)
108
+ value: _utils.default.Meta.documents.setForField(document, formData, componentPath, component.allowMultiple)
103
109
  }
104
110
  });
105
111
  };
@@ -153,7 +159,10 @@ FormComponent.propTypes = {
153
159
  id: _propTypes.default.string,
154
160
  label: _propTypes.default.string,
155
161
  suffix: _propTypes.default.string,
156
- type: _propTypes.default.string
162
+ type: _propTypes.default.string,
163
+ pageCollection: _propTypes.default.shape({
164
+ name: _propTypes.default.string
165
+ })
157
166
  }).isRequired,
158
167
  value: _propTypes.default.oneOfType([_propTypes.default.node,
159
168
  // not included in node
@@ -21,7 +21,12 @@ var getAutocompleteSource = function getAutocompleteSource(config) {
21
21
  var lcQuery = query ? query.toLowerCase() : '';
22
22
  populateResults(options.filter(function (opt) {
23
23
  var label = labelMaker ? labelMaker(opt) : opt.label || '';
24
- return label.toLowerCase().includes(lcQuery);
24
+ if (label.toLowerCase().includes(lcQuery)) {
25
+ return true;
26
+ }
27
+ return opt.synonyms ? opt.synonyms.some(function (synonym) {
28
+ return synonym.toLowerCase().includes(lcQuery);
29
+ }) : false;
25
30
  }));
26
31
  };
27
32
  };
@@ -141,6 +141,29 @@ describe('utils', function () {
141
141
  }]);
142
142
  });
143
143
  });
144
+ it('should match options using a defined synonym', function () {
145
+ var CONFIG = {
146
+ data: {
147
+ options: [{
148
+ value: 'a',
149
+ label: 'Alpha',
150
+ synonyms: ['first']
151
+ }, {
152
+ value: 'b'
153
+ }]
154
+ }
155
+ };
156
+ var SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
157
+ expect(typeof SOURCE === 'function').toBeTruthy();
158
+ SOURCE('first', function (results) {
159
+ expect(results.length).toEqual(1);
160
+ expect(results).toEqual([{
161
+ value: 'a',
162
+ label: 'Alpha',
163
+ synonyms: ['first']
164
+ }]);
165
+ });
166
+ });
144
167
  });
145
168
  describe('with custom item labels', function () {
146
169
  it('should get any specified options from the config', function () {
@@ -346,6 +369,42 @@ describe('utils', function () {
346
369
  }]);
347
370
  });
348
371
  });
372
+ it('should match options using a defined synonym', function () {
373
+ var CONFIG = {
374
+ item: {
375
+ value: 'currencyCode',
376
+ label: 'currencyCode',
377
+ // eslint-disable-next-line no-template-curly-in-string
378
+ format: '${currencyName} (${currencyCode})'
379
+ },
380
+ data: {
381
+ options: [{
382
+ currencyName: 'Great British Pounds',
383
+ currencyCode: 'GBP',
384
+ value: 'GBP',
385
+ label: 'GBP'
386
+ }, {
387
+ currencyName: 'United States Dollars',
388
+ currencyCode: 'USD',
389
+ value: 'USD',
390
+ label: 'USD',
391
+ synonyms: ['bucks']
392
+ }]
393
+ }
394
+ };
395
+ var SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
396
+ expect(typeof SOURCE === 'function').toBeTruthy();
397
+ SOURCE('bucks', function (results) {
398
+ expect(results.length).toEqual(1);
399
+ expect(results).toEqual([{
400
+ currencyName: 'United States Dollars',
401
+ currencyCode: 'USD',
402
+ value: 'USD',
403
+ label: 'USD',
404
+ synonyms: ['bucks']
405
+ }]);
406
+ });
407
+ });
349
408
  });
350
409
  });
351
410
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "5.6.0",
3
+ "version": "5.8.0",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",