@ukhomeoffice/cop-react-form-renderer 6.16.2-beta → 6.16.2-delta

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.
@@ -90,10 +90,6 @@ const onPageAction = (action, patch, patchLabel, hooks, data, formState, validat
90
90
  }
91
91
  // If resetActiveId is false
92
92
  if (collectionArray && collectionArray.length > 0 && !action.resetActiveId) {
93
- // Truncate collection if user has selected no to more entries
94
- if (index !== undefined && action.truncateCollection && form.page.formData[addAnotherName] === 'No') {
95
- form.page.formData[collectionName] = collectionArray.filter((_data, idx) => idx <= index);
96
- }
97
93
  // Set addAnother field to yes if not reached last entry in collection
98
94
  if (index !== undefined && index + 1 < form.page.formData[collectionName].length) {
99
95
  form.page.formData[addAnotherName] = 'Yes';
@@ -162,6 +158,7 @@ const onPageAction = (action, patch, patchLabel, hooks, data, formState, validat
162
158
  delete form.page.formData["".concat(action.collection, "ActiveId")];
163
159
  delete form.page.formData[action.collection];
164
160
  }
161
+ pageUpdate = () => _handlers.default.navigate(action, pageId, onPageChange);
165
162
  break;
166
163
  default:
167
164
  break;
@@ -565,8 +565,8 @@ describe('components.FormRenderer.onPageAction', () => {
565
565
  });
566
566
  postActionChecks(CUSTOM_ARGS);
567
567
 
568
- // This action type uses the default onPageChange.
569
- expect(onPageChangeCalls).toEqual(1);
568
+ // This action type uses handlers navigate callback
569
+ expect(_handlers.default.navigateCalls).toEqual(1);
570
570
  });
571
571
  it("should not store a reference to the removed item if action.recordRemoval is false", () => {
572
572
  const FORM_STATE = {
@@ -598,8 +598,8 @@ describe('components.FormRenderer.onPageAction', () => {
598
598
  expect(FORM_STATE.page.formData.testCollectionLastRemoved).toBeUndefined();
599
599
  postActionChecks(CUSTOM_ARGS);
600
600
 
601
- // This action type uses the default onPageChange.
602
- expect(onPageChangeCalls).toEqual(1);
601
+ // This action type uses handlers navigate callback
602
+ expect(_handlers.default.navigateCalls).toEqual(1);
603
603
  });
604
604
  it("should not store a reference to the removed item if action.recordRemoval is not defined", () => {
605
605
  const FORM_STATE = {
@@ -630,8 +630,8 @@ describe('components.FormRenderer.onPageAction', () => {
630
630
  expect(FORM_STATE.page.formData.testCollectionLastRemoved).toBeUndefined();
631
631
  postActionChecks(CUSTOM_ARGS);
632
632
 
633
- // This action type uses the default onPageChange.
634
- expect(onPageChangeCalls).toEqual(1);
633
+ // This action type uses handlers navigate callback
634
+ expect(_handlers.default.navigateCalls).toEqual(1);
635
635
  });
636
636
  it("should clean collection fields at top level of formData when entries remain", () => {
637
637
  // After removing the active entry, the collection data is assigned to the
@@ -675,8 +675,8 @@ describe('components.FormRenderer.onPageAction', () => {
675
675
  expect(FORM_STATE.page.formData.testCollectionActiveId).toEqual('leaveMeAlone');
676
676
  postActionChecks(CUSTOM_ARGS);
677
677
 
678
- // This action type uses the default onPageChange.
679
- expect(onPageChangeCalls).toEqual(1);
678
+ // This action type uses handlers navigate callback
679
+ expect(_handlers.default.navigateCalls).toEqual(1);
680
680
  });
681
681
  it("should clean collection fields at top level of formData after last entry removed", () => {
682
682
  // After removing the last active entry, the collection array and activeID are deleted,
@@ -714,8 +714,8 @@ describe('components.FormRenderer.onPageAction', () => {
714
714
  expect(FORM_STATE.page.formData.testCollectionActiveId).toBeUndefined();
715
715
  postActionChecks(CUSTOM_ARGS);
716
716
 
717
- // This action type uses the default onPageChange.
718
- expect(onPageChangeCalls).toEqual(1);
717
+ // This action type uses handlers navigate callback
718
+ expect(_handlers.default.navigateCalls).toEqual(1);
719
719
  });
720
720
  });
721
721
  describe('if validation fails', () => {
@@ -811,44 +811,6 @@ describe('components.FormRenderer.onPageAction', () => {
811
811
  expect(_handlers.default.navigateCalls).toEqual(1);
812
812
  expect(FORM_STATE.page.formData["".concat(ACTION.collection, "ActiveId")]).toEqual('1');
813
813
  });
814
- it("should work for the ".concat(_models.PageAction.TYPES.SAVE_AND_NAVIGATE, " action type to truncate the collection"), () => {
815
- // In this test the action includes the truncateCollection field
816
- // and if the user has selected no to more entries
817
- // then all entries after current index (from active ID) are truncated.
818
- const FORM_STATE = {
819
- page: {
820
- formData: {
821
- tests: [{
822
- id: '1'
823
- }, {
824
- id: '2'
825
- }, {
826
- id: '3'
827
- }],
828
- testsActiveId: '2',
829
- addAnotherTest: 'No'
830
- }
831
- }
832
- };
833
- const ACTION = {
834
- type: _models.PageAction.TYPES.SAVE_AND_NAVIGATE,
835
- collection: 'tests',
836
- truncateCollection: true
837
- };
838
- const CUSTOM_ARGS = _objectSpread(_objectSpread({}, ARGS), {}, {
839
- formState: FORM_STATE,
840
- action: ACTION
841
- });
842
- _onPageAction.default.apply(void 0, Object.values(CUSTOM_ARGS));
843
- preActionChecks();
844
- // All the actions does in this case is set a function
845
- // to be called in the onSuccess callback for the onSubmit hook.
846
- postActionChecks(CUSTOM_ARGS);
847
- expect(_handlers.default.navigateCalls).toEqual(1);
848
- expect(FORM_STATE.page.formData["".concat(ACTION.collection, "ActiveId")]).toEqual('2');
849
- expect(FORM_STATE.page.formData[ACTION.collection].length).toEqual(2);
850
- expect(FORM_STATE.page.formData['addAnotherTest']).toEqual('No');
851
- });
852
814
  it("should work for the ".concat(_models.PageAction.TYPES.SAVE_AND_NAVIGATE, " action type to set addAnother to yes"), () => {
853
815
  // If the user is midway through the collection
854
816
  // then the addAnother field is set to yes
@@ -884,7 +846,7 @@ describe('components.FormRenderer.onPageAction', () => {
884
846
  expect(_handlers.default.navigateCalls).toEqual(1);
885
847
  expect(FORM_STATE.page.formData["".concat(ACTION.collection, "ActiveId")]).toEqual('2');
886
848
  expect(FORM_STATE.page.formData[ACTION.collection].length).toEqual(3);
887
- expect(FORM_STATE.page.formData['addAnotherTest']).toEqual('Yes');
849
+ expect(FORM_STATE.page.formData.addAnotherTest).toEqual('Yes');
888
850
  });
889
851
  it("should work for the ".concat(_models.PageAction.TYPES.NAVIGATE, " action type"), () => {
890
852
  const FORM_STATE = {
@@ -19,6 +19,12 @@ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object
19
19
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
20
20
  function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } // Global imports
21
21
  // Local imports
22
+ const showAction = (data, action) => {
23
+ if (!action.show_when) {
24
+ return true;
25
+ }
26
+ return (0, _meetsAllConditions.default)(action.show_when, data);
27
+ };
22
28
  const getContainerForPage = (page, item, labelCount, fullPath) => ({
23
29
  id: item.id,
24
30
  fieldId: item.id,
@@ -74,10 +80,10 @@ const getCollectionNameHeading = (page, titleName, config, changeAction) => ({
74
80
  label: 'Change'
75
81
  }
76
82
  });
77
- const getActionRows = function (page, item, onAction, labelCount) {
83
+ const getActionRows = function (page, item, onAction, labelCount, data) {
78
84
  var _page$collection;
79
- let activeIds = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
80
- return (_page$collection = page.collection) === null || _page$collection === void 0 || (_page$collection = _page$collection.actions) === null || _page$collection === void 0 ? void 0 : _page$collection.map(action => {
85
+ let activeIds = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {};
86
+ return (_page$collection = page.collection) === null || _page$collection === void 0 || (_page$collection = _page$collection.actions) === null || _page$collection === void 0 ? void 0 : _page$collection.filter(action => showAction(data, action)).map(action => {
81
87
  if (action.type === 'remove') {
82
88
  return (0, _getCYACollectionDeleteAction.default)(page, item, onAction, labelCount, action, activeIds);
83
89
  }
@@ -211,7 +217,7 @@ const getCYARowsForCollectionPage = function (page, onAction, fnOverride, data)
211
217
  const labelCount = itemIndex + 1;
212
218
  const actionPosition = ((_page$collection2 = page.collection) === null || _page$collection2 === void 0 ? void 0 : _page$collection2.actionPosition) || 'top';
213
219
  const fullPath = "".concat(page.collection.name, "[").concat(itemIndex, "]");
214
- const actionRows = getActionRows(page, item, onAction, labelCount, activeIds);
220
+ const actionRows = getActionRows(page, item, onAction, labelCount, data, activeIds);
215
221
  if (!hideItemTitles) {
216
222
  const titleRow = getTitleRowForItem(page, item, page.id, labelCount, fullPath);
217
223
  rows = rows.concat(titleRow);
@@ -595,6 +595,55 @@ describe('utils.CheckYourAnswers.getCYARowsForCollectionPage', () => {
595
595
  expect(ROWS[ROWS.length - 2].action.label).toEqual('custom remove label');
596
596
  expect(ROWS[ROWS.length - 1].action.label).toEqual('custom change label');
597
597
  });
598
+ it('Show show the collection action when show_when condition is true', () => {
599
+ const FORM_DATA = {
600
+ collection: [{
601
+ id: '01',
602
+ testText: 'value'
603
+ }]
604
+ };
605
+ const PAGE = _objectSpread(_objectSpread({}, MASTER_PAGE), {}, {
606
+ collection: _objectSpread(_objectSpread({}, MASTER_PAGE.collection), {}, {
607
+ // Add a show_when to the colleciton action.
608
+ actions: MASTER_PAGE.collection.actions.map((action, index) => index === 1 ? _objectSpread(_objectSpread({}, action), {}, {
609
+ show_when: [{
610
+ field: "collection.length",
611
+ op: "=",
612
+ value: 1
613
+ }]
614
+ }) : action)
615
+ }),
616
+ formData: FORM_DATA
617
+ });
618
+ const ROWS = (0, _getCYARowsForCollectionPage.default)(PAGE, null, null, FORM_DATA);
619
+ expect(ROWS[2].action.label).toEqual('Change');
620
+ expect(ROWS[3].action.label).toEqual('custom remove label');
621
+ expect(ROWS[4].action.label).toEqual('custom change label');
622
+ });
623
+ it('Should hide the collection action when show_when condition is false', () => {
624
+ const FORM_DATA = {
625
+ collection: [{
626
+ id: '01',
627
+ testText: 'value'
628
+ }]
629
+ };
630
+ const PAGE = _objectSpread(_objectSpread({}, MASTER_PAGE), {}, {
631
+ collection: _objectSpread(_objectSpread({}, MASTER_PAGE.collection), {}, {
632
+ // Add a show_when to the colleciton action.
633
+ actions: MASTER_PAGE.collection.actions.map((action, index) => index === 1 ? _objectSpread(_objectSpread({}, action), {}, {
634
+ show_when: [{
635
+ field: "collection.length",
636
+ op: "=",
637
+ value: 10
638
+ }]
639
+ }) : action)
640
+ }),
641
+ formData: FORM_DATA
642
+ });
643
+ const ROWS = (0, _getCYARowsForCollectionPage.default)(PAGE, null, null, FORM_DATA);
644
+ expect(ROWS[2].action.label).toEqual('Change');
645
+ expect(ROWS[3].action.label).toEqual('custom change label');
646
+ });
598
647
  it('should ignore the result of the override function if it returns nullish', () => {
599
648
  const FORM_DATA = {
600
649
  collection: [{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "6.16.2-beta",
3
+ "version": "6.16.2-delta",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",