@ukhomeoffice/cop-react-form-renderer 0.1.0-beta → 1.0.0-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.
- package/dist/components/CheckYourAnswers/Answer.js +48 -0
- package/dist/components/CheckYourAnswers/Answer.test.js +94 -0
- package/dist/components/CheckYourAnswers/CheckYourAnswers.js +64 -19
- package/dist/components/CheckYourAnswers/CheckYourAnswers.scss +0 -7
- package/dist/components/CheckYourAnswers/CheckYourAnswers.stories.mdx +100 -0
- package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +317 -0
- package/dist/components/FormComponent/FormComponent.js +7 -7
- package/dist/components/FormComponent/FormComponent.stories.mdx +167 -0
- package/dist/components/FormComponent/FormComponent.test.js +137 -0
- package/dist/components/FormPage/FormPage.js +12 -11
- package/dist/components/FormPage/FormPage.stories.mdx +118 -0
- package/dist/components/FormPage/FormPage.test.js +255 -0
- package/dist/components/FormRenderer/FormRenderer.js +70 -58
- package/dist/components/FormRenderer/FormRenderer.stories.mdx +90 -0
- package/dist/components/FormRenderer/FormRenderer.test.js +469 -0
- package/dist/components/FormRenderer/handlers/cyaAction.js +28 -0
- package/dist/components/FormRenderer/handlers/getPageId.js +28 -0
- package/dist/components/FormRenderer/handlers/getPageId.test.js +61 -0
- package/dist/components/FormRenderer/handlers/handlers.test.js +97 -0
- package/dist/components/FormRenderer/handlers/index.js +23 -0
- package/dist/components/FormRenderer/handlers/navigate.js +28 -0
- package/dist/components/FormRenderer/handlers/submissionError.js +20 -0
- package/dist/components/FormRenderer/helpers/canActionProceed.js +35 -0
- package/dist/components/FormRenderer/helpers/canActionProceed.test.js +73 -0
- package/dist/components/FormRenderer/helpers/canCYASubmit.js +42 -0
- package/dist/components/FormRenderer/helpers/canCYASubmit.test.js +155 -0
- package/dist/components/FormRenderer/helpers/getCYA.js +31 -0
- package/dist/components/FormRenderer/helpers/getCYA.test.js +38 -0
- package/dist/components/FormRenderer/helpers/getFormState.js +32 -0
- package/dist/components/FormRenderer/helpers/getFormState.test.js +61 -0
- package/dist/components/FormRenderer/helpers/getNextPageId.js +65 -0
- package/dist/components/FormRenderer/helpers/getNextPageId.test.js +90 -0
- package/dist/components/FormRenderer/helpers/getPage.js +34 -0
- package/dist/components/FormRenderer/helpers/getPage.test.js +46 -0
- package/dist/components/FormRenderer/helpers/index.js +27 -0
- package/dist/components/PageActions/ActionButton.js +58 -0
- package/dist/components/PageActions/ActionButton.test.js +116 -0
- package/dist/components/PageActions/PageActions.js +18 -25
- package/dist/components/PageActions/PageActions.stories.mdx +74 -0
- package/dist/components/PageActions/PageActions.test.js +157 -0
- package/dist/components/SummaryList/RowAction.js +13 -22
- package/dist/components/SummaryList/RowAction.test.js +94 -0
- package/dist/components/SummaryList/SummaryList.js +6 -20
- package/dist/components/SummaryList/SummaryList.scss +7 -0
- package/dist/components/SummaryList/SummaryList.stories.mdx +36 -0
- package/dist/components/SummaryList/SummaryList.test.js +197 -0
- package/dist/components/SummaryList/helpers/getRowActionAttributes.js +27 -0
- package/dist/components/SummaryList/helpers/getRowActionAttributes.test.js +77 -0
- package/dist/components/SummaryList/helpers/index.js +15 -0
- package/dist/components/index.js +8 -0
- package/dist/hooks/useGetRequest.js +16 -1
- package/dist/hooks/useHooks.js +16 -5
- package/dist/index.js +3 -1
- package/dist/models/EventTypes.js +14 -0
- package/dist/models/FormPages.js +14 -0
- package/dist/models/PageAction.js +40 -0
- package/dist/models/index.js +24 -0
- package/dist/utils/CheckYourAnswers/getCYAAction.js +9 -0
- package/dist/utils/CheckYourAnswers/getCYARow.js +10 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +9 -0
- package/dist/utils/CheckYourAnswers/showComponentCYA.js +6 -5
- package/dist/utils/Component/getComponent.js +2 -2
- package/dist/utils/Component/getComponent.test.js +1 -1
- package/dist/utils/Data/setupFormData.js +3 -3
- package/dist/utils/Data/setupFormData.test.js +6 -6
- package/dist/utils/Data/setupRefDataUrlForComponent.test.js +4 -4
- package/dist/utils/FormPage/getFormPage.js +12 -1
- package/dist/utils/FormPage/getFormPage.test.js +178 -0
- package/dist/utils/FormPage/getFormPages.js +11 -6
- package/dist/utils/FormPage/getFormPages.test.js +95 -0
- package/dist/utils/FormPage/getParagraphFromText.js +6 -0
- package/dist/utils/FormPage/getParagraphFromText.test.js +29 -0
- package/dist/utils/FormPage/useComponent.js +9 -2
- package/dist/utils/FormPage/useComponent.test.js +82 -0
- package/dist/utils/Format/formatData.js +32 -0
- package/dist/utils/Format/formatData.test.js +46 -0
- package/dist/utils/Format/formatDataForComponent.js +41 -0
- package/dist/utils/Format/formatDataForComponent.test.js +161 -0
- package/dist/utils/Format/formatDataForForm.js +33 -0
- package/dist/utils/Format/formatDataForForm.test.js +78 -0
- package/dist/utils/Format/formatDataForPage.js +37 -0
- package/dist/utils/Format/formatDataForPage.test.js +96 -0
- package/dist/utils/Format/index.js +26 -0
- package/dist/utils/Hub/getFormHub.js +18 -4
- package/dist/utils/Hub/getFormHub.test.js +96 -0
- package/dist/utils/Hub/index.js +0 -3
- package/dist/utils/Validate/validatePage.test.js +1 -1
- package/dist/utils/index.js +3 -0
- package/package.json +13 -9
- package/dist/components/FormRenderer/helpers.js +0 -66
- package/dist/components/FormRenderer/helpers.test.js +0 -158
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _getFormPage = _interopRequireDefault(require("./getFormPage"));
|
|
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('getFormPage', 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: '${urls.refData}/v3/charlies'
|
|
34
|
+
}
|
|
35
|
+
}];
|
|
36
|
+
var FORM_DATA = {
|
|
37
|
+
urls: {
|
|
38
|
+
refData: 'https://ho.gov.uk/ref-data/'
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
it('should handle null or undefined pageOptions', function () {
|
|
42
|
+
expect((0, _getFormPage.default)(null, FORM_COMPONENTS, FORM_DATA)).toBeNull();
|
|
43
|
+
expect((0, _getFormPage.default)(undefined, FORM_COMPONENTS, FORM_DATA)).toBeNull();
|
|
44
|
+
});
|
|
45
|
+
it('should handle a page not using any form components', function () {
|
|
46
|
+
var PAGE = {
|
|
47
|
+
title: 'Page',
|
|
48
|
+
components: [{
|
|
49
|
+
type: 'heading',
|
|
50
|
+
size: 'l',
|
|
51
|
+
content: 'Page heading'
|
|
52
|
+
}]
|
|
53
|
+
};
|
|
54
|
+
expect((0, _getFormPage.default)(PAGE, FORM_COMPONENTS, FORM_DATA)).toEqual({
|
|
55
|
+
title: PAGE.title,
|
|
56
|
+
components: PAGE.components,
|
|
57
|
+
formData: FORM_DATA
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
it('should handle a page with inline strings and convert them to paragraphs', function () {
|
|
61
|
+
var PAGE = {
|
|
62
|
+
title: 'Page',
|
|
63
|
+
components: ["Opening paragraph", {
|
|
64
|
+
type: 'heading',
|
|
65
|
+
size: 'l',
|
|
66
|
+
content: 'Page heading'
|
|
67
|
+
}, "Closing paragraph"]
|
|
68
|
+
};
|
|
69
|
+
expect((0, _getFormPage.default)(PAGE, FORM_COMPONENTS, FORM_DATA)).toEqual({
|
|
70
|
+
title: PAGE.title,
|
|
71
|
+
components: [{
|
|
72
|
+
type: 'html',
|
|
73
|
+
tagName: 'p',
|
|
74
|
+
content: PAGE.components[0]
|
|
75
|
+
}, PAGE.components[1], {
|
|
76
|
+
type: 'html',
|
|
77
|
+
tagName: 'p',
|
|
78
|
+
content: PAGE.components[2]
|
|
79
|
+
}],
|
|
80
|
+
formData: FORM_DATA
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
it('should handle a page that references a form-level component', function () {
|
|
84
|
+
var PAGE = {
|
|
85
|
+
title: 'Page',
|
|
86
|
+
components: ["Opening paragraph", {
|
|
87
|
+
type: 'heading',
|
|
88
|
+
size: 'l',
|
|
89
|
+
content: 'Page heading'
|
|
90
|
+
}, "Closing paragraph", {
|
|
91
|
+
use: 'a'
|
|
92
|
+
}]
|
|
93
|
+
};
|
|
94
|
+
var A = FORM_COMPONENTS[0];
|
|
95
|
+
expect((0, _getFormPage.default)(PAGE, FORM_COMPONENTS, FORM_DATA)).toEqual({
|
|
96
|
+
title: PAGE.title,
|
|
97
|
+
components: [{
|
|
98
|
+
type: 'html',
|
|
99
|
+
tagName: 'p',
|
|
100
|
+
content: PAGE.components[0]
|
|
101
|
+
}, PAGE.components[1], {
|
|
102
|
+
type: 'html',
|
|
103
|
+
tagName: 'p',
|
|
104
|
+
content: PAGE.components[2]
|
|
105
|
+
}, _objectSpread(_objectSpread({
|
|
106
|
+
use: 'a'
|
|
107
|
+
}, A), {}, {
|
|
108
|
+
cya_label: A.label
|
|
109
|
+
})],
|
|
110
|
+
formData: FORM_DATA
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
it('should handle a page that references a form-level component with a data url', function () {
|
|
114
|
+
var PAGE = {
|
|
115
|
+
title: 'Page',
|
|
116
|
+
components: ["Opening paragraph", {
|
|
117
|
+
type: 'heading',
|
|
118
|
+
size: 'l',
|
|
119
|
+
content: 'Page heading'
|
|
120
|
+
}, "Closing paragraph", {
|
|
121
|
+
use: 'c'
|
|
122
|
+
}]
|
|
123
|
+
};
|
|
124
|
+
var C = FORM_COMPONENTS[2];
|
|
125
|
+
expect((0, _getFormPage.default)(PAGE, FORM_COMPONENTS, FORM_DATA)).toEqual({
|
|
126
|
+
title: PAGE.title,
|
|
127
|
+
components: [{
|
|
128
|
+
type: 'html',
|
|
129
|
+
tagName: 'p',
|
|
130
|
+
content: PAGE.components[0]
|
|
131
|
+
}, PAGE.components[1], {
|
|
132
|
+
type: 'html',
|
|
133
|
+
tagName: 'p',
|
|
134
|
+
content: PAGE.components[2]
|
|
135
|
+
}, _objectSpread(_objectSpread({
|
|
136
|
+
use: 'c'
|
|
137
|
+
}, C), {}, {
|
|
138
|
+
cya_label: C.label,
|
|
139
|
+
data: {
|
|
140
|
+
url: "".concat(FORM_DATA.urls.refData, "/v3/charlies")
|
|
141
|
+
}
|
|
142
|
+
})],
|
|
143
|
+
formData: FORM_DATA
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
it('should handle a page that references a form-level component with a data url without an environment context', function () {
|
|
147
|
+
var PAGE = {
|
|
148
|
+
title: 'Page',
|
|
149
|
+
components: ["Opening paragraph", {
|
|
150
|
+
type: 'heading',
|
|
151
|
+
size: 'l',
|
|
152
|
+
content: 'Page heading'
|
|
153
|
+
}, "Closing paragraph", {
|
|
154
|
+
use: 'c'
|
|
155
|
+
}]
|
|
156
|
+
};
|
|
157
|
+
var C = FORM_COMPONENTS[2];
|
|
158
|
+
expect((0, _getFormPage.default)(PAGE, FORM_COMPONENTS, {})).toEqual({
|
|
159
|
+
title: PAGE.title,
|
|
160
|
+
components: [{
|
|
161
|
+
type: 'html',
|
|
162
|
+
tagName: 'p',
|
|
163
|
+
content: PAGE.components[0]
|
|
164
|
+
}, PAGE.components[1], {
|
|
165
|
+
type: 'html',
|
|
166
|
+
tagName: 'p',
|
|
167
|
+
content: PAGE.components[2]
|
|
168
|
+
}, _objectSpread(_objectSpread({
|
|
169
|
+
use: 'c'
|
|
170
|
+
}, C), {}, {
|
|
171
|
+
cya_label: C.label
|
|
172
|
+
})],
|
|
173
|
+
formData: {}
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
});
|
|
@@ -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.
|
|
23
|
-
|
|
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
|
|
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: '${urls.refData}/v3/charlies'
|
|
34
|
+
}
|
|
35
|
+
}];
|
|
36
|
+
var FORM_DATA = {
|
|
37
|
+
urls: {
|
|
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.urls.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
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Formats a string according to a format configuration object.
|
|
10
|
+
* @param {object} format The format configuration object.
|
|
11
|
+
* @param {string} value The string value to be formatted.
|
|
12
|
+
* @returns A formatted version of the value.
|
|
13
|
+
*/
|
|
14
|
+
var formatData = function formatData(format, value) {
|
|
15
|
+
if (format && value) {
|
|
16
|
+
switch (format.type) {
|
|
17
|
+
case 'lowercase':
|
|
18
|
+
return value.toLowerCase();
|
|
19
|
+
|
|
20
|
+
case 'uppercase':
|
|
21
|
+
return value.toUpperCase();
|
|
22
|
+
|
|
23
|
+
default:
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return value;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
var _default = formatData;
|
|
32
|
+
exports.default = _default;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _formatData = _interopRequireDefault(require("./formatData"));
|
|
4
|
+
|
|
5
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
6
|
+
|
|
7
|
+
// Local imports
|
|
8
|
+
describe('utils', function () {
|
|
9
|
+
describe('Format', function () {
|
|
10
|
+
describe('formatData', function () {
|
|
11
|
+
it('should handle no format', function () {
|
|
12
|
+
var FORMAT = null;
|
|
13
|
+
var VALUE = 'AlPhA';
|
|
14
|
+
expect((0, _formatData.default)(FORMAT, VALUE)).toEqual(VALUE);
|
|
15
|
+
});
|
|
16
|
+
it('should handle no value', function () {
|
|
17
|
+
var FORMAT = {
|
|
18
|
+
type: 'lowercase'
|
|
19
|
+
};
|
|
20
|
+
var VALUE = null;
|
|
21
|
+
expect((0, _formatData.default)(FORMAT, VALUE)).toBeNull();
|
|
22
|
+
});
|
|
23
|
+
it('should format a string as lowercase', function () {
|
|
24
|
+
var FORMAT = {
|
|
25
|
+
type: 'lowercase'
|
|
26
|
+
};
|
|
27
|
+
var VALUE = 'AlPhA';
|
|
28
|
+
expect((0, _formatData.default)(FORMAT, VALUE)).toEqual('alpha');
|
|
29
|
+
});
|
|
30
|
+
it('should format a string as uppercase', function () {
|
|
31
|
+
var FORMAT = {
|
|
32
|
+
type: 'uppercase'
|
|
33
|
+
};
|
|
34
|
+
var VALUE = 'AlPhA';
|
|
35
|
+
expect((0, _formatData.default)(FORMAT, VALUE)).toEqual('ALPHA');
|
|
36
|
+
});
|
|
37
|
+
it('should leave an unknown format type alone', function () {
|
|
38
|
+
var FORMAT = {
|
|
39
|
+
type: 'not-a-real-type'
|
|
40
|
+
};
|
|
41
|
+
var VALUE = 'AlPhA';
|
|
42
|
+
expect((0, _formatData.default)(FORMAT, VALUE)).toEqual(VALUE);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.formatDataForComponents = exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _formatData = _interopRequireDefault(require("./formatData"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
// Local imports
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Formats the data for a given component, according to the component's format configuration.
|
|
16
|
+
* @param {object} component The component to format.
|
|
17
|
+
* @param {object} data The top-level form data.
|
|
18
|
+
* @param {string} eventType What type of event initiated this call.
|
|
19
|
+
*/
|
|
20
|
+
var formatDataForComponent = function formatDataForComponent(component, data, eventType) {
|
|
21
|
+
if (component.format && component.format.on === eventType) {
|
|
22
|
+
data[component.fieldId] = (0, _formatData.default)(component.format, data[component.fieldId]);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Formats the data for an array of components, according to each component's format configuration.
|
|
27
|
+
* @param {Array} components The array of components to format.
|
|
28
|
+
* @param {object} data The top-level form data.
|
|
29
|
+
* @param {string} eventType What type of event initiated this call.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
var formatDataForComponents = function formatDataForComponents(components, data, eventType) {
|
|
34
|
+
components.forEach(function (component) {
|
|
35
|
+
return formatDataForComponent(component, data, eventType);
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
exports.formatDataForComponents = formatDataForComponents;
|
|
40
|
+
var _default = formatDataForComponent;
|
|
41
|
+
exports.default = _default;
|