@ukhomeoffice/cop-react-form-renderer 1.0.0-gamma → 2.1.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.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +12 -0
  3. package/dist/components/CheckYourAnswers/CheckYourAnswers.js +16 -5
  4. package/dist/components/CheckYourAnswers/CheckYourAnswers.stories.mdx +199 -79
  5. package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +44 -0
  6. package/dist/components/FormPage/FormPage.js +8 -2
  7. package/dist/components/FormPage/FormPage.test.js +3 -3
  8. package/dist/components/FormRenderer/FormRenderer.js +44 -16
  9. package/dist/components/FormRenderer/FormRenderer.stories.mdx +115 -49
  10. package/dist/components/FormRenderer/FormRenderer.test.js +40 -0
  11. package/dist/components/FormRenderer/handlers/getPageId.js +1 -10
  12. package/dist/components/FormRenderer/handlers/getPageId.test.js +5 -31
  13. package/dist/components/FormRenderer/handlers/handlers.test.js +2 -2
  14. package/dist/components/FormRenderer/helpers/getNextPageId.js +33 -26
  15. package/dist/components/FormRenderer/helpers/getNextPageId.test.js +89 -7
  16. package/dist/components/FormRenderer/helpers/getSubmissionStatus.js +27 -0
  17. package/dist/components/FormRenderer/helpers/getSubmissionStatus.test.js +209 -0
  18. package/dist/components/FormRenderer/helpers/index.js +4 -1
  19. package/dist/components/PageActions/ActionButton.test.js +1 -1
  20. package/dist/components/PageActions/PageActions.stories.mdx +1 -1
  21. package/dist/components/PageActions/PageActions.test.js +5 -5
  22. package/dist/components/SummaryList/RowAction.js +1 -1
  23. package/dist/components/SummaryList/RowAction.test.js +6 -6
  24. package/dist/components/SummaryList/SummaryList.js +8 -5
  25. package/dist/components/SummaryList/SummaryList.scss +10 -2
  26. package/dist/components/SummaryList/SummaryList.stories.mdx +64 -10
  27. package/dist/components/SummaryList/SummaryList.test.js +54 -0
  28. package/dist/components/SummaryList/helpers/getRowActionAttributes.js +2 -2
  29. package/dist/components/SummaryList/helpers/getRowActionAttributes.test.js +4 -4
  30. package/dist/components/index.js +8 -0
  31. package/dist/index.js +8 -0
  32. package/dist/json/areYouACivilServant.json +7 -0
  33. package/dist/json/firstForm.json +94 -0
  34. package/dist/json/grade.json +108 -0
  35. package/dist/json/saveAndContinue.json +98 -0
  36. package/dist/json/team.json +17351 -0
  37. package/dist/json/userProfile.data.json +14 -0
  38. package/dist/json/userProfile.json +276 -0
  39. package/dist/models/ComponentTypes.js +7 -1
  40. package/dist/models/PageAction.js +9 -7
  41. package/dist/utils/CheckYourAnswers/getCYAAction.js +24 -3
  42. package/dist/utils/CheckYourAnswers/getCYAAction.test.js +62 -15
  43. package/dist/utils/CheckYourAnswers/getCYARow.js +8 -2
  44. package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +11 -5
  45. package/dist/utils/Component/getComponent.js +32 -0
  46. package/dist/utils/Component/getComponentTests/getComponent.autocomplete.test.js +75 -0
  47. package/dist/utils/Component/getComponentTests/getComponent.checkboxes.test.js +129 -0
  48. package/dist/utils/Component/getComponentTests/getComponent.date.test.js +129 -0
  49. package/dist/utils/Component/getComponentTests/getComponent.email.test.js +80 -0
  50. package/dist/utils/Component/getComponentTests/getComponent.heading.test.js +33 -0
  51. package/dist/utils/Component/getComponentTests/getComponent.html.test.js +45 -0
  52. package/dist/utils/Component/getComponentTests/getComponent.insetText.test.js +31 -0
  53. package/dist/utils/Component/getComponentTests/getComponent.phoneNumber.test.js +80 -0
  54. package/dist/utils/Component/getComponentTests/getComponent.radios.test.js +125 -0
  55. package/dist/utils/Component/getComponentTests/getComponent.text.test.js +80 -0
  56. package/dist/utils/Component/getComponentTests/getComponent.textarea.test.js +76 -0
  57. package/dist/utils/Component/getComponentTests/getComponent.unknown.test.js +14 -0
  58. package/dist/utils/Component/isEditable.js +1 -1
  59. package/dist/utils/Data/refDataToOptions.js +4 -0
  60. package/dist/utils/Data/refDataToOptions.test.js +30 -0
  61. package/dist/utils/FormPage/getEditableComponents.js +28 -0
  62. package/dist/utils/FormPage/getEditableComponents.test.js +75 -0
  63. package/dist/utils/FormPage/getFormPage.js +5 -1
  64. package/dist/utils/FormPage/getPageActions.js +66 -0
  65. package/dist/utils/FormPage/getPageActions.test.js +89 -0
  66. package/dist/utils/FormPage/index.js +7 -1
  67. package/dist/utils/FormPage/showFormPage.js +81 -0
  68. package/dist/utils/FormPage/showFormPage.test.js +131 -0
  69. package/dist/utils/Validate/validateComponent.js +5 -3
  70. package/dist/utils/Validate/validateRequired.js +11 -0
  71. package/dist/utils/Validate/validateRequired.test.js +12 -0
  72. package/package.json +15 -10
  73. package/dist/index.test.js +0 -18
  74. package/dist/utils/Component/getComponent.test.js +0 -329
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+
3
+ var _models = require("../../../models");
4
+
5
+ var _getSubmissionStatus = _interopRequireDefault(require("./getSubmissionStatus"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ // Local imports
10
+ describe('components', function () {
11
+ describe('FormRenderer', function () {
12
+ describe('helpers', function () {
13
+ describe('getSubmissionStatus', function () {
14
+ var OPTIONS = [{
15
+ value: 'romeo',
16
+ label: 'Romeo'
17
+ }, {
18
+ value: 'juliet',
19
+ label: 'Juliet'
20
+ }];
21
+ var RADIOS = {
22
+ id: 'radios',
23
+ fieldId: 'radios',
24
+ type: _models.ComponentTypes.RADIOS,
25
+ data: {
26
+ options: OPTIONS
27
+ }
28
+ };
29
+ var TEXT = {
30
+ id: 'text',
31
+ fieldId: 'text',
32
+ type: _models.ComponentTypes.TEXT
33
+ };
34
+ var HTML = {
35
+ type: _models.ComponentTypes.HTML,
36
+ content: 'HTML'
37
+ };
38
+ var SHOW_WHEN_JULIET = {
39
+ field: RADIOS.fieldId,
40
+ op: '=',
41
+ value: OPTIONS[1].value
42
+ };
43
+ var SHOW_WHEN_ROMEO = {
44
+ field: RADIOS.fieldId,
45
+ op: '=',
46
+ value: OPTIONS[0].value
47
+ };
48
+ var FORM_DATA = {
49
+ radios: OPTIONS[0].value
50
+ }; // 'romeo'
51
+
52
+ var PAGES = [{
53
+ id: 'alpha'
54
+ }, {
55
+ id: 'bravo'
56
+ }, {
57
+ id: 'charlie',
58
+ components: [RADIOS]
59
+ }, {
60
+ id: 'delta',
61
+ components: [TEXT],
62
+ show_when: SHOW_WHEN_JULIET
63
+ }, {
64
+ id: 'echo',
65
+ components: [HTML]
66
+ }, {
67
+ id: 'foxtrot',
68
+ components: [TEXT],
69
+ show_when: SHOW_WHEN_ROMEO
70
+ }];
71
+ describe("when the action type is '".concat(_models.PageAction.TYPES.NAVIGATE, "'"), function () {
72
+ it('should return undefined if the action has no page property', function () {
73
+ var ACTION = {
74
+ type: _models.PageAction.TYPES.NAVIGATE
75
+ };
76
+ expect((0, _getSubmissionStatus.default)(_models.FormTypes.HUB, PAGES, PAGES[0].id, ACTION, FORM_DATA)).toMatchObject({
77
+ page: undefined
78
+ });
79
+ });
80
+ it('should return the page on the action if it exists among the pages', function () {
81
+ var PAGE = PAGES[1].id;
82
+ var ACTION = {
83
+ type: _models.PageAction.TYPES.NAVIGATE,
84
+ page: PAGE
85
+ };
86
+ expect((0, _getSubmissionStatus.default)(_models.FormTypes.HUB, PAGES, PAGES[0].id, ACTION, FORM_DATA)).toMatchObject({
87
+ page: PAGE
88
+ });
89
+ });
90
+ it('should return undefined if the page on the action does not exist among the pages', function () {
91
+ var PAGE = 'golf';
92
+ var ACTION = {
93
+ type: _models.PageAction.TYPES.NAVIGATE,
94
+ page: PAGE
95
+ };
96
+ expect((0, _getSubmissionStatus.default)(_models.FormTypes.HUB, PAGES, PAGES[0].id, ACTION, FORM_DATA)).toMatchObject({
97
+ page: undefined
98
+ });
99
+ });
100
+ });
101
+ describe("when the action type is '".concat(_models.PageAction.TYPES.SAVE_AND_RETURN, "'"), function () {
102
+ Object.values(_models.FormTypes).forEach(function (formType) {
103
+ it("should return the current page if the form type is '".concat(formType, "'"), function () {
104
+ var ACTION = _models.PageAction.DEFAULTS.saveAndReturn;
105
+ expect((0, _getSubmissionStatus.default)(formType, PAGES, PAGES[0].id, ACTION, FORM_DATA)).toMatchObject({
106
+ page: PAGES[0].id
107
+ });
108
+ });
109
+ });
110
+ });
111
+ describe("when the form type is '".concat(_models.FormTypes.HUB, "'"), function () {
112
+ var FORM_TYPE = _models.FormTypes.HUB;
113
+ it("should return '".concat(_models.FormPages.HUB, "' by default"), function () {
114
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES)).toMatchObject({
115
+ page: _models.FormPages.HUB
116
+ });
117
+ });
118
+ it('should return action.page if specified', function () {
119
+ var ACTION = {
120
+ page: 'bob'
121
+ };
122
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES, undefined, ACTION, FORM_DATA)).toMatchObject({
123
+ page: ACTION.page
124
+ });
125
+ });
126
+ });
127
+ describe("when the form type is '".concat(_models.FormTypes.CYA, "'"), function () {
128
+ var FORM_TYPE = _models.FormTypes.CYA;
129
+ var ACTION = _models.PageAction.DEFAULTS.saveAndContinue;
130
+ it('should return the first page by default', function () {
131
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES, ACTION, FORM_DATA)).toMatchObject({
132
+ page: PAGES[0].id
133
+ });
134
+ });
135
+ it('should return the second page when on the first page', function () {
136
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES, PAGES[0].id, ACTION, FORM_DATA)).toMatchObject({
137
+ page: PAGES[1].id
138
+ });
139
+ });
140
+ it('should return the third page when on the second page', function () {
141
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES, PAGES[1].id, ACTION, FORM_DATA)).toMatchObject({
142
+ page: PAGES[2].id
143
+ });
144
+ });
145
+ it("should return the fifth page when on the third page because the fourth page show_when is not met", function () {
146
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES, PAGES[2].id, ACTION, FORM_DATA)).toMatchObject({
147
+ page: PAGES[4].id
148
+ });
149
+ });
150
+ it("should return the sixth page when on the fifth page because the sixth page show_when is met", function () {
151
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES, PAGES[4].id, ACTION, FORM_DATA)).toMatchObject({
152
+ page: PAGES[5].id
153
+ });
154
+ });
155
+ it("should return '".concat(_models.FormPages.CYA, "' when on the sixth (last) page"), function () {
156
+ var lastPageIndex = PAGES.length - 1;
157
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES, PAGES[lastPageIndex].id, ACTION, FORM_DATA)).toMatchObject({
158
+ page: _models.FormPages.CYA
159
+ });
160
+ });
161
+ });
162
+ describe("when the form type is '".concat(_models.FormTypes.WIZARD, "'"), function () {
163
+ var FORM_TYPE = _models.FormTypes.WIZARD;
164
+ it('should return the first page by default', function () {
165
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES)).toMatchObject({
166
+ page: PAGES[0].id
167
+ });
168
+ });
169
+ it('should return the second page when on the first page', function () {
170
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES, PAGES[0].id)).toMatchObject({
171
+ page: PAGES[1].id
172
+ });
173
+ });
174
+ it('should return undefined when on the last page', function () {
175
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES, PAGES[PAGES.length - 1].id)).toMatchObject({
176
+ page: undefined
177
+ });
178
+ });
179
+ });
180
+ describe("when the form type is '".concat(_models.FormTypes.FORM, "'"), function () {
181
+ var FORM_TYPE = _models.FormTypes.FORM;
182
+ it("should always return the first page if there any pages and the action is not '".concat(_models.PageAction.TYPES.SAVE_AND_RETURN, "'"), function () {
183
+ var ACTION = {
184
+ type: _models.PageAction.TYPES.SUBMIT,
185
+ nextPageId: 'bob'
186
+ };
187
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES)).toMatchObject({
188
+ page: PAGES[0].id
189
+ });
190
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES, PAGES[0].id)).toMatchObject({
191
+ page: PAGES[0].id
192
+ });
193
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES, PAGES[1].id)).toMatchObject({
194
+ page: PAGES[0].id
195
+ });
196
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, PAGES, PAGES[0].id, ACTION)).toMatchObject({
197
+ page: PAGES[0].id
198
+ });
199
+ });
200
+ it('should return undefined if there are no pages', function () {
201
+ expect((0, _getSubmissionStatus.default)(FORM_TYPE, [])).toMatchObject({
202
+ page: undefined
203
+ });
204
+ });
205
+ });
206
+ });
207
+ });
208
+ });
209
+ });
@@ -13,6 +13,8 @@ var _getFormState = _interopRequireDefault(require("./getFormState"));
13
13
 
14
14
  var _getNextPageId = _interopRequireDefault(require("./getNextPageId"));
15
15
 
16
+ var _getSubmissionStatus = _interopRequireDefault(require("./getSubmissionStatus"));
17
+
16
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
19
 
18
20
  // Local imports
@@ -20,7 +22,8 @@ var helpers = {
20
22
  canActionProceed: _canActionProceed.default,
21
23
  canCYASubmit: _canCYASubmit.default,
22
24
  getFormState: _getFormState.default,
23
- getNextPageId: _getNextPageId.default
25
+ getNextPageId: _getNextPageId.default,
26
+ getSubmissionStatus: _getSubmissionStatus.default
24
27
  };
25
28
  exports.helpers = helpers;
26
29
  var _default = helpers;
@@ -59,7 +59,7 @@ describe('components', function () {
59
59
  while (1) {
60
60
  switch (_context2.prev = _context2.next) {
61
61
  case 0:
62
- ACTION = 'submit';
62
+ ACTION = _models.PageAction.TYPES.SUBMIT;
63
63
  _render2 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_ActionButton.default, {
64
64
  action: ACTION,
65
65
  onAction: ON_ACTION
@@ -43,7 +43,7 @@ Renders the buttons at the bottom of a Page.
43
43
  <Canvas>
44
44
  <Story name="Custom action">
45
45
  {() => {
46
- const ACTIONS = [{ type: 'navigate', url: '/profile', label: 'Go to profile' }];
46
+ const ACTIONS = [{ type: 'navigate', page: 'profile', label: 'Go to profile' }];
47
47
  const ON_ACTION = (action, patch, onError) => {
48
48
  console.log('action invoked', action, patch);
49
49
  };
@@ -55,7 +55,7 @@ describe('components', function () {
55
55
  while (1) {
56
56
  switch (_context2.prev = _context2.next) {
57
57
  case 0:
58
- ACTIONS = ['submit'];
58
+ ACTIONS = [_models.PageAction.TYPES.SUBMIT];
59
59
  _render2 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_PageActions.default, {
60
60
  actions: ACTIONS,
61
61
  onAction: ON_ACTION
@@ -87,7 +87,7 @@ describe('components', function () {
87
87
  case 0:
88
88
  ACTIONS = [{
89
89
  type: 'navigate',
90
- url: '/alpha',
90
+ page: 'alpha',
91
91
  label: 'Alpha'
92
92
  }];
93
93
  _render3 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_PageActions.default, {
@@ -120,11 +120,11 @@ describe('components', function () {
120
120
  switch (_context4.prev = _context4.next) {
121
121
  case 0:
122
122
  NAVIGATE = {
123
- type: 'navigate',
124
- url: '/alpha',
123
+ type: _models.PageAction.TYPES.NAVIGATE,
124
+ page: 'alpha',
125
125
  label: 'Alpha'
126
126
  };
127
- ACTIONS = [NAVIGATE, 'submit'];
127
+ ACTIONS = [NAVIGATE, _models.PageAction.TYPES.SUBMIT];
128
128
  _render4 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_PageActions.default, {
129
129
  actions: ACTIONS,
130
130
  onAction: ON_ACTION
@@ -56,7 +56,7 @@ RowAction.propTypes = {
56
56
  row: _propTypes.default.shape({
57
57
  action: _propTypes.default.shape({
58
58
  label: _propTypes.default.string.isRequired,
59
- href: _propTypes.default.string,
59
+ page: _propTypes.default.string,
60
60
  aria_suffix: _propTypes.default.string,
61
61
  onAction: _propTypes.default.func
62
62
  })
@@ -23,10 +23,10 @@ describe('components', function () {
23
23
  expect(container.childNodes.length).toEqual(0);
24
24
  });
25
25
  it('should handle a row with an href in the action', function () {
26
- var HREF = 'http://alpha.homeoffice.gov.uk';
26
+ var PAGE = 'alpha';
27
27
  var ROW = {
28
28
  action: {
29
- href: HREF,
29
+ page: PAGE,
30
30
  label: 'Change'
31
31
  }
32
32
  };
@@ -38,14 +38,14 @@ describe('components', function () {
38
38
 
39
39
  var link = container.childNodes[0];
40
40
  expect(link.tagName).toEqual('A');
41
- expect(link.getAttribute('href')).toEqual(HREF);
41
+ expect(link.getAttribute('href')).toEqual("/".concat(PAGE));
42
42
  expect(link.textContent).toEqual(ROW.action.label);
43
43
  });
44
44
  it('should render an aria_suffix appropriately', function () {
45
- var HREF = 'http://alpha.homeoffice.gov.uk';
45
+ var PAGE = 'alpha';
46
46
  var ROW = {
47
47
  action: {
48
- href: HREF,
48
+ page: PAGE,
49
49
  label: 'Change',
50
50
  aria_suffix: 'the thing'
51
51
  }
@@ -58,7 +58,7 @@ describe('components', function () {
58
58
 
59
59
  var link = container.childNodes[0];
60
60
  expect(link.tagName).toEqual('A');
61
- expect(link.getAttribute('href')).toEqual(HREF);
61
+ expect(link.getAttribute('href')).toEqual("/".concat(PAGE));
62
62
  expect(link.textContent).toEqual("".concat(ROW.action.label, " ").concat(ROW.action.aria_suffix));
63
63
  });
64
64
  it('should handle a row with an onAction in the action', function () {
@@ -15,7 +15,7 @@ var _RowAction = _interopRequireDefault(require("./RowAction"));
15
15
 
16
16
  require("./SummaryList.scss");
17
17
 
18
- var _excluded = ["rows", "classBlock", "classModifiers", "className"];
18
+ var _excluded = ["rows", "noChangeAction", "classBlock", "classModifiers", "className"];
19
19
 
20
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
21
 
@@ -30,6 +30,7 @@ exports.DEFAULT_CLASS = DEFAULT_CLASS;
30
30
 
31
31
  var SummaryList = function SummaryList(_ref) {
32
32
  var rows = _ref.rows,
33
+ noChangeAction = _ref.noChangeAction,
33
34
  classBlock = _ref.classBlock,
34
35
  classModifiers = _ref.classModifiers,
35
36
  className = _ref.className,
@@ -47,7 +48,7 @@ var SummaryList = function SummaryList(_ref) {
47
48
  className: classes('key')
48
49
  }, row.key), /*#__PURE__*/_react.default.createElement("dd", {
49
50
  className: classes('value')
50
- }, row.value), /*#__PURE__*/_react.default.createElement("dd", {
51
+ }, row.value), !noChangeAction && /*#__PURE__*/_react.default.createElement("dd", {
51
52
  className: classes('actions')
52
53
  }, row.action && /*#__PURE__*/_react.default.createElement(_RowAction.default, {
53
54
  row: row
@@ -62,7 +63,7 @@ SummaryList.propTypes = {
62
63
  key: _propTypes.default.string.isRequired,
63
64
  value: _propTypes.default.any,
64
65
  action: _propTypes.default.shape({
65
- href: _propTypes.default.string,
66
+ page: _propTypes.default.string,
66
67
  label: _propTypes.default.string,
67
68
  aria_suffix: _propTypes.default.string,
68
69
  onAction: _propTypes.default.func
@@ -70,10 +71,12 @@ SummaryList.propTypes = {
70
71
  })).isRequired,
71
72
  classBlock: _propTypes.default.string,
72
73
  classModifiers: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.arrayOf(_propTypes.default.string)]),
73
- className: _propTypes.default.string
74
+ className: _propTypes.default.string,
75
+ noChangeAction: _propTypes.default.bool
74
76
  };
75
77
  SummaryList.defaultProps = {
76
- classBlock: DEFAULT_CLASS
78
+ classBlock: DEFAULT_CLASS,
79
+ noChangeAction: false
77
80
  };
78
81
  var _default = SummaryList;
79
82
  exports.default = _default;
@@ -1,5 +1,5 @@
1
- @import "node_modules/govuk-frontend/govuk/_base";
2
- @import "node_modules/govuk-frontend/govuk/components/summary-list/_summary-list";
1
+ @import 'node_modules/govuk-frontend/govuk/_base';
2
+ @import 'node_modules/govuk-frontend/govuk/components/summary-list/_summary-list';
3
3
 
4
4
  .govuk-summary-list__actions {
5
5
  .govuk-link {
@@ -7,3 +7,11 @@
7
7
  cursor: pointer;
8
8
  }
9
9
  }
10
+
11
+ .caseview__key {
12
+ font-weight: normal;
13
+ }
14
+
15
+ .caseview__value {
16
+ @include govuk-typography-weight-bold;
17
+ }
@@ -1,36 +1,90 @@
1
1
  <!-- Global imports -->
2
+
2
3
  import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs';
3
4
  import { Details, Heading } from '@ukhomeoffice/cop-react-components';
4
5
 
5
6
  <!-- Local imports -->
7
+
6
8
  import Utils from '../../utils';
7
9
  import SummaryList from './SummaryList';
8
10
 
9
- <Meta title="Components/Summary list" id="D-SummaryList" component={ SummaryList } />
11
+ <Meta
12
+ title='Components/Summary list'
13
+ id='D-SummaryList'
14
+ component={SummaryList}
15
+ />
10
16
 
11
- <Heading size="xl" caption="Components">Summary list</Heading>
17
+ <Heading size='xl' caption='Components'>
18
+ Summary list
19
+ </Heading>
12
20
 
13
21
  Renders a list of key-value pairs, most commonly on the **Check your answers** screen, and optionally
14
22
  display action links.
15
23
 
16
24
  <Canvas withToolbar>
17
- <Story name="Default">
25
+ <Story name='Default'>
18
26
  {() => {
19
27
  const onAction = (row) => {
20
28
  console.log('action clicked', row);
21
29
  };
22
30
  const ROWS = [
23
- { pageId: 'p1', fieldId: 'forename', key: 'Forename(s)', value: 'John' },
31
+ {
32
+ pageId: 'p1',
33
+ fieldId: 'forename',
34
+ key: 'Forename(s)',
35
+ value: 'John',
36
+ },
24
37
  { pageId: 'p1', fieldId: 'surname', key: 'Surname', value: 'Smith' },
25
- { pageId: 'p2', fieldId: 'dob', key: 'Date of birth', value: '29/08/1993', action: { label: 'Change', onAction } },
38
+ {
39
+ pageId: 'p2',
40
+ fieldId: 'dob',
41
+ key: 'Date of birth',
42
+ value: '29/08/1993',
43
+ action: { label: 'Change', onAction },
44
+ },
26
45
  ];
27
- return (
28
- <SummaryList rows={ROWS} />
29
- );
46
+ return <SummaryList rows={ROWS} />;
30
47
  }}
31
48
  </Story>
32
49
  </Canvas>
33
50
 
34
- <Details summary="Properties" className="no-indent">
35
- <ArgsTable of={ SummaryList } />
51
+ <Details summary='Properties' className='no-indent'>
52
+ <ArgsTable of={SummaryList} />
36
53
  </Details>
54
+
55
+ ## Variant
56
+
57
+ ### Read-only style
58
+
59
+ <Canvas>
60
+ <Story name='Read-only style'>
61
+ {() => {
62
+ const onAction = (row) => {
63
+ console.log('action clicked', row);
64
+ };
65
+ const ROWS = [
66
+ {
67
+ pageId: 'p1',
68
+ fieldId: 'forename',
69
+ key: 'Forename(s)',
70
+ value: 'John',
71
+ },
72
+ { pageId: 'p1', fieldId: 'surname', key: 'Surname', value: 'Smith' },
73
+ {
74
+ pageId: 'p2',
75
+ fieldId: 'dob',
76
+ key: 'Date of birth',
77
+ value: '29/08/1993',
78
+ action: { label: 'Change', onAction },
79
+ },
80
+ ];
81
+ return (
82
+ <SummaryList
83
+ classModifiers='no-border'
84
+ rows={ROWS}
85
+ noChangeAction={true}
86
+ />
87
+ );
88
+ }}
89
+ </Story>
90
+ </Canvas>
@@ -54,6 +54,23 @@ describe('components', function () {
54
54
  return [key, value, actions];
55
55
  };
56
56
 
57
+ var checkRowNoChangeActions = function checkRowNoChangeActions(summaryList, index) {
58
+ var row = summaryList.childNodes[index];
59
+ expect(row.tagName).toEqual('DIV');
60
+ expect(row.classList).toContain("".concat(_SummaryList.DEFAULT_CLASS, "__row"));
61
+
62
+ var _row$childNodes2 = _slicedToArray(row.childNodes, 2),
63
+ key = _row$childNodes2[0],
64
+ value = _row$childNodes2[1];
65
+
66
+ expect(key.tagName).toEqual('DT');
67
+ expect(key.classList).toContain("".concat(_SummaryList.DEFAULT_CLASS, "__key"));
68
+ expect(value.tagName).toEqual('DD');
69
+ expect(value.classList).toContain("".concat(_SummaryList.DEFAULT_CLASS, "__value"));
70
+ expect(row.childNodes.length).toEqual(2);
71
+ return [key, value];
72
+ };
73
+
57
74
  it('should handle an empty rows array', function () {
58
75
  var ID = 'test-id';
59
76
  var ROWS = [];
@@ -193,5 +210,42 @@ describe('components', function () {
193
210
  expect(ON_ACTION_CALLS[index]).toEqual(row);
194
211
  });
195
212
  });
213
+ it('should handle rows with component values and actions set to hidden', function () {
214
+ var ID = 'test-id';
215
+ var VALUES = ['Alpha component value', 'Bravo component value'];
216
+ var ROWS = [{
217
+ pageId: 'p1',
218
+ fieldId: 'a',
219
+ key: 'Alpha',
220
+ value: /*#__PURE__*/_react2.default.createElement("div", null, VALUES[0])
221
+ }, {
222
+ pageId: 'p2',
223
+ fieldId: 'b',
224
+ key: 'Bravo',
225
+ value: /*#__PURE__*/_react2.default.createElement("div", null, VALUES[1])
226
+ }];
227
+
228
+ var _render5 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_SummaryList.default, {
229
+ "data-testid": ID,
230
+ rows: ROWS,
231
+ noChangeAction: true
232
+ })),
233
+ container = _render5.container;
234
+
235
+ var summaryList = checkSummaryList(container, ID);
236
+ expect(summaryList.childNodes.length).toEqual(ROWS.length);
237
+ ROWS.forEach(function (row, index) {
238
+ var _checkRowNoChangeActi = checkRowNoChangeActions(summaryList, index),
239
+ _checkRowNoChangeActi2 = _slicedToArray(_checkRowNoChangeActi, 2),
240
+ key = _checkRowNoChangeActi2[0],
241
+ value = _checkRowNoChangeActi2[1];
242
+
243
+ expect(key.textContent).toEqual(row.key);
244
+ expect(value.childNodes.length).toEqual(1);
245
+ var valueDiv = value.childNodes[0];
246
+ expect(valueDiv.tagName).toEqual('DIV');
247
+ expect(valueDiv.textContent).toEqual(VALUES[index]);
248
+ });
249
+ });
196
250
  });
197
251
  });
@@ -13,9 +13,9 @@ var getRowActionAttributes = function getRowActionAttributes(row) {
13
13
  return row.action.onAction(row);
14
14
  }
15
15
  };
16
- } else {
16
+ } else if (row.action.page) {
17
17
  return {
18
- href: row.action.href
18
+ href: "/".concat(row.action.page)
19
19
  };
20
20
  }
21
21
  }
@@ -20,15 +20,15 @@ describe('components', function () {
20
20
  };
21
21
  expect((0, _getRowActionAttributes.default)(ROW)).toEqual({});
22
22
  });
23
- it('should handle a row with an href', function () {
24
- var HREF = 'http://alpha.homeoffice.gov.uk';
23
+ it('should handle a row with a page', function () {
24
+ var PAGE = 'alpha';
25
25
  var ROW = {
26
26
  action: {
27
- href: HREF
27
+ page: PAGE
28
28
  }
29
29
  };
30
30
  expect((0, _getRowActionAttributes.default)(ROW)).toEqual({
31
- href: HREF
31
+ href: "/".concat(PAGE)
32
32
  });
33
33
  });
34
34
  it('should handle a row with an onAction function', function () {
@@ -27,6 +27,12 @@ Object.defineProperty(exports, "FormRenderer", {
27
27
  return _FormRenderer.default;
28
28
  }
29
29
  });
30
+ Object.defineProperty(exports, "SummaryList", {
31
+ enumerable: true,
32
+ get: function get() {
33
+ return _SummaryList.default;
34
+ }
35
+ });
30
36
 
31
37
  var _CheckYourAnswers = _interopRequireDefault(require("./CheckYourAnswers"));
32
38
 
@@ -36,4 +42,6 @@ var _FormPage = _interopRequireDefault(require("./FormPage"));
36
42
 
37
43
  var _FormRenderer = _interopRequireDefault(require("./FormRenderer"));
38
44
 
45
+ var _SummaryList = _interopRequireDefault(require("./SummaryList"));
46
+
39
47
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
package/dist/index.js CHANGED
@@ -21,6 +21,12 @@ Object.defineProperty(exports, "HubFormats", {
21
21
  return _models.HubFormats;
22
22
  }
23
23
  });
24
+ Object.defineProperty(exports, "SummaryList", {
25
+ enumerable: true,
26
+ get: function get() {
27
+ return _SummaryList.default;
28
+ }
29
+ });
24
30
  Object.defineProperty(exports, "Utils", {
25
31
  enumerable: true,
26
32
  get: function get() {
@@ -31,6 +37,8 @@ exports.intercepts = exports.default = void 0;
31
37
 
32
38
  var _FormRenderer = _interopRequireDefault(require("./components/FormRenderer"));
33
39
 
40
+ var _SummaryList = _interopRequireDefault(require("./components/SummaryList"));
41
+
34
42
  var _useHooks = require("./hooks/useHooks");
35
43
 
36
44
  var _models = require("./models");
@@ -0,0 +1,7 @@
1
+ {
2
+ "data": [
3
+ { "id": "yes", "name": "Yes" },
4
+ { "id": "no-contractor", "name": "No, I'm a contractor" },
5
+ { "id": "no-agency", "name": "No, I'm agency staff" }
6
+ ]
7
+ }