@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.
- package/dist/components/CheckYourAnswers/Answer.js +48 -0
- package/dist/components/CheckYourAnswers/CheckYourAnswers.js +28 -9
- package/dist/components/CheckYourAnswers/CheckYourAnswers.stories.mdx +23 -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 +253 -0
- package/dist/components/FormRenderer/FormRenderer.js +33 -18
- package/dist/components/FormRenderer/FormRenderer.stories.mdx +87 -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/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/getPage.js +34 -0
- package/dist/components/FormRenderer/helpers/getPage.test.js +46 -0
- package/dist/components/FormRenderer/helpers/index.js +23 -0
- package/dist/components/PageActions/ActionButton.js +62 -0
- package/dist/components/PageActions/ActionButton.test.js +114 -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 +155 -0
- package/dist/components/SummaryList/RowAction.js +13 -22
- package/dist/components/SummaryList/RowAction.test.js +104 -0
- package/dist/components/SummaryList/SummaryList.js +3 -17
- 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 +15 -1
- package/dist/utils/Component/getComponent.js +2 -2
- package/dist/utils/Data/setupFormData.js +3 -3
- package/dist/utils/FormPage/getFormPage.js +11 -0
- 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/Hub/getFormHub.js +4 -4
- package/dist/utils/Hub/getFormHub.test.js +96 -0
- package/dist/utils/Hub/index.js +0 -3
- package/package.json +1 -1
|
@@ -7,18 +7,20 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
});
|
|
8
8
|
exports.default = void 0;
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _copReactComponents = require("@ukhomeoffice/cop-react-components");
|
|
11
11
|
|
|
12
12
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
13
13
|
|
|
14
|
-
var
|
|
14
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
var _helpers = require("./helpers");
|
|
17
17
|
|
|
18
18
|
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); }
|
|
19
19
|
|
|
20
20
|
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; }
|
|
21
21
|
|
|
22
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
|
+
|
|
22
24
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
23
25
|
|
|
24
26
|
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
@@ -40,32 +42,21 @@ var RowAction = function RowAction(_ref) {
|
|
|
40
42
|
setAttrs = _useState2[1];
|
|
41
43
|
|
|
42
44
|
(0, _react.useEffect)(function () {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (row.action) {
|
|
46
|
-
if (typeof row.action.onAction === 'function') {
|
|
47
|
-
value = {
|
|
48
|
-
onClick: function onClick() {
|
|
49
|
-
return row.action.onAction(row);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
} else {
|
|
53
|
-
value = {
|
|
54
|
-
href: row.action.href
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
setAttrs(value);
|
|
45
|
+
setAttrs((0, _helpers.getRowActionAttributes)(row));
|
|
60
46
|
}, [row, setAttrs]);
|
|
61
|
-
|
|
47
|
+
|
|
48
|
+
if (!(row !== null && row !== void 0 && row.action)) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return /*#__PURE__*/_react.default.createElement(_copReactComponents.Link, attrs, row.action.label, row.action.aria_suffix && /*#__PURE__*/_react.default.createElement(_copReactComponents.VisuallyHidden, null, " ", row.action.aria_suffix));
|
|
62
53
|
};
|
|
63
54
|
|
|
64
55
|
RowAction.propTypes = {
|
|
65
56
|
row: _propTypes.default.shape({
|
|
66
57
|
action: _propTypes.default.shape({
|
|
58
|
+
label: _propTypes.default.string.isRequired,
|
|
67
59
|
href: _propTypes.default.string,
|
|
68
|
-
label: _propTypes.default.string,
|
|
69
60
|
aria_suffix: _propTypes.default.string,
|
|
70
61
|
onAction: _propTypes.default.func
|
|
71
62
|
})
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _react = require("@testing-library/react");
|
|
4
|
+
|
|
5
|
+
var _react2 = _interopRequireDefault(require("react"));
|
|
6
|
+
|
|
7
|
+
var _RowAction = _interopRequireDefault(require("./RowAction"));
|
|
8
|
+
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
|
|
11
|
+
// Global imports
|
|
12
|
+
// Local imports
|
|
13
|
+
describe('components', function () {
|
|
14
|
+
describe('SummaryList.RowAction', function () {
|
|
15
|
+
it('should handle a null row', function () {
|
|
16
|
+
var ROW = null;
|
|
17
|
+
|
|
18
|
+
var _render = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_RowAction.default, {
|
|
19
|
+
row: ROW
|
|
20
|
+
})),
|
|
21
|
+
container = _render.container;
|
|
22
|
+
|
|
23
|
+
expect(container.childNodes.length).toEqual(0);
|
|
24
|
+
});
|
|
25
|
+
it('should handle a row without an action', function () {
|
|
26
|
+
var ROW = {};
|
|
27
|
+
|
|
28
|
+
var _render2 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_RowAction.default, {
|
|
29
|
+
row: ROW
|
|
30
|
+
})),
|
|
31
|
+
container = _render2.container;
|
|
32
|
+
|
|
33
|
+
expect(container.childNodes.length).toEqual(0);
|
|
34
|
+
});
|
|
35
|
+
it('should handle a row with an href in the action', function () {
|
|
36
|
+
var HREF = 'http://alpha.homeoffice.gov.uk';
|
|
37
|
+
var ROW = {
|
|
38
|
+
action: {
|
|
39
|
+
href: HREF,
|
|
40
|
+
label: 'Change'
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
var _render3 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_RowAction.default, {
|
|
45
|
+
row: ROW
|
|
46
|
+
})),
|
|
47
|
+
container = _render3.container;
|
|
48
|
+
|
|
49
|
+
var link = container.childNodes[0];
|
|
50
|
+
expect(link.tagName).toEqual('A');
|
|
51
|
+
expect(link.getAttribute('href')).toEqual(HREF);
|
|
52
|
+
expect(link.textContent).toEqual(ROW.action.label);
|
|
53
|
+
});
|
|
54
|
+
it('should render an aria_suffix appropriately', function () {
|
|
55
|
+
var HREF = 'http://alpha.homeoffice.gov.uk';
|
|
56
|
+
var ROW = {
|
|
57
|
+
action: {
|
|
58
|
+
href: HREF,
|
|
59
|
+
label: 'Change',
|
|
60
|
+
aria_suffix: 'the thing'
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
var _render4 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_RowAction.default, {
|
|
65
|
+
row: ROW
|
|
66
|
+
})),
|
|
67
|
+
container = _render4.container;
|
|
68
|
+
|
|
69
|
+
var link = container.childNodes[0];
|
|
70
|
+
expect(link.tagName).toEqual('A');
|
|
71
|
+
expect(link.getAttribute('href')).toEqual(HREF);
|
|
72
|
+
expect(link.textContent).toEqual("".concat(ROW.action.label, " ").concat(ROW.action.aria_suffix));
|
|
73
|
+
});
|
|
74
|
+
it('should handle a row with an onAction in the action', function () {
|
|
75
|
+
var ON_ACTION_CALLS = [];
|
|
76
|
+
|
|
77
|
+
var ON_ACTION = function ON_ACTION(row) {
|
|
78
|
+
ON_ACTION_CALLS.push(row);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
var ROW = {
|
|
82
|
+
action: {
|
|
83
|
+
onAction: ON_ACTION,
|
|
84
|
+
label: 'Change'
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
var _render5 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_RowAction.default, {
|
|
89
|
+
row: ROW
|
|
90
|
+
})),
|
|
91
|
+
container = _render5.container;
|
|
92
|
+
|
|
93
|
+
var link = container.childNodes[0];
|
|
94
|
+
expect(link.tagName).toEqual('A');
|
|
95
|
+
expect(link.getAttribute('onClick')).toBeDefined();
|
|
96
|
+
expect(link.textContent).toEqual(ROW.action.label);
|
|
97
|
+
|
|
98
|
+
_react.fireEvent.click(link, {});
|
|
99
|
+
|
|
100
|
+
expect(ON_ACTION_CALLS.length).toEqual(1);
|
|
101
|
+
expect(ON_ACTION_CALLS[0]).toEqual(ROW);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -5,30 +5,22 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = exports.DEFAULT_CLASS = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _copReactComponents = require("@ukhomeoffice/cop-react-components");
|
|
9
9
|
|
|
10
10
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
11
11
|
|
|
12
|
-
var
|
|
12
|
+
var _react = _interopRequireDefault(require("react"));
|
|
13
13
|
|
|
14
14
|
var _RowAction = _interopRequireDefault(require("./RowAction"));
|
|
15
15
|
|
|
16
16
|
require("./SummaryList.scss");
|
|
17
17
|
|
|
18
|
-
var _FormComponent = _interopRequireDefault(require("../FormComponent"));
|
|
19
|
-
|
|
20
18
|
var _excluded = ["rows", "classBlock", "classModifiers", "className"];
|
|
21
19
|
|
|
22
20
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
21
|
|
|
24
22
|
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
23
|
|
|
26
|
-
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; }
|
|
27
|
-
|
|
28
|
-
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; }
|
|
29
|
-
|
|
30
|
-
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; }
|
|
31
|
-
|
|
32
24
|
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
33
25
|
|
|
34
26
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
@@ -55,13 +47,7 @@ var SummaryList = function SummaryList(_ref) {
|
|
|
55
47
|
className: classes('key')
|
|
56
48
|
}, row.key), /*#__PURE__*/_react.default.createElement("dd", {
|
|
57
49
|
className: classes('value')
|
|
58
|
-
}, row.
|
|
59
|
-
component: _objectSpread(_objectSpread({}, row.component), {}, {
|
|
60
|
-
readonly: true
|
|
61
|
-
}),
|
|
62
|
-
noWrap: true,
|
|
63
|
-
value: row.value
|
|
64
|
-
}) : row.value), row.action && /*#__PURE__*/_react.default.createElement("dd", {
|
|
50
|
+
}, row.value), row.action && /*#__PURE__*/_react.default.createElement("dd", {
|
|
65
51
|
className: classes('actions')
|
|
66
52
|
}, /*#__PURE__*/_react.default.createElement(_RowAction.default, {
|
|
67
53
|
row: row
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var getRowActionAttributes = function getRowActionAttributes(row) {
|
|
9
|
+
if (row && row.action) {
|
|
10
|
+
if (typeof row.action.onAction === 'function') {
|
|
11
|
+
return {
|
|
12
|
+
onClick: function onClick() {
|
|
13
|
+
return row.action.onAction(row);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
} else {
|
|
17
|
+
return {
|
|
18
|
+
href: row.action.href
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return {};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
var _default = getRowActionAttributes;
|
|
27
|
+
exports.default = _default;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _getRowActionAttributes = _interopRequireDefault(require("./getRowActionAttributes"));
|
|
4
|
+
|
|
5
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
6
|
+
|
|
7
|
+
describe('components', function () {
|
|
8
|
+
describe('SummaryList', function () {
|
|
9
|
+
describe('helpers', function () {
|
|
10
|
+
describe('getRowActionAttributes', function () {
|
|
11
|
+
it('should handle a null row', function () {
|
|
12
|
+
expect((0, _getRowActionAttributes.default)(null)).toEqual({});
|
|
13
|
+
});
|
|
14
|
+
it('should handle a row without an action', function () {
|
|
15
|
+
expect((0, _getRowActionAttributes.default)({})).toEqual({});
|
|
16
|
+
});
|
|
17
|
+
it('should handle a row with an empty action', function () {
|
|
18
|
+
var ROW = {
|
|
19
|
+
action: {}
|
|
20
|
+
};
|
|
21
|
+
expect((0, _getRowActionAttributes.default)(ROW)).toEqual({});
|
|
22
|
+
});
|
|
23
|
+
it('should handle a row with an href', function () {
|
|
24
|
+
var HREF = 'http://alpha.homeoffice.gov.uk';
|
|
25
|
+
var ROW = {
|
|
26
|
+
action: {
|
|
27
|
+
href: HREF
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
expect((0, _getRowActionAttributes.default)(ROW)).toEqual({
|
|
31
|
+
href: HREF
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
it('should handle a row with an onAction function', function () {
|
|
35
|
+
var ON_ACTION_CALLS = [];
|
|
36
|
+
|
|
37
|
+
var ON_ACTION = function ON_ACTION(row) {
|
|
38
|
+
ON_ACTION_CALLS.push(row);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
var ROW = {
|
|
42
|
+
action: {
|
|
43
|
+
onAction: ON_ACTION
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var ATTRS = (0, _getRowActionAttributes.default)(ROW);
|
|
47
|
+
expect(ATTRS.onClick).toBeDefined();
|
|
48
|
+
expect(ATTRS.href).not.toBeDefined();
|
|
49
|
+
ATTRS.onClick();
|
|
50
|
+
expect(ON_ACTION_CALLS.length).toEqual(1);
|
|
51
|
+
expect(ON_ACTION_CALLS[0]).toEqual(ROW);
|
|
52
|
+
});
|
|
53
|
+
it('should favour onAction over href', function () {
|
|
54
|
+
var ON_ACTION_CALLS = [];
|
|
55
|
+
|
|
56
|
+
var ON_ACTION = function ON_ACTION(row) {
|
|
57
|
+
ON_ACTION_CALLS.push(row);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
var HREF = 'http://alpha.homeoffice.gov.uk';
|
|
61
|
+
var ROW = {
|
|
62
|
+
action: {
|
|
63
|
+
href: HREF,
|
|
64
|
+
onAction: ON_ACTION
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
var ATTRS = (0, _getRowActionAttributes.default)(ROW);
|
|
68
|
+
expect(ATTRS.onClick).toBeDefined();
|
|
69
|
+
expect(ATTRS.href).not.toBeDefined();
|
|
70
|
+
ATTRS.onClick();
|
|
71
|
+
expect(ON_ACTION_CALLS.length).toEqual(1);
|
|
72
|
+
expect(ON_ACTION_CALLS[0]).toEqual(ROW);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "getRowActionAttributes", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function get() {
|
|
9
|
+
return _getRowActionAttributes.default;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
var _getRowActionAttributes = _interopRequireDefault(require("./getRowActionAttributes"));
|
|
14
|
+
|
|
15
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
package/dist/components/index.js
CHANGED
|
@@ -9,6 +9,12 @@ Object.defineProperty(exports, "CheckYourAnswers", {
|
|
|
9
9
|
return _CheckYourAnswers.default;
|
|
10
10
|
}
|
|
11
11
|
});
|
|
12
|
+
Object.defineProperty(exports, "FormComponent", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function get() {
|
|
15
|
+
return _FormComponent.default;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
12
18
|
Object.defineProperty(exports, "FormPage", {
|
|
13
19
|
enumerable: true,
|
|
14
20
|
get: function get() {
|
|
@@ -24,6 +30,8 @@ Object.defineProperty(exports, "FormRenderer", {
|
|
|
24
30
|
|
|
25
31
|
var _CheckYourAnswers = _interopRequireDefault(require("./CheckYourAnswers"));
|
|
26
32
|
|
|
33
|
+
var _FormComponent = _interopRequireDefault(require("./FormComponent"));
|
|
34
|
+
|
|
27
35
|
var _FormPage = _interopRequireDefault(require("./FormPage"));
|
|
28
36
|
|
|
29
37
|
var _FormRenderer = _interopRequireDefault(require("./FormRenderer"));
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.default = exports.STATUS_IDLE = exports.STATUS_FETCHING = exports.STATUS_FETCHED = exports.STATUS_ERROR = void 0;
|
|
6
|
+
exports.default = exports.clear = exports.STATUS_IDLE = exports.STATUS_FETCHING = exports.STATUS_FETCHED = exports.STATUS_ERROR = void 0;
|
|
7
7
|
|
|
8
8
|
var _axios = _interopRequireDefault(require("axios"));
|
|
9
9
|
|
|
@@ -29,8 +29,23 @@ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Sy
|
|
|
29
29
|
|
|
30
30
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
31
31
|
|
|
32
|
+
// Caches for responses and errors.
|
|
32
33
|
var cache = {};
|
|
33
34
|
var errorCache = {};
|
|
35
|
+
|
|
36
|
+
var clearCache = function clearCache(cacheObject) {
|
|
37
|
+
var keys = Object.keys(cacheObject);
|
|
38
|
+
keys.forEach(function (key) {
|
|
39
|
+
delete cacheObject[key];
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
var clear = function clear() {
|
|
44
|
+
clearCache(cache);
|
|
45
|
+
clearCache(errorCache);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
exports.clear = clear;
|
|
34
49
|
var STATUS_IDLE = 'idle';
|
|
35
50
|
exports.STATUS_IDLE = STATUS_IDLE;
|
|
36
51
|
var STATUS_FETCHING = 'fetching';
|
package/dist/hooks/useHooks.js
CHANGED
|
@@ -4,15 +4,29 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.resetHooks = exports.removeHook = exports.default = exports.addHook = void 0;
|
|
7
|
-
var ALLOWED_HOOKS = ['onRequest'];
|
|
7
|
+
var ALLOWED_HOOKS = ['onRequest', 'onFormLoad', 'onPageChange', 'onSubmit'];
|
|
8
8
|
var DEFAULT_HOOKS = {
|
|
9
9
|
onRequest: function onRequest(req) {
|
|
10
10
|
return req;
|
|
11
|
+
},
|
|
12
|
+
onFormLoad: function onFormLoad() {},
|
|
13
|
+
onPageChange: function onPageChange(pageId) {
|
|
14
|
+
return pageId;
|
|
15
|
+
},
|
|
16
|
+
onSubmit: function onSubmit(type, payload, onSuccess, onError) {
|
|
17
|
+
onSuccess();
|
|
11
18
|
}
|
|
12
19
|
};
|
|
13
20
|
var hooks = {
|
|
14
21
|
onRequest: function onRequest(req) {
|
|
15
22
|
return req;
|
|
23
|
+
},
|
|
24
|
+
onFormLoad: function onFormLoad() {},
|
|
25
|
+
onPageChange: function onPageChange(pageId) {
|
|
26
|
+
return pageId;
|
|
27
|
+
},
|
|
28
|
+
onSubmit: function onSubmit(type, payload, onSuccess, onError) {
|
|
29
|
+
onSuccess();
|
|
16
30
|
}
|
|
17
31
|
};
|
|
18
32
|
|
|
@@ -102,10 +102,10 @@ var getComponentByType = function getComponentByType(config) {
|
|
|
102
102
|
};
|
|
103
103
|
|
|
104
104
|
var getComponent = function getComponent(config) {
|
|
105
|
-
var
|
|
105
|
+
var wrap = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
106
106
|
var component = getComponentByType(config);
|
|
107
107
|
|
|
108
|
-
if (component &&
|
|
108
|
+
if (component && wrap && (0, _isEditable.default)(config)) {
|
|
109
109
|
var attrs = (0, _cleanAttributes.default)(config, ['fieldId', 'displayMenu']);
|
|
110
110
|
return (0, _wrapInFormGroup.default)(attrs, component);
|
|
111
111
|
}
|
|
@@ -44,9 +44,9 @@ var setupPageSourceData = function setupPageSourceData(pages, data) {
|
|
|
44
44
|
* 2: fieldC: 'value'
|
|
45
45
|
* In the above example, fieldA will not get a value as fieldB isn't
|
|
46
46
|
* set until after fieldA is processed.
|
|
47
|
-
* @param {
|
|
48
|
-
* @param {
|
|
49
|
-
* @param {
|
|
47
|
+
* @param {Array} pages
|
|
48
|
+
* @param {Array} components
|
|
49
|
+
* @param {object} baseData
|
|
50
50
|
* @returns
|
|
51
51
|
*/
|
|
52
52
|
|
|
@@ -19,7 +19,18 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
19
19
|
|
|
20
20
|
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; }
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Converts a page object defined in the JSON to a page object for rendering.
|
|
24
|
+
* @param {object} pageOptions The JSON page.
|
|
25
|
+
* @param {Array} formComponents The components defined at the top-level of the form.
|
|
26
|
+
* @param {object} formData The top-level form data, used for setting up components.
|
|
27
|
+
* @returns A page object for rendering.
|
|
28
|
+
*/
|
|
22
29
|
var getFormPage = function getFormPage(pageOptions, formComponents, formData) {
|
|
30
|
+
if (!pageOptions) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
23
34
|
var components = pageOptions.components.map(function (componentOptions) {
|
|
24
35
|
if (typeof componentOptions === 'string') {
|
|
25
36
|
return (0, _getParagraphFromText.default)(componentOptions);
|
|
@@ -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: '${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, _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.environmentContext.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
|
+
});
|