@ukhomeoffice/cop-react-form-renderer 0.1.0-beta → 0.1.0-gamma

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 (48) hide show
  1. package/dist/components/CheckYourAnswers/Answer.js +48 -0
  2. package/dist/components/CheckYourAnswers/CheckYourAnswers.js +28 -9
  3. package/dist/components/CheckYourAnswers/CheckYourAnswers.stories.mdx +23 -0
  4. package/dist/components/FormComponent/FormComponent.js +7 -7
  5. package/dist/components/FormComponent/FormComponent.stories.mdx +167 -0
  6. package/dist/components/FormComponent/FormComponent.test.js +137 -0
  7. package/dist/components/FormPage/FormPage.js +12 -11
  8. package/dist/components/FormPage/FormPage.stories.mdx +118 -0
  9. package/dist/components/FormPage/FormPage.test.js +253 -0
  10. package/dist/components/FormRenderer/FormRenderer.js +33 -18
  11. package/dist/components/FormRenderer/FormRenderer.stories.mdx +87 -0
  12. package/dist/components/FormRenderer/helpers/canActionProceed.js +35 -0
  13. package/dist/components/FormRenderer/helpers/canActionProceed.test.js +73 -0
  14. package/dist/components/FormRenderer/helpers/getCYA.js +31 -0
  15. package/dist/components/FormRenderer/helpers/getCYA.test.js +38 -0
  16. package/dist/components/FormRenderer/helpers/getFormState.js +32 -0
  17. package/dist/components/FormRenderer/helpers/getFormState.test.js +61 -0
  18. package/dist/components/FormRenderer/helpers/getPage.js +34 -0
  19. package/dist/components/FormRenderer/helpers/getPage.test.js +46 -0
  20. package/dist/components/FormRenderer/helpers/index.js +23 -0
  21. package/dist/components/PageActions/ActionButton.js +62 -0
  22. package/dist/components/PageActions/ActionButton.test.js +114 -0
  23. package/dist/components/PageActions/PageActions.js +18 -25
  24. package/dist/components/PageActions/PageActions.stories.mdx +74 -0
  25. package/dist/components/PageActions/PageActions.test.js +155 -0
  26. package/dist/components/SummaryList/RowAction.js +13 -22
  27. package/dist/components/SummaryList/RowAction.test.js +104 -0
  28. package/dist/components/SummaryList/SummaryList.js +3 -17
  29. package/dist/components/SummaryList/helpers/getRowActionAttributes.js +27 -0
  30. package/dist/components/SummaryList/helpers/getRowActionAttributes.test.js +77 -0
  31. package/dist/components/SummaryList/helpers/index.js +15 -0
  32. package/dist/components/index.js +8 -0
  33. package/dist/hooks/useGetRequest.js +16 -1
  34. package/dist/hooks/useHooks.js +15 -1
  35. package/dist/utils/Component/getComponent.js +2 -2
  36. package/dist/utils/Data/setupFormData.js +3 -3
  37. package/dist/utils/FormPage/getFormPage.js +11 -0
  38. package/dist/utils/FormPage/getFormPage.test.js +178 -0
  39. package/dist/utils/FormPage/getFormPages.js +11 -6
  40. package/dist/utils/FormPage/getFormPages.test.js +95 -0
  41. package/dist/utils/FormPage/getParagraphFromText.js +6 -0
  42. package/dist/utils/FormPage/getParagraphFromText.test.js +29 -0
  43. package/dist/utils/FormPage/useComponent.js +9 -2
  44. package/dist/utils/FormPage/useComponent.test.js +82 -0
  45. package/dist/utils/Hub/getFormHub.js +4 -4
  46. package/dist/utils/Hub/getFormHub.test.js +96 -0
  47. package/dist/utils/Hub/index.js +0 -3
  48. package/package.json +1 -1
@@ -15,18 +15,23 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
15
15
 
16
16
  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; }
17
17
 
18
+ /**
19
+ * Converts pages objects defined in the JSON to page objects for rendering.
20
+ * @param {Array} pages The JSON pages.
21
+ * @param {Array} components The components defined at the top-level of the form.
22
+ * @param {object} formData The top-level form data, used for setting up components.
23
+ * @returns An array of page objects for rendering.
24
+ */
18
25
  var getFormPages = function getFormPages(pages, components, formData) {
19
- var formPages = [];
20
-
21
26
  if (Array.isArray(pages)) {
22
- pages.forEach(function (options, index) {
23
- formPages.push(_objectSpread(_objectSpread({}, (0, _getFormPage.default)(options, components, formData)), {}, {
27
+ return pages.map(function (options, index) {
28
+ return _objectSpread(_objectSpread({}, (0, _getFormPage.default)(options, components, formData)), {}, {
24
29
  index: index
25
- }));
30
+ });
26
31
  });
27
32
  }
28
33
 
29
- return formPages;
34
+ return [];
30
35
  };
31
36
 
32
37
  var _default = getFormPages;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+
3
+ var _getFormPages = _interopRequireDefault(require("./getFormPages"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
8
+
9
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
10
+
11
+ 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; }
12
+
13
+ describe('utils', function () {
14
+ describe('FormPage', function () {
15
+ describe('getFormPages', function () {
16
+ var FORM_COMPONENTS = [{
17
+ id: 'a',
18
+ fieldId: 'a',
19
+ label: 'Alpha',
20
+ type: 'text'
21
+ }, {
22
+ id: 'b',
23
+ fieldId: 'b',
24
+ label: 'Bravo',
25
+ type: 'text'
26
+ }, // eslint-disable-next-line no-template-curly-in-string
27
+ {
28
+ id: 'c',
29
+ fieldId: 'c',
30
+ label: 'Charlie',
31
+ type: 'radios',
32
+ data: {
33
+ url: '${environmentContext.refData}/v3/charlies'
34
+ }
35
+ }];
36
+ var FORM_DATA = {
37
+ environmentContext: {
38
+ refData: 'https://ho.gov.uk/ref-data/'
39
+ }
40
+ };
41
+ it('should handle null or undefined pageOptions', function () {
42
+ expect((0, _getFormPages.default)(null, FORM_COMPONENTS, FORM_DATA)).toEqual([]);
43
+ expect((0, _getFormPages.default)(undefined, FORM_COMPONENTS, FORM_DATA)).toEqual([]);
44
+ });
45
+ it('should appropriately set up multiples pages with various configurations', function () {
46
+ var PAGE_1 = {
47
+ title: 'Page 1',
48
+ components: [{
49
+ type: 'heading',
50
+ size: 'l',
51
+ content: 'Page heading'
52
+ }]
53
+ };
54
+ var PAGE_2 = {
55
+ title: 'Page 2',
56
+ components: ["Opening paragraph", {
57
+ type: 'heading',
58
+ size: 'l',
59
+ content: 'Page heading'
60
+ }, "Closing paragraph", {
61
+ use: 'c'
62
+ }]
63
+ };
64
+ var PAGES = [PAGE_1, PAGE_2];
65
+ var C = FORM_COMPONENTS[2];
66
+ expect((0, _getFormPages.default)(PAGES, FORM_COMPONENTS, FORM_DATA)).toEqual([{
67
+ index: 0,
68
+ title: PAGE_1.title,
69
+ components: PAGE_1.components,
70
+ formData: FORM_DATA
71
+ }, {
72
+ index: 1,
73
+ title: PAGE_2.title,
74
+ components: [{
75
+ type: 'html',
76
+ tagName: 'p',
77
+ content: PAGE_2.components[0]
78
+ }, PAGE_2.components[1], {
79
+ type: 'html',
80
+ tagName: 'p',
81
+ content: PAGE_2.components[2]
82
+ }, _objectSpread(_objectSpread({
83
+ use: 'c'
84
+ }, C), {}, {
85
+ cya_label: C.label,
86
+ data: {
87
+ url: "".concat(FORM_DATA.environmentContext.refData, "/v3/charlies")
88
+ }
89
+ })],
90
+ formData: FORM_DATA
91
+ }]);
92
+ });
93
+ });
94
+ });
95
+ });
@@ -8,6 +8,12 @@ exports.default = void 0;
8
8
  var _models = require("../../models");
9
9
 
10
10
  // Local imports
11
+
12
+ /**
13
+ * Converts simple text into a paragraph component.
14
+ * @param {string} content The content to render as a paragraph.
15
+ * @returns An HTML component configuration for a paragraph of the supplied tect.
16
+ */
11
17
  var getParagraphFromText = function getParagraphFromText(content) {
12
18
  return {
13
19
  type: _models.ComponentTypes.HTML,
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ var _models = require("../../models");
4
+
5
+ var _getParagraphFromText = _interopRequireDefault(require("./getParagraphFromText"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ // Local imports
10
+ describe('utils', function () {
11
+ describe('FormPage', function () {
12
+ describe('getParagraphFromText', function () {
13
+ it('should handle text content', function () {
14
+ var CONTENT = 'Appropriate text content';
15
+ expect((0, _getParagraphFromText.default)(CONTENT)).toEqual({
16
+ type: _models.ComponentTypes.HTML,
17
+ tagName: 'p',
18
+ content: CONTENT
19
+ });
20
+ });
21
+ it('should handle undefined content', function () {
22
+ expect((0, _getParagraphFromText.default)(undefined)).toEqual({
23
+ type: _models.ComponentTypes.HTML,
24
+ tagName: 'p'
25
+ });
26
+ });
27
+ });
28
+ });
29
+ });
@@ -11,10 +11,17 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
11
11
 
12
12
  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; }
13
13
 
14
+ /**
15
+ * Use an existing component from the formComponents, overriding any properties
16
+ * where appropriate.
17
+ * @param {object} toUse A configuration object that references the component to use and any overrides.
18
+ * @param {Array} formComponents An array of existing components on the form.
19
+ * @returns A component configuration object.
20
+ */
14
21
  var useComponent = function useComponent(toUse, formComponents) {
15
- var formComponent = formComponents.find(function (fc) {
22
+ var formComponent = toUse ? formComponents.find(function (fc) {
16
23
  return fc.id === toUse.use;
17
- });
24
+ }) : undefined;
18
25
 
19
26
  if (formComponent) {
20
27
  var fieldId = toUse.fieldId || formComponent.fieldId;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+
3
+ var _useComponent = _interopRequireDefault(require("./useComponent"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
8
+
9
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
10
+
11
+ 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; }
12
+
13
+ describe('utils', function () {
14
+ describe('FormPage', function () {
15
+ describe('useComponent', function () {
16
+ var FORM_COMPONENTS = [{
17
+ id: 'a',
18
+ fieldId: 'a',
19
+ label: 'Alpha',
20
+ type: 'text'
21
+ }, {
22
+ id: 'b',
23
+ fieldId: 'b',
24
+ label: 'Bravo',
25
+ type: 'text'
26
+ }, {
27
+ id: 'c',
28
+ fieldId: 'c',
29
+ label: 'Charlie',
30
+ type: 'text'
31
+ }];
32
+ it('should handle a null toUse configuration', function () {
33
+ var TO_USE = null;
34
+ expect((0, _useComponent.default)(TO_USE, FORM_COMPONENTS)).toEqual({});
35
+ });
36
+ it('should handle an unrecognised component reference', function () {
37
+ var TO_USE = {
38
+ use: 'd',
39
+ label: 'Delta'
40
+ };
41
+ expect((0, _useComponent.default)(TO_USE, FORM_COMPONENTS)).toEqual(TO_USE);
42
+ });
43
+ it('should use the appropriate component', function () {
44
+ var TO_USE = {
45
+ use: 'a'
46
+ };
47
+ var A = FORM_COMPONENTS[0];
48
+ expect((0, _useComponent.default)(TO_USE, FORM_COMPONENTS)).toEqual(_objectSpread(_objectSpread({
49
+ use: 'a'
50
+ }, A), {}, {
51
+ cya_label: A.label
52
+ }));
53
+ });
54
+ it('should allow the fieldId to be overridden', function () {
55
+ var TO_USE = {
56
+ use: 'a',
57
+ fieldId: 'alpha'
58
+ };
59
+ var A = FORM_COMPONENTS[0];
60
+ expect((0, _useComponent.default)(TO_USE, FORM_COMPONENTS)).toEqual(_objectSpread(_objectSpread({
61
+ use: 'a'
62
+ }, A), {}, {
63
+ cya_label: A.label,
64
+ fieldId: TO_USE.fieldId
65
+ }));
66
+ });
67
+ it('should allow the label to be overridden but keep the cya_label unaffected', function () {
68
+ var TO_USE = {
69
+ use: 'a',
70
+ label: 'Alpha foxtrot'
71
+ };
72
+ var A = FORM_COMPONENTS[0];
73
+ expect((0, _useComponent.default)(TO_USE, FORM_COMPONENTS)).toEqual(_objectSpread(_objectSpread({
74
+ use: 'a'
75
+ }, A), {}, {
76
+ cya_label: A.label,
77
+ label: TO_USE.label
78
+ }));
79
+ });
80
+ });
81
+ });
82
+ });
@@ -5,21 +5,21 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _FormPage = _interopRequireDefault(require("../FormPage"));
9
-
10
8
  var _models = require("../../models");
11
9
 
10
+ var _FormPage = _interopRequireDefault(require("../FormPage"));
11
+
12
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
13
 
14
14
  // Local imports
15
- var getFormHub = function getFormHub(type, hub, components) {
15
+ var getFormHub = function getFormHub(type, hub, components, formData) {
16
16
  if (type === _models.FormTypes.HUB && hub) {
17
17
  if (hub.format === _models.HubFormats.CYA) {
18
18
  return _models.HubFormats.CYA;
19
19
  }
20
20
 
21
21
  if (hub.components) {
22
- return _FormPage.default.get(hub, components);
22
+ return _FormPage.default.get(hub, components, formData);
23
23
  }
24
24
  }
25
25
 
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+
3
+ var _models = require("../../models");
4
+
5
+ var _getFormHub = _interopRequireDefault(require("./getFormHub"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
10
+
11
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
12
+
13
+ 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; }
14
+
15
+ describe('utils', function () {
16
+ describe('Hub', function () {
17
+ describe('getFormHub', function () {
18
+ var FORM_COMPONENTS = [{
19
+ id: 'a',
20
+ fieldId: 'a',
21
+ label: 'Alpha',
22
+ type: 'text'
23
+ }, {
24
+ id: 'b',
25
+ fieldId: 'b',
26
+ label: 'Bravo',
27
+ type: 'text'
28
+ }, // eslint-disable-next-line no-template-curly-in-string
29
+ {
30
+ id: 'c',
31
+ fieldId: 'c',
32
+ label: 'Charlie',
33
+ type: 'radios',
34
+ data: {
35
+ url: '${environmentContext.refData}/v3/charlies'
36
+ }
37
+ }];
38
+ var FORM_DATA = {
39
+ environmentContext: {
40
+ refData: 'https://ho.gov.uk/ref-data/'
41
+ }
42
+ };
43
+ [_models.FormTypes.CYA, _models.FormTypes.FORM, _models.FormTypes.WIZARD].forEach(function (formType) {
44
+ describe("when the FormType is \"".concat(formType, "\""), function () {
45
+ it('should return undefined', function () {
46
+ expect((0, _getFormHub.default)(formType, {}, FORM_COMPONENTS, FORM_DATA)).toBeUndefined();
47
+ });
48
+ });
49
+ });
50
+ describe("when the FormType is \"".concat(_models.FormTypes.HUB, "\""), function () {
51
+ it('should return undefined when there is no hub', function () {
52
+ expect((0, _getFormHub.default)(_models.FormTypes.HUB, null, FORM_COMPONENTS, FORM_DATA)).toBeUndefined();
53
+ });
54
+ it("should return undefined when the format is not \"".concat(_models.HubFormats.CYA, "\" but hub also has no components"), function () {
55
+ expect((0, _getFormHub.default)(_models.FormTypes.HUB, {}, FORM_COMPONENTS, FORM_DATA)).toBeUndefined();
56
+ });
57
+ it("should return the string \"".concat(_models.HubFormats.CYA, "\" when the format is set to \"").concat(_models.HubFormats.CYA, "\""), function () {
58
+ var HUB = {
59
+ format: _models.HubFormats.CYA
60
+ };
61
+ expect((0, _getFormHub.default)(_models.FormTypes.HUB, HUB, FORM_COMPONENTS, FORM_DATA)).toEqual(_models.HubFormats.CYA);
62
+ });
63
+ it("should return an appropriate page when the format is not \"".concat(_models.HubFormats.CYA, "\" and hub contains components"), function () {
64
+ var HUB = {
65
+ title: 'Hub title',
66
+ components: ["Opening paragraph", {
67
+ type: 'heading',
68
+ size: 'l',
69
+ content: 'Hub heading'
70
+ }, "Closing paragraph", {
71
+ use: 'a'
72
+ }]
73
+ };
74
+ var A = FORM_COMPONENTS[0];
75
+ expect((0, _getFormHub.default)(_models.FormTypes.HUB, HUB, FORM_COMPONENTS, FORM_DATA)).toEqual({
76
+ title: HUB.title,
77
+ components: [{
78
+ type: 'html',
79
+ tagName: 'p',
80
+ content: HUB.components[0]
81
+ }, HUB.components[1], {
82
+ type: 'html',
83
+ tagName: 'p',
84
+ content: HUB.components[2]
85
+ }, _objectSpread(_objectSpread({
86
+ use: 'a'
87
+ }, A), {}, {
88
+ cya_label: A.label
89
+ })],
90
+ formData: FORM_DATA
91
+ });
92
+ });
93
+ });
94
+ });
95
+ });
96
+ });
@@ -7,13 +7,10 @@ exports.default = void 0;
7
7
 
8
8
  var _getFormHub = _interopRequireDefault(require("./getFormHub"));
9
9
 
10
- var _models = require("../../models");
11
-
12
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
11
 
14
12
  // Local imports
15
13
  var Hub = {
16
- formats: _models.HubFormats,
17
14
  get: _getFormHub.default
18
15
  };
19
16
  var _default = Hub;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "0.1.0-beta",
3
+ "version": "0.1.0-gamma",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",