@ukhomeoffice/cop-react-form-renderer 4.8.0 → 4.9.1

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.
@@ -5,13 +5,11 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _Utils = _interopRequireDefault(require("@ukhomeoffice/cop-react-components/dist/utils/Utils"));
9
-
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+ var _copReactComponents = require("@ukhomeoffice/cop-react-components");
11
9
 
12
10
  var getComponentDisabled = function getComponentDisabled(disabled, formData) {
13
11
  if (typeof disabled === 'string') {
14
- return !!_Utils.default.interpolateString(disabled, formData);
12
+ return !!_copReactComponents.Utils.interpolateString(disabled, formData);
15
13
  }
16
14
 
17
15
  return !!disabled;
@@ -18,9 +18,11 @@ describe('components.FormComponent.helpers.getComponentDisabled', function () {
18
18
  expect((0, _getComponentDisabled.default)(true, DATA)).toEqual(true);
19
19
  });
20
20
  it('should interpolate correctly if disabled is a field path', function () {
21
+ // eslint-disable-next-line no-template-curly-in-string
21
22
  expect((0, _getComponentDisabled.default)('${shouldDisable}', DATA)).toEqual(true);
22
23
  });
23
24
  it('should return false if interpolating a field that does not exist', function () {
25
+ // eslint-disable-next-line no-template-curly-in-string
24
26
  expect((0, _getComponentDisabled.default)('${notARealField}', DATA)).toEqual(false);
25
27
  });
26
28
  });
@@ -103,7 +103,7 @@ var DEFAULT_CLASS = 'hods-form';
103
103
  exports.DEFAULT_CLASS = DEFAULT_CLASS;
104
104
 
105
105
  var InternalFormRenderer = function InternalFormRenderer(_ref2) {
106
- var _formState$page3;
106
+ var _formState$page4;
107
107
 
108
108
  var title = _ref2.title,
109
109
  type = _ref2.type,
@@ -165,7 +165,12 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
165
165
  var _useState17 = (0, _react.useState)('load'),
166
166
  _useState18 = _slicedToArray(_useState17, 2),
167
167
  pagePoint = _useState18[0],
168
- setPagePoint = _useState18[1]; // Set up hooks.
168
+ setPagePoint = _useState18[1];
169
+
170
+ var _useState19 = (0, _react.useState)(false),
171
+ _useState20 = _slicedToArray(_useState19, 2),
172
+ goingBack = _useState20[0],
173
+ setGoingBack = _useState20[1]; // Set up hooks.
169
174
 
170
175
 
171
176
  var _useHooks = (0, _hooks.useHooks)(),
@@ -181,6 +186,10 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
181
186
  window.onpopstate = function () {
182
187
  var _formState$page;
183
188
 
189
+ setGoingBack(true);
190
+ hooks.onGoingBack();
191
+ clearErrors();
192
+
184
193
  if (components && pages && data && (_formState$page = formState.page) !== null && _formState$page !== void 0 && _formState$page.formData && pagePoint === undefined) {
185
194
  var submissionData = _utils.default.Format.form({
186
195
  pages: pages,
@@ -219,8 +228,16 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
219
228
  }, [type, _hub, data, components, setHub]); // Form state.
220
229
 
221
230
  (0, _react.useEffect)(function () {
231
+ var _formState$page2;
232
+
233
+ // revert formData changes if going back in hub-and-spoke
234
+ if (type === _models.FormTypes.HUB && goingBack && data && (_formState$page2 = formState.page) !== null && _formState$page2 !== void 0 && _formState$page2.formData) {
235
+ formState.page.formData = _objectSpread({}, data);
236
+ }
237
+
238
+ setGoingBack(false);
222
239
  setFormState(_helpers.default.getFormState(pageId, pages, hub));
223
- }, [pages, hub, pageId, setFormState]); // Call the onFormLoad hook just when this component first renders.
240
+ }, [pages, hub, pageId, setFormState, type, goingBack, data, formState.page]); // Call the onFormLoad hook just when this component first renders.
224
241
 
225
242
  (0, _react.useEffect)(function () {
226
243
  setPagePoint(undefined);
@@ -304,7 +321,7 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
304
321
 
305
322
  switch (action.type) {
306
323
  case _models.PageAction.TYPES.SUBMIT:
307
- setPagePoint('end');
324
+ setPagePoint('submit');
308
325
  break;
309
326
 
310
327
  case _models.PageAction.TYPES.SAVE_AND_NAVIGATE:
@@ -353,6 +370,15 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
353
370
 
354
371
  if (patch) {
355
372
  setData(submissionData);
373
+ } // In case of hub-and-spoke if patch has changed then save field name and value to variables for call to onSubmit hook
374
+
375
+
376
+ var changedFieldName = undefined;
377
+ var changedFieldValue = undefined;
378
+
379
+ if (type === _models.FormTypes.HUB && Object.keys(patch).length > 0) {
380
+ changedFieldName = formState.page.components[0].cya_label;
381
+ changedFieldValue = Object.values(patch)[0];
356
382
  } // Now submit the data to the backend...
357
383
 
358
384
 
@@ -367,7 +393,7 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
367
393
  pageUpdate(sData);
368
394
  }, function (errors) {
369
395
  _handlers.default.submissionError(errors, addErrors);
370
- });
396
+ }, changedFieldName, changedFieldValue);
371
397
  }
372
398
  }
373
399
  }; // Handle navigation from "Check your answers".
@@ -479,9 +505,9 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
479
505
  }
480
506
 
481
507
  if (action.type === _models.PageAction.TYPES.NAVIGATE) {
482
- var _formState$page2;
508
+ var _formState$page3;
483
509
 
484
- onPageChange(_helpers.default.getNextPageId(type, pages, pageId, action, (_formState$page2 = formState.page) === null || _formState$page2 === void 0 ? void 0 : _formState$page2.formData));
510
+ onPageChange(_helpers.default.getNextPageId(type, pages, pageId, action, (_formState$page3 = formState.page) === null || _formState$page3 === void 0 ? void 0 : _formState$page3.formData));
485
511
  }
486
512
 
487
513
  if (action.type === _models.PageAction.TYPES.CANCEL) {
@@ -499,7 +525,7 @@ var InternalFormRenderer = function InternalFormRenderer(_ref2) {
499
525
  className: classes()
500
526
  }, title && !hide_title && pageId === _models.FormPages.HUB && /*#__PURE__*/_react.default.createElement(_copReactComponents.LargeHeading, null, title), formState.cya && /*#__PURE__*/_react.default.createElement(_CheckYourAnswers.default, _extends({
501
527
  pages: _helpers.default.getRelevantPages(formState, pages, currentTask.fullPages)
502
- }, cya, ((_formState$page3 = formState.page) === null || _formState$page3 === void 0 ? void 0 : _formState$page3.type) === _models.FormPages.PARTIAL_CYA && {
528
+ }, cya, ((_formState$page4 = formState.page) === null || _formState$page4 === void 0 ? void 0 : _formState$page4.type) === _models.FormPages.PARTIAL_CYA && {
503
529
  actions: formState.page.actions
504
530
  }, formState.cya, {
505
531
  onAction: onCYAAction,
@@ -32,6 +32,7 @@ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Sy
32
32
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
33
33
 
34
34
  var DEFAULT_HOOKS = {
35
+ onGoingBack: function onGoingBack() {},
35
36
  onFormComplete: function onFormComplete() {},
36
37
  onFormLoad: function onFormLoad() {},
37
38
  onGetComponent: function onGetComponent(config, wrap) {
@@ -59,6 +60,7 @@ var HooksContextProvider = function HooksContextProvider(_ref) {
59
60
  children = _ref.children;
60
61
 
61
62
  var _useState = (0, _react.useState)({
63
+ onGoingBack: (overrides === null || overrides === void 0 ? void 0 : overrides.onGoingBack) || DEFAULT_HOOKS.onGoingBack,
62
64
  onFormComplete: (overrides === null || overrides === void 0 ? void 0 : overrides.onFormComplete) || DEFAULT_HOOKS.onFormComplete,
63
65
  onFormLoad: (overrides === null || overrides === void 0 ? void 0 : overrides.onFormLoad) || DEFAULT_HOOKS.onFormLoad,
64
66
  onGetComponent: (overrides === null || overrides === void 0 ? void 0 : overrides.onGetComponent) || DEFAULT_HOOKS.onGetComponent,
@@ -38,7 +38,7 @@ describe('utils', function () {
38
38
  it('should remove selected item from the form data', function () {
39
39
  var data = null;
40
40
 
41
- var ON_ACTION = function ON_ACTION(undefined, call_back) {
41
+ var ON_ACTION = function ON_ACTION(_, call_back) {
42
42
  data = call_back(TEST_DATA);
43
43
  };
44
44
 
@@ -22,6 +22,7 @@ describe('utils.CheckYourAnswers.getCYARowsForCollectionPage', function () {
22
22
  collection: {
23
23
  name: 'collection',
24
24
  labels: {
25
+ // eslint-disable-next-line no-template-curly-in-string
25
26
  item: 'Collection entry ${index}'
26
27
  },
27
28
  actions: [{
@@ -32,6 +32,7 @@ describe('Utils.Operate.getIndexOfPriorMatchingValueIn', function () {
32
32
  expect(result).toEqual('2');
33
33
  });
34
34
  it('Should handle interpolated string for field', function () {
35
+ // eslint-disable-next-line no-template-curly-in-string
35
36
  var CONFIG = {
36
37
  target: 'arrayOfValues',
37
38
  field: '${fieldName}'
@@ -62,6 +63,7 @@ describe('Utils.Operate.getIndexOfPriorMatchingValueIn', function () {
62
63
  expect(result).toEqual(null); // Cutoff occurs before matching value is reached.
63
64
  });
64
65
  it('Should handle interpolated string for cutoff.field', function () {
66
+ // eslint-disable-next-line no-template-curly-in-string
65
67
  var CONFIG = {
66
68
  target: 'arrayOfValues',
67
69
  value: 'cde',
@@ -95,6 +97,7 @@ describe('Utils.Operate.getIndexOfPriorMatchingValueIn', function () {
95
97
  expect(result).toEqual(null); // Should ignore the matching value.
96
98
  });
97
99
  it('Should handle interpolated string for ignore.field', function () {
100
+ // eslint-disable-next-line no-template-curly-in-string
98
101
  var CONFIG = {
99
102
  target: 'arrayOfValues',
100
103
  value: 'cde',
@@ -106,6 +109,7 @@ describe('Utils.Operate.getIndexOfPriorMatchingValueIn', function () {
106
109
  expect(result).toEqual(null); // Should ignore the matching value.
107
110
  });
108
111
  it('Should handle interpolated string for target', function () {
112
+ // eslint-disable-next-line no-template-curly-in-string
109
113
  var CONFIG = {
110
114
  target: '${targetName}',
111
115
  value: 'cde'
@@ -90,6 +90,7 @@ describe('Utils.Operate.persistValueInFormData', function () {
90
90
  expect(ON_CHANGE_COUNT).toEqual(0);
91
91
  });
92
92
  it('Should correctly interpolate a field value', function () {
93
+ // eslint-disable-next-line no-template-curly-in-string
93
94
  var CONFIG = {
94
95
  name: 'alpha',
95
96
  field: '${beta}'
@@ -63,6 +63,7 @@ describe('Utils.Operate.runPageOperations', function () {
63
63
  }, {
64
64
  output: 'secondOpResult',
65
65
  function: 'setValueInFormData',
66
+ // eslint-disable-next-line no-template-curly-in-string
66
67
  field: '${firstOpResult}'
67
68
  }]
68
69
  };
@@ -77,6 +78,7 @@ describe('Utils.Operate.runPageOperations', function () {
77
78
  operations: [{
78
79
  function: 'setValueInFormData',
79
80
  value: 'leaf',
81
+ // eslint-disable-next-line no-template-curly-in-string
80
82
  output: '${delta}.trunk.branch'
81
83
  }]
82
84
  };
@@ -25,6 +25,7 @@ describe('Utils.Operate.addToFormData', function () {
25
25
  expect(result).toEqual(DATA.a);
26
26
  });
27
27
  it('Should handle interpolated field strings', function () {
28
+ // eslint-disable-next-line no-template-curly-in-string
28
29
  var CONFIG = {
29
30
  field: 'b[${indexOfThree}]'
30
31
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "4.8.0",
3
+ "version": "4.9.1",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",