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

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 (82) hide show
  1. package/README.md +38 -0
  2. package/dist/components/CheckYourAnswers/CheckYourAnswers.js +77 -0
  3. package/dist/components/CheckYourAnswers/CheckYourAnswers.scss +9 -0
  4. package/dist/components/CheckYourAnswers/index.js +13 -0
  5. package/dist/components/FormComponent/FormComponent.js +95 -0
  6. package/dist/components/FormComponent/index.js +13 -0
  7. package/dist/components/FormPage/FormPage.js +121 -0
  8. package/dist/components/FormPage/FormPage.scss +7 -0
  9. package/dist/components/FormPage/index.js +13 -0
  10. package/dist/components/FormRenderer/FormRenderer.js +203 -0
  11. package/dist/components/FormRenderer/FormRenderer.scss +5 -0
  12. package/dist/components/FormRenderer/helpers.js +66 -0
  13. package/dist/components/FormRenderer/helpers.test.js +158 -0
  14. package/dist/components/FormRenderer/index.js +13 -0
  15. package/dist/components/PageActions/PageActions.js +49 -0
  16. package/dist/components/PageActions/index.js +13 -0
  17. package/dist/components/SummaryList/RowAction.js +75 -0
  18. package/dist/components/SummaryList/SummaryList.js +93 -0
  19. package/dist/components/SummaryList/SummaryList.scss +2 -0
  20. package/dist/components/SummaryList/index.js +13 -0
  21. package/dist/components/index.js +31 -0
  22. package/dist/hooks/index.js +39 -0
  23. package/dist/hooks/useAxios.js +80 -0
  24. package/dist/hooks/useGetRequest.js +149 -0
  25. package/dist/hooks/useHooks.js +51 -0
  26. package/dist/hooks/useRefData.js +91 -0
  27. package/dist/index.js +48 -0
  28. package/dist/models/ComponentTypes.js +26 -0
  29. package/dist/models/FormTypes.js +18 -0
  30. package/dist/models/HubFormats.js +12 -0
  31. package/dist/models/index.js +31 -0
  32. package/dist/utils/CheckYourAnswers/getCYAAction.js +22 -0
  33. package/dist/utils/CheckYourAnswers/getCYAAction.test.js +92 -0
  34. package/dist/utils/CheckYourAnswers/getCYARow.js +27 -0
  35. package/dist/utils/CheckYourAnswers/getCYARow.test.js +151 -0
  36. package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +24 -0
  37. package/dist/utils/CheckYourAnswers/getCYARowsForPage.test.js +134 -0
  38. package/dist/utils/CheckYourAnswers/index.js +17 -0
  39. package/dist/utils/CheckYourAnswers/showComponentCYA.js +37 -0
  40. package/dist/utils/CheckYourAnswers/showComponentCYA.test.js +54 -0
  41. package/dist/utils/Component/cleanAttributes.js +55 -0
  42. package/dist/utils/Component/cleanAttributes.test.js +69 -0
  43. package/dist/utils/Component/getComponent.js +117 -0
  44. package/dist/utils/Component/getComponent.test.js +329 -0
  45. package/dist/utils/Component/index.js +28 -0
  46. package/dist/utils/Component/isEditable.js +19 -0
  47. package/dist/utils/Component/isEditable.test.js +42 -0
  48. package/dist/utils/Component/showComponent.js +44 -0
  49. package/dist/utils/Component/showComponent.test.js +94 -0
  50. package/dist/utils/Component/wrapInFormGroup.js +23 -0
  51. package/dist/utils/Data/getAutocompleteSource.js +28 -0
  52. package/dist/utils/Data/getAutocompleteSource.test.js +146 -0
  53. package/dist/utils/Data/getOptions.js +21 -0
  54. package/dist/utils/Data/getOptions.test.js +71 -0
  55. package/dist/utils/Data/getSourceData.js +34 -0
  56. package/dist/utils/Data/getSourceData.test.js +125 -0
  57. package/dist/utils/Data/index.js +34 -0
  58. package/dist/utils/Data/refDataToOptions.js +33 -0
  59. package/dist/utils/Data/refDataToOptions.test.js +99 -0
  60. package/dist/utils/Data/setupFormData.js +63 -0
  61. package/dist/utils/Data/setupFormData.test.js +216 -0
  62. package/dist/utils/Data/setupRefDataUrlForComponent.js +29 -0
  63. package/dist/utils/Data/setupRefDataUrlForComponent.test.js +83 -0
  64. package/dist/utils/FormPage/getFormPage.js +45 -0
  65. package/dist/utils/FormPage/getFormPages.js +33 -0
  66. package/dist/utils/FormPage/getParagraphFromText.js +20 -0
  67. package/dist/utils/FormPage/index.js +19 -0
  68. package/dist/utils/FormPage/useComponent.js +31 -0
  69. package/dist/utils/Hub/getFormHub.js +30 -0
  70. package/dist/utils/Hub/index.js +20 -0
  71. package/dist/utils/Validate/index.js +26 -0
  72. package/dist/utils/Validate/validateComponent.js +49 -0
  73. package/dist/utils/Validate/validateComponent.test.js +119 -0
  74. package/dist/utils/Validate/validateEmail.js +39 -0
  75. package/dist/utils/Validate/validateEmail.test.js +57 -0
  76. package/dist/utils/Validate/validatePage.js +35 -0
  77. package/dist/utils/Validate/validatePage.test.js +122 -0
  78. package/dist/utils/Validate/validateRequired.js +38 -0
  79. package/dist/utils/Validate/validateRequired.test.js +50 -0
  80. package/dist/utils/index.js +43 -0
  81. package/dist/utils/meetsCondition.js +79 -0
  82. package/package.json +91 -0
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+
3
+ var _getCYARow = _interopRequireDefault(require("./getCYARow"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ // Local imports
8
+ describe('utils', function () {
9
+ describe('CheckYourAnswers', function () {
10
+ describe('getCYARow', function () {
11
+ var expectObjectLike = function expectObjectLike(received, expected) {
12
+ Object.keys(expected).forEach(function (key) {
13
+ expect(received[key]).toEqual(expected[key]);
14
+ });
15
+ };
16
+
17
+ it('should get an appropriate row for a readonly text component', function () {
18
+ var PAGE = {
19
+ id: 'page',
20
+ formData: {
21
+ a: 'Bravo'
22
+ }
23
+ };
24
+ var COMPONENT = {
25
+ type: 'text',
26
+ readonly: true,
27
+ id: 'a',
28
+ fieldId: 'a',
29
+ label: 'Alpha'
30
+ };
31
+
32
+ var ON_ACTION = function ON_ACTION() {};
33
+
34
+ var ROW = (0, _getCYARow.default)(PAGE, COMPONENT, ON_ACTION);
35
+ expectObjectLike(ROW, {
36
+ pageId: PAGE.id,
37
+ fieldId: COMPONENT.fieldId,
38
+ key: COMPONENT.label,
39
+ value: 'Bravo',
40
+ component: COMPONENT,
41
+ action: null
42
+ });
43
+ });
44
+ it('should get an appropriate row for a readonly text component with no value', function () {
45
+ var PAGE = {
46
+ id: 'page',
47
+ formData: {}
48
+ };
49
+ var COMPONENT = {
50
+ type: 'text',
51
+ readonly: true,
52
+ id: 'a',
53
+ fieldId: 'a',
54
+ label: 'Alpha'
55
+ };
56
+
57
+ var ON_ACTION = function ON_ACTION() {};
58
+
59
+ expect((0, _getCYARow.default)(PAGE, COMPONENT, ON_ACTION)).toEqual({
60
+ pageId: PAGE.id,
61
+ fieldId: COMPONENT.fieldId,
62
+ key: COMPONENT.label,
63
+ value: '',
64
+ component: COMPONENT,
65
+ action: null
66
+ });
67
+ });
68
+ it('should get an appropriate row for a readonly non-editable component', function () {
69
+ var PAGE = {
70
+ id: 'page',
71
+ formData: {
72
+ a: 'BLAH'
73
+ }
74
+ };
75
+ var COMPONENT = {
76
+ type: 'blah',
77
+ readonly: true,
78
+ id: 'a',
79
+ fieldId: 'a',
80
+ label: 'Alpha'
81
+ };
82
+
83
+ var ON_ACTION = function ON_ACTION() {};
84
+
85
+ expect((0, _getCYARow.default)(PAGE, COMPONENT, ON_ACTION)).toEqual({
86
+ pageId: PAGE.id,
87
+ fieldId: COMPONENT.fieldId,
88
+ key: COMPONENT.label,
89
+ value: 'BLAH',
90
+ action: null
91
+ });
92
+ });
93
+ it('should use the cya_label where there is no label', function () {
94
+ var PAGE = {
95
+ id: 'page',
96
+ formData: {
97
+ a: 'Bravo'
98
+ }
99
+ };
100
+ var COMPONENT = {
101
+ type: 'text',
102
+ readonly: true,
103
+ id: 'a',
104
+ fieldId: 'a',
105
+ cya_label: 'CYA Alpha'
106
+ };
107
+
108
+ var ON_ACTION = function ON_ACTION() {};
109
+
110
+ var ROW = (0, _getCYARow.default)(PAGE, COMPONENT, ON_ACTION);
111
+ expectObjectLike(ROW, {
112
+ pageId: PAGE.id,
113
+ fieldId: COMPONENT.fieldId,
114
+ key: COMPONENT.cya_label,
115
+ action: null,
116
+ component: COMPONENT,
117
+ value: 'Bravo'
118
+ });
119
+ });
120
+ it('should get an appropriate row for an editable text component', function () {
121
+ var PAGE = {
122
+ id: 'page',
123
+ formData: {
124
+ a: 'Bravo'
125
+ },
126
+ cya_link: {}
127
+ };
128
+ var COMPONENT = {
129
+ type: 'text',
130
+ id: 'a',
131
+ fieldId: 'a',
132
+ label: 'Alpha'
133
+ };
134
+
135
+ var ON_ACTION = function ON_ACTION() {};
136
+
137
+ var ROW = (0, _getCYARow.default)(PAGE, COMPONENT, ON_ACTION);
138
+ expectObjectLike(ROW, {
139
+ pageId: PAGE.id,
140
+ fieldId: COMPONENT.fieldId,
141
+ key: COMPONENT.label,
142
+ component: COMPONENT,
143
+ value: 'Bravo'
144
+ });
145
+ expectObjectLike(ROW.action, {
146
+ onAction: ON_ACTION
147
+ });
148
+ });
149
+ });
150
+ });
151
+ });
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _getCYARow = _interopRequireDefault(require("./getCYARow"));
9
+
10
+ var _showComponentCYA = _interopRequireDefault(require("./showComponentCYA"));
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ // Local imports
15
+ var getCYARowsForPage = function getCYARowsForPage(page, onAction) {
16
+ return page.components.filter(function (c) {
17
+ return (0, _showComponentCYA.default)(c, page.formData);
18
+ }).map(function (component) {
19
+ return (0, _getCYARow.default)(page, component, onAction);
20
+ });
21
+ };
22
+
23
+ var _default = getCYARowsForPage;
24
+ exports.default = _default;
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+
3
+ var _getCYARowsForPage = _interopRequireDefault(require("./getCYARowsForPage"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ // Local imports
8
+ describe('utils', function () {
9
+ describe('CheckYourAnswers', function () {
10
+ describe('getCYARowsForPage', function () {
11
+ var expectObjectLike = function expectObjectLike(received, expected) {
12
+ Object.keys(expected).forEach(function (key) {
13
+ expect(received[key]).toEqual(expected[key]);
14
+ });
15
+ };
16
+
17
+ it('should get a appropriate row for a page with a single readonly text component', function () {
18
+ var COMPONENT = {
19
+ type: 'text',
20
+ readonly: true,
21
+ id: 'a',
22
+ fieldId: 'a',
23
+ label: 'Alpha'
24
+ };
25
+ var PAGE = {
26
+ id: 'page',
27
+ components: [COMPONENT],
28
+ formData: {
29
+ a: 'Bravo'
30
+ }
31
+ };
32
+
33
+ var ON_ACTION = function ON_ACTION() {};
34
+
35
+ var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION);
36
+ expect(ROWS.length).toEqual(1);
37
+ ROWS.forEach(function (row, index) {
38
+ expectObjectLike(row, {
39
+ pageId: PAGE.id,
40
+ fieldId: PAGE.components[index].fieldId,
41
+ key: PAGE.components[index].label,
42
+ action: null,
43
+ component: COMPONENT,
44
+ value: 'Bravo'
45
+ });
46
+ });
47
+ });
48
+ it('should get appropriate rows for a page with two editable text components', function () {
49
+ var COMPONENT_A = {
50
+ type: 'text',
51
+ id: 'a',
52
+ fieldId: 'a',
53
+ label: 'Alpha'
54
+ };
55
+ var COMPONENT_B = {
56
+ type: 'text',
57
+ id: 'b',
58
+ fieldId: 'b',
59
+ label: 'Bravo'
60
+ };
61
+ var PAGE = {
62
+ id: 'page',
63
+ components: [COMPONENT_A, COMPONENT_B],
64
+ formData: {
65
+ a: 'Alpha Charlie',
66
+ b: 'Bravo Charlie'
67
+ },
68
+ cya_link: {}
69
+ };
70
+
71
+ var ON_ACTION = function ON_ACTION() {};
72
+
73
+ var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION);
74
+ expect(ROWS.length).toEqual(2);
75
+ ROWS.forEach(function (row, index) {
76
+ expectObjectLike(row, {
77
+ pageId: PAGE.id,
78
+ fieldId: PAGE.components[index].fieldId,
79
+ key: PAGE.components[index].label,
80
+ component: PAGE.components[index],
81
+ value: "".concat(PAGE.components[index].label, " Charlie")
82
+ });
83
+ expectObjectLike(row.action, {
84
+ onAction: ON_ACTION
85
+ });
86
+ });
87
+ });
88
+ it("should filter out any components that shouldn't be shown", function () {
89
+ var COMPONENT_A = {
90
+ type: 'text',
91
+ id: 'a',
92
+ fieldId: 'a',
93
+ label: 'Alpha'
94
+ };
95
+ var COMPONENT_B = {
96
+ type: 'text',
97
+ id: 'b',
98
+ fieldId: 'b',
99
+ label: 'Bravo'
100
+ };
101
+ var COMPONENT_C = {
102
+ type: 'heading',
103
+ content: 'Heading component'
104
+ };
105
+ var PAGE = {
106
+ id: 'page',
107
+ components: [COMPONENT_A, COMPONENT_B, COMPONENT_C],
108
+ formData: {
109
+ a: 'Alpha Charlie',
110
+ b: 'Bravo Charlie'
111
+ },
112
+ cya_link: {}
113
+ };
114
+
115
+ var ON_ACTION = function ON_ACTION() {};
116
+
117
+ var ROWS = (0, _getCYARowsForPage.default)(PAGE, ON_ACTION);
118
+ expect(ROWS.length).toEqual(2);
119
+ ROWS.forEach(function (row, index) {
120
+ expectObjectLike(row, {
121
+ pageId: PAGE.id,
122
+ fieldId: PAGE.components[index].fieldId,
123
+ key: PAGE.components[index].label,
124
+ component: PAGE.components[index],
125
+ value: "".concat(PAGE.components[index].label, " Charlie")
126
+ });
127
+ expectObjectLike(row.action, {
128
+ onAction: ON_ACTION
129
+ });
130
+ });
131
+ });
132
+ });
133
+ });
134
+ });
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _getCYARowsForPage = _interopRequireDefault(require("./getCYARowsForPage"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ // Local imports
13
+ var CheckYourAnswers = {
14
+ getRows: _getCYARowsForPage.default
15
+ };
16
+ var _default = CheckYourAnswers;
17
+ exports.default = _default;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _Component = _interopRequireDefault(require("../Component"));
9
+
10
+ var _models = require("../../models");
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ // Local imports
15
+ var EXCLUDE_FROM_CYA = [_models.ComponentTypes.HEADING, _models.ComponentTypes.HTML, _models.ComponentTypes.INSET_TEXT];
16
+ /**
17
+ * Determines whether a given component should display on the
18
+ * Check your answers page.
19
+ * @param {object} options The component options.
20
+ * @param {object} data The data contained on the form.
21
+ * @returns A boolean true, if the component should show; otherwise false.
22
+ */
23
+
24
+ var showComponentCYA = function showComponentCYA(options, data) {
25
+ if (!options) {
26
+ return false;
27
+ }
28
+
29
+ if (EXCLUDE_FROM_CYA.includes(options.type)) {
30
+ return false;
31
+ }
32
+
33
+ return _Component.default.show(options, data);
34
+ };
35
+
36
+ var _default = showComponentCYA;
37
+ exports.default = _default;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ var _models = require("../../models");
4
+
5
+ var _showComponentCYA = _interopRequireDefault(require("./showComponentCYA"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ // Local imports
10
+ describe('utils', function () {
11
+ describe('CheckYourAnswers', function () {
12
+ describe('showComponentCYA', function () {
13
+ it('should not show when there are no options', function () {
14
+ expect((0, _showComponentCYA.default)(null, null)).toBeFalsy();
15
+ });
16
+ it('should not show when it is a heading type', function () {
17
+ expect((0, _showComponentCYA.default)({
18
+ type: _models.ComponentTypes.HEADING
19
+ }, null)).toBeFalsy();
20
+ });
21
+ it('should not show when it is an html type', function () {
22
+ expect((0, _showComponentCYA.default)({
23
+ type: _models.ComponentTypes.HTML
24
+ }, null)).toBeFalsy();
25
+ });
26
+ it('should not show when it is an inset-text type', function () {
27
+ expect((0, _showComponentCYA.default)({
28
+ type: _models.ComponentTypes.INSET_TEXT
29
+ }, null)).toBeFalsy();
30
+ });
31
+ it('should not show when it hidden and disabled', function () {
32
+ expect((0, _showComponentCYA.default)({
33
+ hidden: true,
34
+ disabled: true
35
+ }, null)).toBeFalsy();
36
+ });
37
+ it('should show when it hidden but not disabled', function () {
38
+ expect((0, _showComponentCYA.default)({
39
+ hidden: true
40
+ }, null)).toBeTruthy();
41
+ });
42
+ it('should show when it disabled but not hidden', function () {
43
+ expect((0, _showComponentCYA.default)({
44
+ disabled: true
45
+ }, null)).toBeTruthy();
46
+ });
47
+ it('should show when it not html, inset-text, a heading, hidden, or disabled', function () {
48
+ expect((0, _showComponentCYA.default)({
49
+ type: _models.ComponentTypes.TEXT
50
+ }, null)).toBeTruthy();
51
+ });
52
+ });
53
+ });
54
+ });
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.JSON_ONLY_PROPERTIES = void 0;
7
+
8
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
9
+
10
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
11
+
12
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
13
+
14
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
15
+
16
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
17
+
18
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
19
+
20
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
21
+
22
+ var JSON_ONLY_PROPERTIES = ['source', 'use', 'show_when', 'options'];
23
+ /**
24
+ * This method removes and properties that are entirely specific to the JSON
25
+ * configuration and that should not be used as HTML attributes.
26
+ *
27
+ * This is in place as certain properties being used as attributes result in
28
+ * console errors.
29
+ *
30
+ * @param {object} options The options object that contains attributes and JSON configuration properties.
31
+ * @param {Array} alsoRemove An array of additional properties to remove.
32
+ * @returns A clean(er) JSON object.
33
+ */
34
+
35
+ exports.JSON_ONLY_PROPERTIES = JSON_ONLY_PROPERTIES;
36
+
37
+ var cleanAttributes = function cleanAttributes(options) {
38
+ var alsoRemove = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
39
+ var removeKeys = Array.isArray(alsoRemove) ? [].concat(JSON_ONLY_PROPERTIES, _toConsumableArray(alsoRemove)) : JSON_ONLY_PROPERTIES;
40
+
41
+ if (options && _typeof(options) === 'object') {
42
+ return Object.keys(options).reduce(function (obj, key) {
43
+ if (!removeKeys.includes(key)) {
44
+ obj[key] = options[key];
45
+ }
46
+
47
+ return obj;
48
+ }, {});
49
+ }
50
+
51
+ return {};
52
+ };
53
+
54
+ var _default = cleanAttributes;
55
+ exports.default = _default;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+
3
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
+
5
+ var _cleanAttributes = _interopRequireWildcard(require("./cleanAttributes"));
6
+
7
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
8
+
9
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
10
+
11
+ // Local imports
12
+ describe('utils', function () {
13
+ describe('Component', function () {
14
+ describe('clean', function () {
15
+ it('should handle null options', function () {
16
+ expect((0, _cleanAttributes.default)(null)).toEqual({});
17
+ });
18
+ it('should handle undefined options', function () {
19
+ expect((0, _cleanAttributes.default)(undefined)).toEqual({});
20
+ });
21
+ it('should handle empty options', function () {
22
+ expect((0, _cleanAttributes.default)({}, null)).toEqual({});
23
+ });
24
+ it('should handle a non-object options', function () {
25
+ expect((0, _cleanAttributes.default)(23, null)).toEqual({});
26
+ });
27
+ it('should only remove properties specific to JSON', function () {
28
+ var OPTIONS = {
29
+ id: 'bob'
30
+ }; // Add a bunch of properties we know will need to be removed.
31
+
32
+ _cleanAttributes.JSON_ONLY_PROPERTIES.forEach(function (p) {
33
+ return OPTIONS[p] = 'value';
34
+ });
35
+
36
+ var RESULT = (0, _cleanAttributes.default)(OPTIONS); // The original options should remain untouched.
37
+
38
+ expect(OPTIONS.id).toEqual('bob');
39
+ expect(OPTIONS['source']).toEqual('value'); // But those values should have been removed from the result,
40
+ // which means it should simply have an id of 'bob'.
41
+
42
+ expect(RESULT).toEqual({
43
+ id: 'bob'
44
+ });
45
+ });
46
+ it('should also remove properties specified in the alsoRemove parameter', function () {
47
+ var OPTIONS = {
48
+ id: 'bob',
49
+ fieldId: 'bobField'
50
+ }; // Add a bunch of properties we know will need to be removed.
51
+
52
+ _cleanAttributes.JSON_ONLY_PROPERTIES.forEach(function (p) {
53
+ return OPTIONS[p] = 'value';
54
+ });
55
+
56
+ var RESULT = (0, _cleanAttributes.default)(OPTIONS, ['fieldId']); // The original options should remain untouched.
57
+
58
+ expect(OPTIONS.id).toEqual('bob');
59
+ expect(OPTIONS.fieldId).toEqual('bobField');
60
+ expect(OPTIONS['source']).toEqual('value'); // But those values should have been removed from the result,
61
+ // which means it should simply have an id of 'bob'.
62
+
63
+ expect(RESULT).toEqual({
64
+ id: 'bob'
65
+ });
66
+ });
67
+ });
68
+ });
69
+ });
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _react = _interopRequireDefault(require("react"));
9
+
10
+ var _copReactComponents = require("@ukhomeoffice/cop-react-components");
11
+
12
+ var _cleanAttributes = _interopRequireDefault(require("./cleanAttributes"));
13
+
14
+ var _models = require("../../models");
15
+
16
+ var _Data = _interopRequireDefault(require("../Data"));
17
+
18
+ var _isEditable = _interopRequireDefault(require("./isEditable"));
19
+
20
+ var _wrapInFormGroup = _interopRequireDefault(require("./wrapInFormGroup"));
21
+
22
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
+
24
+ function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
25
+
26
+ /**
27
+ * Separate function for each component type for the sake of
28
+ * code clarity - having the additional bits and pieces in the
29
+ * switch statement increases the cyclomatic complexity and
30
+ * makes it much harder to follow what's going on.
31
+ */
32
+ var getAutocomplete = function getAutocomplete(config) {
33
+ var attrs = (0, _cleanAttributes.default)(config);
34
+
35
+ var source = _Data.default.getAutocompleteSource(config);
36
+
37
+ return /*#__PURE__*/_react.default.createElement(_copReactComponents.Autocomplete, _extends({}, attrs, {
38
+ source: source
39
+ }));
40
+ };
41
+
42
+ var getHeading = function getHeading(config) {
43
+ var attrs = (0, _cleanAttributes.default)(config);
44
+ return /*#__PURE__*/_react.default.createElement(_copReactComponents.Heading, attrs, config.content);
45
+ };
46
+
47
+ var getHTML = function getHTML(config) {
48
+ var attrs = (0, _cleanAttributes.default)(config);
49
+ return /*#__PURE__*/_react.default.createElement(_copReactComponents.Markup, attrs);
50
+ };
51
+
52
+ var getInsetText = function getInsetText(config) {
53
+ var attrs = (0, _cleanAttributes.default)(config);
54
+ return /*#__PURE__*/_react.default.createElement(_copReactComponents.InsetText, attrs, config.content);
55
+ };
56
+
57
+ var getRadios = function getRadios(config) {
58
+ var options = [];
59
+
60
+ _Data.default.getOptions(config, function (val) {
61
+ options = val;
62
+ });
63
+
64
+ var attrs = (0, _cleanAttributes.default)(config);
65
+ return /*#__PURE__*/_react.default.createElement(_copReactComponents.Radios, _extends({}, attrs, {
66
+ options: options
67
+ }));
68
+ };
69
+
70
+ var getTextInput = function getTextInput(config) {
71
+ var attrs = (0, _cleanAttributes.default)(config);
72
+ return /*#__PURE__*/_react.default.createElement(_copReactComponents.TextInput, attrs);
73
+ };
74
+
75
+ var getComponentByType = function getComponentByType(config) {
76
+ switch (config.type) {
77
+ case _models.ComponentTypes.HTML:
78
+ return getHTML(config);
79
+
80
+ case _models.ComponentTypes.HEADING:
81
+ return getHeading(config);
82
+
83
+ case _models.ComponentTypes.INSET_TEXT:
84
+ return getInsetText(config);
85
+
86
+ case _models.ComponentTypes.TEXT:
87
+ case _models.ComponentTypes.EMAIL:
88
+ case _models.ComponentTypes.PHONE_NUMBER:
89
+ return getTextInput(config);
90
+
91
+ case _models.ComponentTypes.AUTOCOMPLETE:
92
+ return getAutocomplete(config);
93
+
94
+ case _models.ComponentTypes.RADIOS:
95
+ return getRadios(config);
96
+
97
+ default:
98
+ {
99
+ return null;
100
+ }
101
+ }
102
+ };
103
+
104
+ var getComponent = function getComponent(config) {
105
+ var noWrap = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
106
+ var component = getComponentByType(config);
107
+
108
+ if (component && !noWrap && (0, _isEditable.default)(config)) {
109
+ var attrs = (0, _cleanAttributes.default)(config, ['fieldId', 'displayMenu']);
110
+ return (0, _wrapInFormGroup.default)(attrs, component);
111
+ }
112
+
113
+ return component;
114
+ };
115
+
116
+ var _default = getComponent;
117
+ exports.default = _default;