@ukhomeoffice/cop-react-form-renderer 4.14.3 → 4.15.1-alpha

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.
@@ -131,7 +131,7 @@ var FormComponent = function FormComponent(_ref) {
131
131
  onTopLevelChange({
132
132
  target: {
133
133
  name: _utils.default.Meta.name,
134
- value: _utils.default.Meta.documents.setForField(document, formData, component.full_path)
134
+ value: _utils.default.Meta.documents.setForField(document, formData, component.full_path, component.allowMultiple)
135
135
  }
136
136
  });
137
137
  };
@@ -55,16 +55,22 @@ var getCheckboxes = function getCheckboxes(config) {
55
55
  };
56
56
 
57
57
  var getCalculation = function getCalculation(config) {
58
- var calculatedValue = _Data.default.applyFormula(config);
59
-
60
- if (!!calculatedValue) {
61
- config.value = calculatedValue;
62
- config.formData[config.fieldId] = "" + calculatedValue;
58
+ var value = _Data.default.applyFormula(config);
59
+
60
+ if (typeof config.onChange === 'function') {
61
+ if (value !== config.value) {
62
+ config.onChange({
63
+ target: {
64
+ name: config.fieldId,
65
+ value: value
66
+ }
67
+ });
68
+ }
63
69
  }
64
70
 
65
71
  var attrs = (0, _cleanAttributes.default)(config, ['formula']);
66
72
  return /*#__PURE__*/_react.default.createElement(_copReactComponents.TextInput, _extends({}, attrs, {
67
- readOnly: true
73
+ readonly: true
68
74
  }));
69
75
  };
70
76
 
@@ -26,6 +26,10 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
26
26
 
27
27
  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; }
28
28
 
29
+ 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); } }
30
+
31
+ 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); }); }; }
32
+
29
33
  describe('utils.Component.get', function () {
30
34
  var ID = 'test-id';
31
35
  var FIELD_ID = 'field-id';
@@ -134,28 +138,39 @@ describe('utils.Component.get', function () {
134
138
  },
135
139
  result: '266.67'
136
140
  }].forEach(function (test) {
137
- it("should render 'calculation' component with formula to given calculated value: ".concat(test.result), function () {
138
- var _render = (0, _react.render)((0, _getComponent.default)(_objectSpread(_objectSpread({}, COMPONENT), test.config))),
139
- container = _render.container;
141
+ test.config.onChange = function (_ref) {
142
+ var target = _ref.target;
143
+ COMPONENT.formData[target.name] = target.value;
144
+ };
140
145
 
141
- var _getAllByTestId = (0, _react.getAllByTestId)(container, ID),
142
- _getAllByTestId2 = _slicedToArray(_getAllByTestId, 2),
143
- formGroup = _getAllByTestId2[0],
144
- input = _getAllByTestId2[1];
146
+ it("should render 'calculation' component with formula to given calculated value: ".concat(test.result), /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
147
+ var _render, container, _getAllByTestId, _getAllByTestId2, formGroup, input, label;
145
148
 
146
- expect(formGroup.tagName).toEqual('DIV');
147
- expect(formGroup.classList).toContain('govuk-form-group');
148
- var label = formGroup.childNodes[0];
149
- expect(label.tagName).toEqual('LABEL');
150
- expect(label.classList).toContain('govuk-label');
151
- expect(label.textContent).toContain(LABEL);
152
- expect(formGroup.classList).toContain('govuk-form-group');
153
- expect(input.tagName).toEqual('INPUT');
154
- expect(input.classList).toContain('govuk-input');
155
- expect(input.id).toEqual(ID);
156
- expect(input.getAttribute('readonly')).not.toBeNull();
157
- expect(input.getAttribute('value')).toEqual(test.result);
158
- });
149
+ return regeneratorRuntime.wrap(function _callee$(_context) {
150
+ while (1) {
151
+ switch (_context.prev = _context.next) {
152
+ case 0:
153
+ _render = (0, _react.render)((0, _getComponent.default)(_objectSpread(_objectSpread({}, COMPONENT), test.config))), container = _render.container;
154
+ _getAllByTestId = (0, _react.getAllByTestId)(container, ID), _getAllByTestId2 = _slicedToArray(_getAllByTestId, 2), formGroup = _getAllByTestId2[0], input = _getAllByTestId2[1];
155
+ expect(formGroup.tagName).toEqual('DIV');
156
+ expect(formGroup.classList).toContain('govuk-form-group');
157
+ label = formGroup.childNodes[0];
158
+ expect(label.tagName).toEqual('LABEL');
159
+ expect(label.classList).toContain('govuk-label');
160
+ expect(label.textContent).toContain(LABEL);
161
+ expect(formGroup.classList).toContain('govuk-form-group');
162
+ expect(input.tagName).toEqual('DIV');
163
+ expect(input.classList).toContain('hods-readonly');
164
+ expect(input.id).toEqual(ID);
165
+ expect("".concat(COMPONENT.formData[FIELD_ID])).toEqual(test.result);
166
+
167
+ case 13:
168
+ case "end":
169
+ return _context.stop();
170
+ }
171
+ }
172
+ }, _callee);
173
+ })));
159
174
  });
160
175
  /**
161
176
  * Iteratively test collction of negative test data objects each containing:
@@ -258,10 +273,9 @@ describe('utils.Component.get', function () {
258
273
  expect(label.classList).toContain('govuk-label');
259
274
  expect(label.textContent).toContain(LABEL);
260
275
  expect(formGroup.classList).toContain('govuk-form-group');
261
- expect(input.tagName).toEqual('INPUT');
262
- expect(input.classList).toContain('govuk-input');
276
+ expect(input.tagName).toEqual('DIV');
277
+ expect(input.classList).toContain('hods-readonly');
263
278
  expect(input.id).toEqual(ID);
264
- expect(input.getAttribute('readonly')).not.toBeNull();
265
279
  expect(error).toBeCalledTimes(1);
266
280
  expect(error).toBeCalledWith(test.error);
267
281
  });
@@ -17,10 +17,14 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
17
17
 
18
18
  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; }
19
19
 
20
- var setDocumentForField = function setDocumentForField(document, formData, field) {
21
- var documents = (0, _getDocuments.default)(formData).filter(function (d) {
22
- return d.field !== field;
23
- });
20
+ var setDocumentForField = function setDocumentForField(document, formData, field, allowMultiple) {
21
+ var documents = (0, _getDocuments.default)(formData);
22
+
23
+ if (!allowMultiple) {
24
+ documents = documents.filter(function (d) {
25
+ return d.field !== field;
26
+ });
27
+ }
24
28
 
25
29
  if (document) {
26
30
  documents.push(_objectSpread(_objectSpread({}, document), {}, {
@@ -36,6 +36,17 @@ describe('Utils.Meta.documents.add', function () {
36
36
 
37
37
  expect((0, _setDocumentForField.default)(NEW_ALPHA, FORM_DATA, NEW_ALPHA.field)).toEqual(_defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [NEW_ALPHA]));
38
38
  });
39
+ it('should add a document with the same field if allowMultiple is true', function () {
40
+ var NEW_URL = 'http://replacement.com/files/alpha';
41
+ var NEW_ALPHA = {
42
+ field: ALPHA_DOCUMENT.field,
43
+ url: NEW_URL
44
+ };
45
+
46
+ var FORM_DATA = _defineProperty({}, _constants.META_PROPERTY, _defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [ALPHA_DOCUMENT]));
47
+
48
+ expect((0, _setDocumentForField.default)(NEW_ALPHA, FORM_DATA, NEW_ALPHA.field, true)).toEqual(_defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [ALPHA_DOCUMENT, NEW_ALPHA]));
49
+ });
39
50
  it('should handle a null document being passed where a document with that field already exists', function () {
40
51
  var FORM_DATA = _defineProperty({}, _constants.META_PROPERTY, _defineProperty({}, _constants.META_DOCUMENTS_PROPERTY, [ALPHA_DOCUMENT]));
41
52
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.14.3",
3
+ "version": "4.15.1-alpha",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",
@@ -16,7 +16,7 @@
16
16
  "post-compile": "rimraf dist/*.test.* dist/**/*.test.* dist/**/*.stories.* dist/docs dist/assets"
17
17
  },
18
18
  "dependencies": {
19
- "@ukhomeoffice/cop-react-components": "2.2.3",
19
+ "@ukhomeoffice/cop-react-components": "2.2.4",
20
20
  "axios": "^0.23.0",
21
21
  "dayjs": "^1.11.0",
22
22
  "govuk-frontend": "^4.3.1",