@ukhomeoffice/cop-react-form-renderer 6.15.3-alpha → 6.15.3
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/CollectionSummary/BannerStrip.scss +4 -0
- package/dist/components/CollectionSummary/CollectionSummary.js +2 -2
- package/dist/components/CollectionSummary/SummaryCard.js +2 -2
- package/dist/components/FormComponent/Collection.js +2 -2
- package/dist/components/FormComponent/Container.js +2 -2
- package/dist/components/FormComponent/FormComponent.js +2 -2
- package/dist/components/FormComponent/helpers/addLabel.js +3 -2
- package/dist/components/FormPage/FormPage.js +28 -17
- package/dist/components/FormPage/FormPage.test.js +53 -0
- package/dist/components/FormRenderer/FormRenderer.js +7 -12
- package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-component-show-when-in-component-and-page.json +62 -0
- package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-hidden-page-same-component-reused.json +61 -0
- package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/forms/form-page-same-component-reused-one-shown.json +74 -0
- package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/cop-airpax-change-what-happened-before.json +300 -0
- package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-component-show-when-in-component-and-page.json +6 -0
- package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-hidden-page-same-component-reused.json +6 -0
- package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/input/data-page-same-component-reused-one-shown.json +8 -0
- package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/cop-airpax-change-what-happened-after.json +280 -0
- package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-component-show-when-in-component-and-page-removed.json +5 -0
- package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-hidden-page-same-component-reused-removed.json +5 -0
- package/dist/components/FormRenderer/clear-uncompleted-routes/test-data/output/data-page-same-component-reused-one-shown-removed.json +7 -0
- package/dist/components/FormRenderer/helpers/clearOutUncompletedRoutes.js +259 -289
- package/dist/components/FormRenderer/helpers/clearOutUncompletedRoutes.test.js +68 -27
- package/dist/components/FormRenderer/helpers/clearOutUncompletedRoutesUtils.js +381 -0
- package/dist/components/FormRenderer/helpers/clearOutUncompletedRoutesUtils.test.js +559 -0
- package/dist/components/FormRenderer/onCYAAction.js +12 -0
- package/dist/components/FormRenderer/onCYAAction.test.js +5 -0
- package/dist/components/FormRenderer/onPageAction.js +0 -1
- package/dist/components/PageActions/ActionButton.js +2 -2
- package/dist/components/SummaryList/SummaryList.js +2 -2
- package/dist/components/TaskList/TaskList.js +2 -2
- package/dist/hooks/useGetRequest.js +15 -15
- package/dist/hooks/useRefData.js +3 -2
- package/dist/utils/CollectionPage/getQuickEditPage.js +2 -2
- package/dist/utils/Component/getComponentTests/getComponent.multifile.test.js +2 -1
- package/dist/utils/Component/getDefaultValueFromConfig.js +2 -1
- package/dist/utils/Condition/meetsCondition.js +26 -12
- package/dist/utils/Condition/meetsCondition.test.js +21 -0
- package/dist/utils/Data/getAutocompleteSource.js +68 -51
- package/dist/utils/Data/getAutocompleteSource.test.js +31 -18
- package/dist/utils/Operate/doesContainValue.js +34 -0
- package/dist/utils/Operate/doesContainValue.test.js +75 -0
- package/dist/utils/Operate/runPageOperations.js +2 -0
- package/dist/utils/Validate/validateOnPageLoad.js +23 -0
- package/dist/utils/Validate/validateOnPageLoad.test.js +88 -0
- package/package.json +4 -4
- package/dist/components/FormRenderer/helpers/deleteNodeByPath.js +0 -29
- package/dist/components/FormRenderer/helpers/deleteNodeByPath.test.js +0 -56
|
@@ -8,10 +8,6 @@ var _axios = _interopRequireDefault(require("axios"));
|
|
|
8
8
|
var _react = require("react");
|
|
9
9
|
var _useAxios = _interopRequireDefault(require("./useAxios"));
|
|
10
10
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
|
-
// Global imports
|
|
12
|
-
|
|
13
|
-
// Local imports
|
|
14
|
-
|
|
15
11
|
// Caches for responses and errors.
|
|
16
12
|
const cache = {};
|
|
17
13
|
const errorCache = {};
|
|
@@ -31,15 +27,18 @@ const STATUS_IDLE = exports.STATUS_IDLE = 'idle';
|
|
|
31
27
|
const STATUS_FETCHING = exports.STATUS_FETCHING = 'fetching';
|
|
32
28
|
const STATUS_FETCHED = exports.STATUS_FETCHED = 'fetched';
|
|
33
29
|
const STATUS_ERROR = exports.STATUS_ERROR = 'error';
|
|
34
|
-
const useGetRequest = url
|
|
30
|
+
const useGetRequest = function (url) {
|
|
31
|
+
let caching = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
35
32
|
const axiosInstance = (0, _useAxios.default)();
|
|
36
|
-
const
|
|
37
|
-
const cancelRequests = () => {
|
|
38
|
-
if (cancelToken) cancelToken.cancel();
|
|
39
|
-
};
|
|
33
|
+
const cancelTokenRef = (0, _react.useRef)(_axios.default.CancelToken.source());
|
|
40
34
|
const [status, setStatus] = (0, _react.useState)(STATUS_IDLE);
|
|
41
35
|
const [error, setError] = (0, _react.useState)(null);
|
|
42
36
|
const [data, setData] = (0, _react.useState)(null);
|
|
37
|
+
const cancelRequests = () => {
|
|
38
|
+
if (cancelTokenRef.current) {
|
|
39
|
+
cancelTokenRef.current.cancel();
|
|
40
|
+
}
|
|
41
|
+
};
|
|
43
42
|
(0, _react.useEffect)(() => {
|
|
44
43
|
if (!url || !axiosInstance) return;
|
|
45
44
|
const fetchData = async () => {
|
|
@@ -47,9 +46,9 @@ const useGetRequest = url => {
|
|
|
47
46
|
setError(null);
|
|
48
47
|
setStatus(STATUS_FETCHING);
|
|
49
48
|
let fetchedData;
|
|
50
|
-
if (cache[url]) {
|
|
49
|
+
if (caching && cache[url]) {
|
|
51
50
|
fetchedData = cache[url];
|
|
52
|
-
} else if (errorCache[url]) {
|
|
51
|
+
} else if (caching && errorCache[url]) {
|
|
53
52
|
/**
|
|
54
53
|
* This logic is intended to stop multiple requests being made in succession
|
|
55
54
|
* that all fail. Presently, this will only allow the first request to be
|
|
@@ -60,24 +59,25 @@ const useGetRequest = url => {
|
|
|
60
59
|
throw errorCache[url];
|
|
61
60
|
} else {
|
|
62
61
|
const response = await axiosInstance.get(url, {
|
|
63
|
-
cancelToken:
|
|
62
|
+
cancelToken: cancelTokenRef.current.token
|
|
64
63
|
}).catch(e => {
|
|
64
|
+
setError(e);
|
|
65
65
|
throw e;
|
|
66
66
|
});
|
|
67
67
|
fetchedData = response.data;
|
|
68
|
-
cache[url] = fetchedData;
|
|
68
|
+
if (caching) cache[url] = fetchedData;
|
|
69
69
|
}
|
|
70
70
|
setData(fetchedData);
|
|
71
71
|
setStatus(STATUS_FETCHED);
|
|
72
72
|
} catch (e) {
|
|
73
|
-
errorCache[url] = e;
|
|
73
|
+
if (caching) errorCache[url] = e;
|
|
74
74
|
setError(e);
|
|
75
75
|
setData(null);
|
|
76
76
|
setStatus(STATUS_ERROR);
|
|
77
77
|
}
|
|
78
78
|
};
|
|
79
79
|
fetchData();
|
|
80
|
-
}, [axiosInstance, url,
|
|
80
|
+
}, [axiosInstance, url, caching]);
|
|
81
81
|
return {
|
|
82
82
|
status,
|
|
83
83
|
error,
|
package/dist/hooks/useRefData.js
CHANGED
|
@@ -28,13 +28,14 @@ const getRefDataUrl = component => {
|
|
|
28
28
|
return undefined;
|
|
29
29
|
};
|
|
30
30
|
const useRefData = (component, formData) => {
|
|
31
|
+
var _component$data;
|
|
31
32
|
const url = getRefDataUrl(_objectSpread(_objectSpread({}, component), {}, {
|
|
32
33
|
formData
|
|
33
34
|
}));
|
|
34
35
|
const {
|
|
35
36
|
status: _status,
|
|
36
37
|
data: _data
|
|
37
|
-
} = (0, _useGetRequest.default)(url);
|
|
38
|
+
} = (0, _useGetRequest.default)(url, component === null || component === void 0 || (_component$data = component.data) === null || _component$data === void 0 ? void 0 : _component$data.useCache);
|
|
38
39
|
const [data, setData] = (0, _react.useState)([]);
|
|
39
40
|
const [status, setStatus] = (0, _react.useState)(STATUS_LOADING);
|
|
40
41
|
(0, _react.useEffect)(() => {
|
|
@@ -52,7 +53,7 @@ const useRefData = (component, formData) => {
|
|
|
52
53
|
setData([]);
|
|
53
54
|
setStatus(STATUS_COMPLETE);
|
|
54
55
|
}
|
|
55
|
-
}, [component, _status, _data, url
|
|
56
|
+
}, [component.id, _status, _data, url]);
|
|
56
57
|
return {
|
|
57
58
|
data,
|
|
58
59
|
status
|
|
@@ -8,8 +8,8 @@ var _meetsAllConditions = _interopRequireDefault(require("../Condition/meetsAllC
|
|
|
8
8
|
var _showFormPage = _interopRequireDefault(require("../FormPage/showFormPage"));
|
|
9
9
|
const _excluded = ["id"];
|
|
10
10
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
|
-
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var
|
|
12
|
-
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.
|
|
11
|
+
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
|
12
|
+
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|
13
13
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
14
14
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
15
15
|
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
@@ -75,7 +75,8 @@ const setupDefaultObjectValue = (defaultObject, data) => {
|
|
|
75
75
|
// defaultObject.sourced is an object with values that should
|
|
76
76
|
// be used as field names to get values from data.
|
|
77
77
|
if (defaultObj.sourced) {
|
|
78
|
-
|
|
78
|
+
var _result;
|
|
79
|
+
result = (_result = result) !== null && _result !== void 0 ? _result : {};
|
|
79
80
|
Object.keys(defaultObj.sourced).every(key => {
|
|
80
81
|
const sourcedValue = typeof defaultObj.sourced[key] === 'string' && (0, _getSourceData.default)(data, defaultObj.sourced[key]);
|
|
81
82
|
if (sourcedValue === undefined && defaultObj.skipIfSourceInvalid) {
|
|
@@ -20,6 +20,26 @@ const getComparisonValue = (condition, data) => {
|
|
|
20
20
|
return condition.value;
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Processes a value based on its type.
|
|
25
|
+
* - If it's an array, returns its length.
|
|
26
|
+
* - If it's a string, parses it as a float.
|
|
27
|
+
* - If it's a number, returns it as is.
|
|
28
|
+
* - Returns `undefined` for other types.
|
|
29
|
+
*
|
|
30
|
+
* @param {any} value - The value to process.
|
|
31
|
+
* @returns {number | undefined} - The processed value or `undefined` if not applicable.
|
|
32
|
+
*/
|
|
33
|
+
const getProcessedValue = obj => {
|
|
34
|
+
if (Array.isArray(obj)) return obj.length;
|
|
35
|
+
if (typeof obj === "string") {
|
|
36
|
+
const cleaned = obj.replace(/,/g, ''); // Remove all commas
|
|
37
|
+
return Number.isNaN(cleaned) ? null : parseFloat(cleaned);
|
|
38
|
+
}
|
|
39
|
+
if (typeof obj === "number") return obj;
|
|
40
|
+
return null;
|
|
41
|
+
};
|
|
42
|
+
|
|
23
43
|
/**
|
|
24
44
|
* Looks at a condition object to see if the supplied value meets it.
|
|
25
45
|
* The condition object contains an `op` property, along with a comparator
|
|
@@ -66,21 +86,15 @@ const meetsCondition = (condition, value, data) => {
|
|
|
66
86
|
}
|
|
67
87
|
case '<':
|
|
68
88
|
{
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const valFloat = parseFloat(value.replace(',', ''));
|
|
73
|
-
const compareFloat = parseFloat(compare.replace(',', ''));
|
|
74
|
-
return valFloat < compareFloat;
|
|
89
|
+
const valFloat = getProcessedValue(value);
|
|
90
|
+
const compareFloat = getProcessedValue(compare);
|
|
91
|
+
return valFloat != null && compareFloat != null && valFloat < compareFloat;
|
|
75
92
|
}
|
|
76
93
|
case '>':
|
|
77
94
|
{
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const valFloat = parseFloat(value.replace(/,/g, ''));
|
|
82
|
-
const compareFloat = parseFloat(compare.replace(/,/g, ''));
|
|
83
|
-
return valFloat > compareFloat;
|
|
95
|
+
const valFloat = getProcessedValue(value);
|
|
96
|
+
const compareFloat = getProcessedValue(compare);
|
|
97
|
+
return valFloat != null && compareFloat != null && valFloat > compareFloat;
|
|
84
98
|
}
|
|
85
99
|
case 'contains':
|
|
86
100
|
{
|
|
@@ -551,6 +551,27 @@ describe('utils.Condition.meetsCondition', () => {
|
|
|
551
551
|
};
|
|
552
552
|
expect((0, _meetsCondition.default)(CONDITION, '10,000,000')).toBeTruthy();
|
|
553
553
|
});
|
|
554
|
+
it('greater than should handle value is 0', () => {
|
|
555
|
+
const CONDITION = {
|
|
556
|
+
op: '>',
|
|
557
|
+
value: 0
|
|
558
|
+
};
|
|
559
|
+
expect((0, _meetsCondition.default)(CONDITION, 1)).toBeTruthy();
|
|
560
|
+
});
|
|
561
|
+
it('greater than should handle compear is 0', () => {
|
|
562
|
+
const CONDITION = {
|
|
563
|
+
op: '<',
|
|
564
|
+
value: 1
|
|
565
|
+
};
|
|
566
|
+
expect((0, _meetsCondition.default)(CONDITION, 0)).toBeTruthy();
|
|
567
|
+
});
|
|
568
|
+
it('greater than should handle array is 0', () => {
|
|
569
|
+
const CONDITION = {
|
|
570
|
+
op: '>',
|
|
571
|
+
value: 1
|
|
572
|
+
};
|
|
573
|
+
expect((0, _meetsCondition.default)(CONDITION, ["A", "B", "C"])).toBeTruthy();
|
|
574
|
+
});
|
|
554
575
|
});
|
|
555
576
|
describe('operator includes', () => {
|
|
556
577
|
it('should accept a string that exists within the value', () => {
|
|
@@ -7,63 +7,80 @@ exports.default = void 0;
|
|
|
7
7
|
var _copReactComponents = require("@ukhomeoffice/cop-react-components");
|
|
8
8
|
var _getOptions = _interopRequireDefault(require("./getOptions"));
|
|
9
9
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
-
//
|
|
11
|
-
|
|
12
|
-
// Local imports
|
|
10
|
+
const MAX_NUMBER_RESULTS = 6000; // lower is better for performance, this still includes all ports
|
|
13
11
|
|
|
12
|
+
const assignFullMatchScore = (subStringMatchIndex, label) => {
|
|
13
|
+
const matchAtBeginning = subStringMatchIndex === 0;
|
|
14
|
+
let score = 0;
|
|
15
|
+
if (matchAtBeginning) {
|
|
16
|
+
score = 1;
|
|
17
|
+
}
|
|
18
|
+
if (!matchAtBeginning) {
|
|
19
|
+
const isCompleteWord = label.substring(subStringMatchIndex - 1).startsWith(" ");
|
|
20
|
+
score = isCompleteWord ? 2 : 3;
|
|
21
|
+
}
|
|
22
|
+
return score;
|
|
23
|
+
};
|
|
24
|
+
const noMatch = score => score === 0;
|
|
25
|
+
const lowerCaseMatch = (searchTerm, query) => typeof searchTerm !== 'string' ? false : searchTerm.toLowerCase().includes(query);
|
|
26
|
+
const assignSynonymsScore = (option, searchQuery) => {
|
|
27
|
+
let score = 0;
|
|
28
|
+
const isMatch = option.synonyms.some(synonym => lowerCaseMatch(synonym, searchQuery));
|
|
29
|
+
if (isMatch) {
|
|
30
|
+
score = 4;
|
|
31
|
+
}
|
|
32
|
+
return score;
|
|
33
|
+
};
|
|
34
|
+
const assignExtraFieldsScore = (config, option, searchQuery) => {
|
|
35
|
+
let score = 0;
|
|
36
|
+
const isMatch = config.item.extraFieldsToSearch.some(field => {
|
|
37
|
+
const fieldToSearch = option[field];
|
|
38
|
+
return lowerCaseMatch(fieldToSearch, searchQuery);
|
|
39
|
+
});
|
|
40
|
+
if (isMatch) {
|
|
41
|
+
score = 5;
|
|
42
|
+
}
|
|
43
|
+
return score;
|
|
44
|
+
};
|
|
45
|
+
const mapToScoreAndLabel = (option, searchQuery, labelMapper, config) => {
|
|
46
|
+
var _config$item;
|
|
47
|
+
const label = labelMapper ? labelMapper(option).toLowerCase() : option.label.toLowerCase() || '';
|
|
48
|
+
let score = 0;
|
|
49
|
+
const subStringMatchIndex = label.indexOf(searchQuery);
|
|
50
|
+
const isFullMatch = subStringMatchIndex !== -1;
|
|
51
|
+
if (isFullMatch) {
|
|
52
|
+
score = assignFullMatchScore(subStringMatchIndex, label);
|
|
53
|
+
}
|
|
54
|
+
if (noMatch(score) && option.synonyms) {
|
|
55
|
+
score = assignSynonymsScore(option, searchQuery);
|
|
56
|
+
}
|
|
57
|
+
if (noMatch(score) && (_config$item = config.item) !== null && _config$item !== void 0 && _config$item.extraFieldsToSearch) {
|
|
58
|
+
score = assignExtraFieldsScore(config, option, searchQuery);
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
score,
|
|
62
|
+
label,
|
|
63
|
+
option
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
const scoreOptions = (searchQuery, labelMapper, config) => (scoredOptions, option) => {
|
|
67
|
+
const scoredOption = mapToScoreAndLabel(option, searchQuery, labelMapper, config);
|
|
68
|
+
if (scoredOption.score > 0) {
|
|
69
|
+
scoredOptions.push(scoredOption);
|
|
70
|
+
}
|
|
71
|
+
return scoredOptions;
|
|
72
|
+
};
|
|
73
|
+
const compareByScoreAndLabel = (r1, r2) => (r1.score - r2.score) * 16 + r1.label.localeCompare(r2.label);
|
|
14
74
|
const getAutocompleteSource = config => {
|
|
15
75
|
let options = [];
|
|
16
76
|
(0, _getOptions.default)(config, val => {
|
|
17
77
|
options = val;
|
|
18
78
|
});
|
|
19
|
-
const
|
|
20
|
-
return (query,
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const results = options.map(opt => {
|
|
26
|
-
const label = labelMaker ? labelMaker(opt) : opt.label || '';
|
|
27
|
-
const lcLabel = label.toLowerCase();
|
|
28
|
-
|
|
29
|
-
// result 'score'
|
|
30
|
-
let grade = 0;
|
|
31
|
-
|
|
32
|
-
// highest result - match at start of string
|
|
33
|
-
const index = lcLabel.indexOf(lcQuery);
|
|
34
|
-
if (index === 0) {
|
|
35
|
-
grade = 1;
|
|
36
|
-
} else if (index > 0) {
|
|
37
|
-
grade = lcLabel.substring(index - 1).startsWith(" ") ? 2 : 3;
|
|
38
|
-
} else {
|
|
39
|
-
var _config$item;
|
|
40
|
-
if (opt.synonyms) {
|
|
41
|
-
const match = opt.synonyms.some(synonym => synonym.toLowerCase().includes(lcQuery));
|
|
42
|
-
if (match) {
|
|
43
|
-
grade = 4;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
if (!grade && (_config$item = config.item) !== null && _config$item !== void 0 && _config$item.extraFieldsToSearch) {
|
|
47
|
-
const found = config.item.extraFieldsToSearch.some(field => {
|
|
48
|
-
if (typeof opt[field] !== 'string') {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
return opt[field].toLowerCase().includes(lcQuery);
|
|
52
|
-
});
|
|
53
|
-
if (found) grade = 5;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return {
|
|
57
|
-
grade,
|
|
58
|
-
label,
|
|
59
|
-
opt
|
|
60
|
-
};
|
|
61
|
-
}).filter(result => result.grade > 0);
|
|
62
|
-
|
|
63
|
-
// sort results and then map to just opts
|
|
64
|
-
populateResults(results
|
|
65
|
-
// the sort prioritises grade but also sorts by string value
|
|
66
|
-
.sort((r1, r2) => (r1.grade - r2.grade) * 16 + r1.label.localeCompare(r2.label)).map(r => r.opt));
|
|
79
|
+
const labelMapper = config !== null && config !== void 0 && config.item ? _copReactComponents.Utils.itemLabel(config.item) : null;
|
|
80
|
+
return (query, renderResult) => {
|
|
81
|
+
const searchQuery = typeof query === 'string' ? query.toLowerCase() : '';
|
|
82
|
+
const result = options.reduce(scoreOptions(searchQuery, labelMapper, config), []).sort(compareByScoreAndLabel).slice(0, MAX_NUMBER_RESULTS).map(scoredOptions => scoredOptions.option);
|
|
83
|
+
renderResult(result);
|
|
67
84
|
};
|
|
68
85
|
};
|
|
69
86
|
var _default = exports.default = getAutocompleteSource;
|
|
@@ -6,10 +6,9 @@ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbol
|
|
|
6
6
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
7
7
|
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
8
8
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
9
|
-
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
9
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
10
10
|
describe('utils', () => {
|
|
11
11
|
describe('Data', () => {
|
|
12
|
-
// test data
|
|
13
12
|
const gbCurrency = {
|
|
14
13
|
currencyName: 'Great British Pounds',
|
|
15
14
|
currencyCode: 'GBP',
|
|
@@ -25,7 +24,7 @@ describe('utils', () => {
|
|
|
25
24
|
value: 'USD',
|
|
26
25
|
label: 'USD',
|
|
27
26
|
slangTerm: 'bucks',
|
|
28
|
-
synonyms: ['greenbacks']
|
|
27
|
+
synonyms: ['greenbacks', 11]
|
|
29
28
|
};
|
|
30
29
|
const options = {
|
|
31
30
|
alpha: {
|
|
@@ -81,6 +80,15 @@ describe('utils', () => {
|
|
|
81
80
|
}]);
|
|
82
81
|
});
|
|
83
82
|
});
|
|
83
|
+
it('should only return first 6000 results that have some type of match', () => {
|
|
84
|
+
const moreThanMaxNumberOfOptions = Array(6001).fill(options.alpha);
|
|
85
|
+
const config = {
|
|
86
|
+
data: {
|
|
87
|
+
options: moreThanMaxNumberOfOptions
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
(0, _getAutocompleteSource.default)(config)(options.alpha.label, results => expect(results.length).toEqual(6000));
|
|
91
|
+
});
|
|
84
92
|
it('should handle an empty query', () => {
|
|
85
93
|
const CONFIG = {
|
|
86
94
|
data: {
|
|
@@ -117,6 +125,18 @@ describe('utils', () => {
|
|
|
117
125
|
expect(results).toEqual([options.alpha, options.bravo]);
|
|
118
126
|
});
|
|
119
127
|
});
|
|
128
|
+
it('should handle a non string query', () => {
|
|
129
|
+
const CONFIG = {
|
|
130
|
+
data: {
|
|
131
|
+
options: [options.alpha, options.bravo]
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
const SOURCE = (0, _getAutocompleteSource.default)(CONFIG);
|
|
135
|
+
SOURCE(1, results => {
|
|
136
|
+
expect(results.length).toEqual(2);
|
|
137
|
+
expect(results).toEqual([options.alpha, options.bravo]);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
120
140
|
it('should handle a missing label on an option', () => {
|
|
121
141
|
const CONFIG = {
|
|
122
142
|
data: {
|
|
@@ -171,8 +191,7 @@ describe('utils', () => {
|
|
|
171
191
|
item: {
|
|
172
192
|
value: 'currencyCode',
|
|
173
193
|
label: 'currencyCode',
|
|
174
|
-
|
|
175
|
-
format: '${currencyName} (${currencyCode})'
|
|
194
|
+
format: "${currencyName} (${currencyCode})"
|
|
176
195
|
},
|
|
177
196
|
data: {
|
|
178
197
|
options: testCurrencies
|
|
@@ -194,8 +213,7 @@ describe('utils', () => {
|
|
|
194
213
|
item: {
|
|
195
214
|
value: 'currencyCode',
|
|
196
215
|
label: 'currencyCode',
|
|
197
|
-
|
|
198
|
-
format: '${currencyName} (${currencyCode})'
|
|
216
|
+
format: "${currencyName} (${currencyCode})"
|
|
199
217
|
},
|
|
200
218
|
data: {
|
|
201
219
|
options: testCurrencies
|
|
@@ -212,8 +230,7 @@ describe('utils', () => {
|
|
|
212
230
|
item: {
|
|
213
231
|
value: 'currencyCode',
|
|
214
232
|
label: 'currencyCode',
|
|
215
|
-
|
|
216
|
-
format: '${currencyName} (${currencyCode})'
|
|
233
|
+
format: "${currencyName} (${currencyCode})"
|
|
217
234
|
},
|
|
218
235
|
data: {
|
|
219
236
|
options: testCurrencies
|
|
@@ -230,8 +247,7 @@ describe('utils', () => {
|
|
|
230
247
|
item: {
|
|
231
248
|
value: 'currencyCode',
|
|
232
249
|
label: 'currencyCode',
|
|
233
|
-
|
|
234
|
-
format: '${currencyName} (${currencyCode})'
|
|
250
|
+
format: "${currencyName} (${currencyCode})"
|
|
235
251
|
},
|
|
236
252
|
data: {
|
|
237
253
|
options: testCurrencies
|
|
@@ -248,8 +264,7 @@ describe('utils', () => {
|
|
|
248
264
|
item: {
|
|
249
265
|
value: 'currencyCode',
|
|
250
266
|
label: 'currencyCode',
|
|
251
|
-
|
|
252
|
-
format: '${currencyName} (${currencyCode})'
|
|
267
|
+
format: "${currencyName} (${currencyCode})"
|
|
253
268
|
},
|
|
254
269
|
data: {
|
|
255
270
|
options: [gbCurrency, {
|
|
@@ -281,8 +296,7 @@ describe('utils', () => {
|
|
|
281
296
|
item: {
|
|
282
297
|
value: 'currencyCode',
|
|
283
298
|
label: 'currencyCode',
|
|
284
|
-
|
|
285
|
-
format: '${currencyName} (${currencyCode})'
|
|
299
|
+
format: "${currencyName} (${currencyCode})"
|
|
286
300
|
},
|
|
287
301
|
data: {
|
|
288
302
|
options: testCurrencies
|
|
@@ -300,9 +314,8 @@ describe('utils', () => {
|
|
|
300
314
|
item: {
|
|
301
315
|
value: 'currencyCode',
|
|
302
316
|
label: 'currencyCode',
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
extraFieldsToSearch: ['slangTerm']
|
|
317
|
+
format: "${currencyName} (${currencyCode})",
|
|
318
|
+
extraFieldsToSearch: ['slangTerm', null]
|
|
306
319
|
},
|
|
307
320
|
data: {
|
|
308
321
|
options: testCurrencies
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _copReactComponents = require("@ukhomeoffice/cop-react-components");
|
|
8
|
+
var _getSourceData = _interopRequireDefault(require("../Data/getSourceData"));
|
|
9
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
// Global imports.
|
|
11
|
+
|
|
12
|
+
// Local imports.
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Returns if it can find a value in the form data. supports searching in arrays and objects
|
|
16
|
+
* @param {object} config The config of the operation.
|
|
17
|
+
* @param {string} config.target the JPath to the object/array that contains the data we want to checkon
|
|
18
|
+
* @param {string} config.key the JPath to the field we want to check
|
|
19
|
+
* @param {string} config.value the value that we want to check the the data against
|
|
20
|
+
* @param {object} data A page's formData.
|
|
21
|
+
* @returns The index of the matching value if one exists, or null.
|
|
22
|
+
*/
|
|
23
|
+
const doesContainValue = (config, data) => {
|
|
24
|
+
const targetPath = _copReactComponents.Utils.interpolateString(config.target, data);
|
|
25
|
+
const target = (0, _getSourceData.default)(data, targetPath);
|
|
26
|
+
if (target && Array.isArray(target)) {
|
|
27
|
+
return target.some(entry => config.key ? (0, _getSourceData.default)(entry, config.key) === config.value : entry === config.value);
|
|
28
|
+
}
|
|
29
|
+
if (target && typeof target === "object") {
|
|
30
|
+
return (0, _getSourceData.default)(target, config.key) === config.value;
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
};
|
|
34
|
+
var _default = exports.default = doesContainValue;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _doesContainValue = _interopRequireDefault(require("./doesContainValue"));
|
|
4
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
5
|
+
describe('doesContainValue', () => {
|
|
6
|
+
const mockData = {
|
|
7
|
+
arrayOfValues: ['abc', 'bcd', 'cde', 'def', {
|
|
8
|
+
root: {
|
|
9
|
+
branch: 'leaf'
|
|
10
|
+
}
|
|
11
|
+
}],
|
|
12
|
+
cutoffIndex: 1,
|
|
13
|
+
ignoreIndex: 2,
|
|
14
|
+
valueToSearchFor: 'cde',
|
|
15
|
+
targetName: 'arrayOfValues',
|
|
16
|
+
fieldName: 'valueToSearchFor',
|
|
17
|
+
cutoffName: 'cutoffIndex',
|
|
18
|
+
ignoreName: 'ignoreIndex'
|
|
19
|
+
};
|
|
20
|
+
test('returns true if value exists in array', () => {
|
|
21
|
+
const config = {
|
|
22
|
+
target: 'arrayOfValues',
|
|
23
|
+
value: 'cde'
|
|
24
|
+
};
|
|
25
|
+
const result = (0, _doesContainValue.default)(config, mockData);
|
|
26
|
+
expect(result).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
test('returns false if value does not exist in array', () => {
|
|
29
|
+
const config = {
|
|
30
|
+
target: 'arrayOfValues',
|
|
31
|
+
value: 'xyz'
|
|
32
|
+
};
|
|
33
|
+
const result = (0, _doesContainValue.default)(config, mockData);
|
|
34
|
+
expect(result).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
test('returns true if key-value pair matches in array object', () => {
|
|
37
|
+
const config = {
|
|
38
|
+
target: 'arrayOfValues',
|
|
39
|
+
key: 'root.branch',
|
|
40
|
+
value: 'leaf'
|
|
41
|
+
};
|
|
42
|
+
const result = (0, _doesContainValue.default)(config, mockData);
|
|
43
|
+
expect(result).toBe(true);
|
|
44
|
+
});
|
|
45
|
+
test('returns false if key-value pair does not match in array object', () => {
|
|
46
|
+
const config = {
|
|
47
|
+
target: 'arrayOfValues',
|
|
48
|
+
key: 'root.branch',
|
|
49
|
+
value: 'notLeaf'
|
|
50
|
+
};
|
|
51
|
+
const result = (0, _doesContainValue.default)(config, mockData);
|
|
52
|
+
expect(result).toBe(false);
|
|
53
|
+
});
|
|
54
|
+
test('handles non-array targets gracefully', () => {
|
|
55
|
+
const config = {
|
|
56
|
+
target: 'nonExistentTarget',
|
|
57
|
+
value: 'value'
|
|
58
|
+
};
|
|
59
|
+
const result = (0, _doesContainValue.default)(config, mockData);
|
|
60
|
+
expect(result).toBe(false);
|
|
61
|
+
});
|
|
62
|
+
test('handles objects as target correctly', () => {
|
|
63
|
+
const config = {
|
|
64
|
+
target: 'objectTarget',
|
|
65
|
+
key: 'key',
|
|
66
|
+
value: 'value'
|
|
67
|
+
};
|
|
68
|
+
const result = (0, _doesContainValue.default)(config, {
|
|
69
|
+
objectTarget: {
|
|
70
|
+
key: 'value'
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
expect(result).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
@@ -14,6 +14,7 @@ var _shouldRun = _interopRequireDefault(require("./shouldRun"));
|
|
|
14
14
|
var _setDataItem = _interopRequireDefault(require("../Data/setDataItem"));
|
|
15
15
|
var _getFirstOf = _interopRequireDefault(require("./getFirstOf"));
|
|
16
16
|
var _getLength = _interopRequireDefault(require("./getLength"));
|
|
17
|
+
var _doesContainValue = _interopRequireDefault(require("./doesContainValue"));
|
|
17
18
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
18
19
|
// Global imports.
|
|
19
20
|
|
|
@@ -22,6 +23,7 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
|
|
|
22
23
|
const functions = {
|
|
23
24
|
checkValueIsTruthy: _checkValueIsTruthy.default,
|
|
24
25
|
getIndexOfMatchingValueIn: _getIndexOfMatchingValueIn.default,
|
|
26
|
+
doesContainValue: _doesContainValue.default,
|
|
25
27
|
persistValueInFormData: _persistValueInFormData.default,
|
|
26
28
|
setValueInFormData: _setValueInFormData.default,
|
|
27
29
|
deleteValueInFormData: _deleteValueInFormData.default,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.doValidateOnPageLoad = void 0;
|
|
7
|
+
var _Condition = _interopRequireDefault(require("../Condition"));
|
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
const doValidateOnPageLoad = page => {
|
|
10
|
+
var _validation$runWhen, _validation$runWhen2;
|
|
11
|
+
if (!(page !== null && page !== void 0 && page.customValidation)) return false;
|
|
12
|
+
|
|
13
|
+
// Find the validation with `onLoad` set to true
|
|
14
|
+
const validation = page === null || page === void 0 ? void 0 : page.customValidation.find(item => {
|
|
15
|
+
var _item$runWhen;
|
|
16
|
+
return ((_item$runWhen = item.runWhen) === null || _item$runWhen === void 0 ? void 0 : _item$runWhen.onLoad) === true;
|
|
17
|
+
});
|
|
18
|
+
if (!validation) return false;
|
|
19
|
+
|
|
20
|
+
// Check if the constraint is met using the condition
|
|
21
|
+
return (_validation$runWhen = validation.runWhen) !== null && _validation$runWhen !== void 0 && _validation$runWhen.constraint ? _Condition.default.meetsOne((_validation$runWhen2 = validation.runWhen) === null || _validation$runWhen2 === void 0 ? void 0 : _validation$runWhen2.constraint, page.formData) : false;
|
|
22
|
+
};
|
|
23
|
+
exports.doValidateOnPageLoad = doValidateOnPageLoad;
|