hof 22.12.0-unit-of-measure-beta.1 → 22.13.0-frontend-v4-beta.1
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/CHANGELOG.md +13 -1
- package/README.md +0 -78
- package/build/tasks/images/index.js +20 -5
- package/build/tasks/watch/index.js +10 -2
- package/components/date/templates/date.html +1 -1
- package/components/index.js +0 -1
- package/config/builder-defaults.js +6 -2
- package/controller/controller.js +3 -5
- package/controller/validation/index.js +1 -1
- package/controller/validation/validators.js +1 -0
- package/frontend/govuk-template/build/config.js +1 -0
- package/frontend/govuk-template/build/govuk_template.html +58 -42
- package/frontend/govuk-template/govuk_template_generated.html +118 -0
- package/frontend/template-mixins/mixins/template-mixins.js +10 -59
- package/frontend/template-mixins/partials/forms/checkbox.html +5 -1
- package/frontend/template-mixins/partials/forms/input-text-date.html +2 -1
- package/frontend/template-mixins/partials/forms/input-text-group.html +1 -1
- package/frontend/template-mixins/partials/forms/textarea-group.html +1 -1
- package/frontend/template-partials/views/layout.html +1 -1
- package/frontend/template-partials/views/partials/cookie-banner.html +50 -32
- package/frontend/template-partials/views/partials/navigation.html +2 -2
- package/frontend/themes/gov-uk/client-js/cookieSettings.js +35 -23
- package/frontend/themes/gov-uk/styles/govuk.scss +0 -1
- package/frontend/toolkit/assets/javascript/form-focus.js +0 -4
- package/frontend/toolkit/assets/rebrand/images/govuk-logo.svg +25 -0
- package/index.js +4 -2
- package/package.json +2 -2
- package/sandbox/apps/sandbox/fields.js +1 -18
- package/sandbox/apps/sandbox/index.js +0 -4
- package/sandbox/apps/sandbox/sections/summary-data-sections.js +1 -7
- package/sandbox/apps/sandbox/translations/en/default.json +0 -33
- package/sandbox/apps/sandbox/translations/src/en/fields.json +0 -10
- package/sandbox/apps/sandbox/translations/src/en/pages.json +0 -3
- package/sandbox/apps/sandbox/translations/src/en/validation.json +0 -12
- package/sandbox/package.json +1 -1
- package/sandbox/public/css/app.css +2996 -1330
- package/sandbox/public/images/govuk-logo.svg +25 -0
- package/sandbox/public/js/bundle.js +4751 -2217
- package/sandbox/yarn.lock +4 -4
- package/components/amount-with-unit-select/amount-with-unit-select.js +0 -107
- package/components/amount-with-unit-select/fields.js +0 -15
- package/components/amount-with-unit-select/hooks.js +0 -168
- package/components/amount-with-unit-select/templates/amount-with-unit-select.html +0 -20
- package/components/amount-with-unit-select/utils.js +0 -191
- package/components/amount-with-unit-select/validation.js +0 -175
- package/frontend/template-mixins/partials/forms/grouped-inputs-select.html +0 -13
- package/frontend/template-mixins/partials/forms/grouped-inputs-text.html +0 -37
- package/frontend/themes/gov-uk/styles/_grouped-input.scss +0 -5
package/sandbox/yarn.lock
CHANGED
|
@@ -86,10 +86,10 @@ glob-parent@~5.1.2:
|
|
|
86
86
|
dependencies:
|
|
87
87
|
is-glob "^4.0.1"
|
|
88
88
|
|
|
89
|
-
govuk-frontend@
|
|
90
|
-
version "
|
|
91
|
-
resolved "https://registry.yarnpkg.com/govuk-frontend/-/govuk-frontend-
|
|
92
|
-
integrity sha512-
|
|
89
|
+
govuk-frontend@4.10:
|
|
90
|
+
version "4.10.1"
|
|
91
|
+
resolved "https://registry.yarnpkg.com/govuk-frontend/-/govuk-frontend-4.10.1.tgz#a67a680f34339c210d59ed0f6c473af29c975744"
|
|
92
|
+
integrity sha512-k7wKjPpLovQlfOiBt0A3RHQCpNagOR8xpfMgvTEQkX6Id5njYNzQTsBvpArDKAxI2174csv2uzn02py02zTVLA==
|
|
93
93
|
|
|
94
94
|
has-flag@^3.0.0:
|
|
95
95
|
version "3.0.0"
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const hooks = require('./hooks');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const getFields = require('./fields');
|
|
6
|
-
|
|
7
|
-
const TEMPLATE = path.resolve(__dirname, './templates/amount-with-unit-select.html');
|
|
8
|
-
|
|
9
|
-
module.exports = (key, opts) => {
|
|
10
|
-
if (!key) {
|
|
11
|
-
throw new Error('Key must be passed to amountWithUnitSelect component');
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const fields = getFields(key); // the child field definitions and configurations
|
|
15
|
-
const options = opts || {}; // the component's configuration options
|
|
16
|
-
const template = options.template ? // the field template path
|
|
17
|
-
path.resolve(__dirname, options.template) :
|
|
18
|
-
TEMPLATE;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Pre-process hook.
|
|
22
|
-
* @param {Object} req - The form's request object.
|
|
23
|
-
* @param {Object} res - The form's response object.
|
|
24
|
-
* @param {Function} next - The next middleware function in the chain.
|
|
25
|
-
*/
|
|
26
|
-
const preProcess = (req, res, next) => {
|
|
27
|
-
hooks.preProcess(req, fields, key);
|
|
28
|
-
next();
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Post-process hook.
|
|
33
|
-
* @param {Object} req - The form's request object.
|
|
34
|
-
* @param {Object} res - The form's response object.
|
|
35
|
-
* @param {Function} next - The next middleware function in the chain.
|
|
36
|
-
*/
|
|
37
|
-
const postProcess = (req, res, next) => {
|
|
38
|
-
hooks.postProcess(req, key);
|
|
39
|
-
next();
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Pre-validate hook.
|
|
44
|
-
* @param {Object} req - The form's request object.
|
|
45
|
-
* @param {Object} res - The form's response object.
|
|
46
|
-
* @param {Function} next - The next middleware function in the chain.
|
|
47
|
-
*/
|
|
48
|
-
const preValidate = (req, res, next) => {
|
|
49
|
-
hooks.preValidate(req, fields, key, options);
|
|
50
|
-
next();
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Pre-getErrors hook.
|
|
55
|
-
* @param {Object} req - The form's request object.
|
|
56
|
-
* @param {Object} res - The form's response object.
|
|
57
|
-
* @param {Function} next - The next middleware function in the chain.
|
|
58
|
-
*/
|
|
59
|
-
const preGetErrors = (req, res, next) => {
|
|
60
|
-
hooks.preGetErrors(req, fields, key);
|
|
61
|
-
next();
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Post-getErrors hook.
|
|
66
|
-
* @param {Object} req - The form's request object.
|
|
67
|
-
* @param {Object} res - The form's response object.
|
|
68
|
-
* @param {Function} next - The next middleware function in the chain.
|
|
69
|
-
*/
|
|
70
|
-
const postGetErrors = (req, res, next) => {
|
|
71
|
-
hooks.postGetErrors(req, res, fields, key);
|
|
72
|
-
next();
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Post-getValues hook.
|
|
77
|
-
* @param {Object} req - The form's request object.
|
|
78
|
-
* @param {Object} res - The form's response object.
|
|
79
|
-
* @param {Function} next - The next middleware function in the chain.
|
|
80
|
-
*/
|
|
81
|
-
const postGetValues = (req, res, next) => {
|
|
82
|
-
hooks.postGetValues(req, fields, key);
|
|
83
|
-
next();
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Pre-render hook.
|
|
88
|
-
* @param {Object} req - The form's request object.
|
|
89
|
-
* @param {Object} res - The form's response object.
|
|
90
|
-
* @param {Function} next - The next middleware function in the chain.
|
|
91
|
-
*/
|
|
92
|
-
const preRender = (req, res, next) => {
|
|
93
|
-
hooks.preRender(req, res, fields, options, template, key, next);
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
return Object.assign({}, options, {
|
|
97
|
-
hooks: {
|
|
98
|
-
'pre-process': preProcess,
|
|
99
|
-
'post-process': postProcess,
|
|
100
|
-
'pre-validate': preValidate,
|
|
101
|
-
'pre-getErrors': preGetErrors,
|
|
102
|
-
'post-getErrors': postGetErrors,
|
|
103
|
-
'post-getValues': postGetValues,
|
|
104
|
-
'pre-render': preRender
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
};
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const _ = require('lodash');
|
|
4
|
-
const utils = require('./utils');
|
|
5
|
-
const validation = require('./validation');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Pre-process hook. This function:
|
|
9
|
-
* - Splits the amountWithUnitSelect value into its parts (amount and unit in the format [Amount]-[Unit],
|
|
10
|
-
* E.G. 5-Kilograms) and assigns them to the request body (req.body).
|
|
11
|
-
* @param {Object} req - The form's request object.
|
|
12
|
-
* @param {Object} fields - The component's child field definitions and configurations.
|
|
13
|
-
* @param {string} key - The parent component's key.
|
|
14
|
-
*/
|
|
15
|
-
const preProcess = (req, fields, key) => {
|
|
16
|
-
const parts = utils.getParts(req.body, fields, key);
|
|
17
|
-
if (_.some(parts, part => part !== '')) {
|
|
18
|
-
req.body[key] = `${(parts.amount || '')}-${(parts.unit || '')}`;
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Post-process hook. This function:
|
|
24
|
-
* - Copies the field value from the request body (req.body) to the form values (req.form.values)
|
|
25
|
-
* if a reference to the field exists in the form values.
|
|
26
|
-
* @param {Object} req - The form's request object.
|
|
27
|
-
* @param {string} key - The parent component's key.
|
|
28
|
-
*/
|
|
29
|
-
const postProcess = (req, key) => {
|
|
30
|
-
if (req.form.values[key]) {
|
|
31
|
-
req.form.values[key] = req.body[key];
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Pre-validate hook. This function:
|
|
37
|
-
* - Prevents default select component assignment of 'equal' validator to the parent component.
|
|
38
|
-
* - Resolves required validators and optional configurations for child components.
|
|
39
|
-
* - Propagates child component field data and values to the request to enable their validation.
|
|
40
|
-
* - Adds a custom 'equal' validator to the unit child component.
|
|
41
|
-
* - Adds a custom 'twoHyphenSeparatedValues' validator to the parent component to validate overall value format.
|
|
42
|
-
* - Moves excess validators that do not apply to the parent component to the 'amount' child component.
|
|
43
|
-
* @param {Object} req - The form's request object.
|
|
44
|
-
* @param {Object} fields - The component's child field definitions and configurations.
|
|
45
|
-
* @param {string} key - The parent component's key.
|
|
46
|
-
* @param {Object} options - The component's configuration options.
|
|
47
|
-
*/
|
|
48
|
-
const preValidate = (req, fields, key, options) => {
|
|
49
|
-
// Prevents auto assignment of 'equal' validator to parent component
|
|
50
|
-
validation.addGroupedFieldsWithOptionsProperty(req.form.options.fields[key]);
|
|
51
|
-
// resolves required validators and optional configurations for child components
|
|
52
|
-
validation.resolveOptionalFields(req.form.options.fields, fields, options.validate, key);
|
|
53
|
-
// propagates child component field data and values to the request to enable validation
|
|
54
|
-
validation.propagateChildFieldValidation(req.form, fields, key);
|
|
55
|
-
// adds custom 'equal' validator to the unit child component
|
|
56
|
-
validation.addValidator(fields[`${key}-unit`], validation.createCustomEqualValidator(fields[`${key}-unit`].options));
|
|
57
|
-
// adds custom 'twoHyphenSeparatedValues' validator to the parent component to validate overall value format
|
|
58
|
-
validation.addValidator(options, validation.isTwoHyphenSeparatedValues);
|
|
59
|
-
// moves excess validators that do not apply to the parent component to the 'amount' child component
|
|
60
|
-
validation.moveExcessValidatorToChildComponent(req.form.options.fields, fields, key);
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Pre-getErrors hook. This function:
|
|
65
|
-
* - If the parent component has a flagged error, this extends the session model's error values with
|
|
66
|
-
* the child components' error values.
|
|
67
|
-
* @param {Object} req - The form's request object.
|
|
68
|
-
* @param {Object} fields - The component's child field definitions and configurations.
|
|
69
|
-
* @param {string} key - The parent component's key.
|
|
70
|
-
*/
|
|
71
|
-
const preGetErrors = (req, fields, key) => {
|
|
72
|
-
// if the amountWithUnitSelect field is included in errorValues (E.G. if there was a validation error),
|
|
73
|
-
// extend errorValues with the individual components
|
|
74
|
-
// (I.E. add the child components' K:V pair to the request sessionModel's attributes)
|
|
75
|
-
const errorValues = req.sessionModel.get('errorValues');
|
|
76
|
-
if (errorValues && errorValues[key]) {
|
|
77
|
-
req.sessionModel.set('errorValues',
|
|
78
|
-
Object.assign({}, errorValues, utils.getPartsFromAmountWithUnitSelect(errorValues[key], Object.keys(fields)))
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Post-getErrors hook. This function:
|
|
85
|
-
* - Ensures only one error is associated with the components in the request form errors
|
|
86
|
-
* (by setting excess errors' type to null) when either the parent or child components have
|
|
87
|
-
* (jointly) multiple errors in the session model.
|
|
88
|
-
* - If there is no parent component error, one of the child component errors (if any) is inserted.
|
|
89
|
-
* @param {Object} req - The form's request object.
|
|
90
|
-
* @param {Object} res - The form's response object.
|
|
91
|
-
* @param {Object} fields - The component's child field definitions and configurations.
|
|
92
|
-
* @param {string} key - The parent component's key.
|
|
93
|
-
*/
|
|
94
|
-
const postGetErrors = (req, res, fields, key) => {
|
|
95
|
-
// if the amountWithUnitSelect field or its child fields have any recorded validation error,
|
|
96
|
-
// the remaining errors are added to req.form.errors
|
|
97
|
-
// and their type is set to null to avoid duplicate error messages
|
|
98
|
-
const errors = req.sessionModel.get('errors');
|
|
99
|
-
if (errors && (errors[key] || errors[`${key}-amount`] || errors[`${key}-unit`])) {
|
|
100
|
-
Object.assign(req.form.errors, Object.keys(fields).reduce((obj, field) =>
|
|
101
|
-
Object.assign({}, obj, { [field]: { type: null } })
|
|
102
|
-
, {}));
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// inserts child component validation errors into req.form.errors
|
|
106
|
-
validation.insertChildValidationErrors(req, res, errors);
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Post-getValues hook. This function:
|
|
111
|
-
* - Splits the component's value into its parts and assigns them to the request object's form values (req.form.values).
|
|
112
|
-
* @param {Object} req - The form's request object.
|
|
113
|
-
* @param {Object} fields - The component's child field definitions and configurations.
|
|
114
|
-
* @param {string} key - The parent component's key.
|
|
115
|
-
*/
|
|
116
|
-
const postGetValues = (req, fields, key) => {
|
|
117
|
-
// if amountWithUnitSelect value is set, split it into its parts and assign to req.form.values
|
|
118
|
-
// extends the session model's error values, if any
|
|
119
|
-
const amountWithUnitSelect = req.form.values[key];
|
|
120
|
-
if (amountWithUnitSelect) {
|
|
121
|
-
Object.assign(
|
|
122
|
-
req.form.values,
|
|
123
|
-
utils.getPartsFromAmountWithUnitSelect(amountWithUnitSelect, Object.keys(fields)),
|
|
124
|
-
req.sessionModel.get('errorValues') || {}
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Pre-render hook. This function:
|
|
131
|
-
* - Translates the unit options and child component labels.
|
|
132
|
-
* - Renders the component's template to a string
|
|
133
|
-
* and assigns the HTML output to the component field in res.locals.fields.
|
|
134
|
-
* @param {Object} req - The form's request object.
|
|
135
|
-
* @param {Object} res - The form's response object.
|
|
136
|
-
* @param {Object} fields - The component's child field definitions and configurations.
|
|
137
|
-
* @param {Object} options - The component's configuration options.
|
|
138
|
-
* @param {string} template - The component's template path.
|
|
139
|
-
* @param {string} key - The parent component's key.
|
|
140
|
-
* @param {Function} next - The next middleware function in the chain.
|
|
141
|
-
*/
|
|
142
|
-
const preRender = (req, res, fields, options, template, key, next) => {
|
|
143
|
-
// applies translations
|
|
144
|
-
utils.translateUnitOptions(req, fields, options, key);
|
|
145
|
-
utils.translateLabels(req, fields, key, ['amount', 'unit']);
|
|
146
|
-
|
|
147
|
-
// renders the template to a string and assign the html output
|
|
148
|
-
// to the amountWithUnitSelect field (in res.locals.fields)
|
|
149
|
-
res.render(template, utils.constructFieldToRender(req, fields, options, key), (err, html) => {
|
|
150
|
-
if (err) {
|
|
151
|
-
next(err);
|
|
152
|
-
} else {
|
|
153
|
-
const field = res.locals.fields.find(f => f.key === key);
|
|
154
|
-
Object.assign(field, { html });
|
|
155
|
-
next();
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
module.exports = {
|
|
161
|
-
preProcess,
|
|
162
|
-
postProcess,
|
|
163
|
-
preValidate,
|
|
164
|
-
preGetErrors,
|
|
165
|
-
postGetErrors,
|
|
166
|
-
postGetValues,
|
|
167
|
-
preRender
|
|
168
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
<div class="govuk-form-group {{#error}}govuk-form-group--error{{/error}}">
|
|
2
|
-
<fieldset id="{{key}}-group" class="govuk-fieldset{{#className}} {{className}}{{/className}}" role="group">
|
|
3
|
-
<legend class="govuk-fieldset__legend {{#isPageHeading}}govuk-fieldset__legend--l{{/isPageHeading}}{{#legendClassName}} {{legendClassName}}{{/legendClassName}}">
|
|
4
|
-
{{#isPageHeading}}<h1 class="govuk-fieldset__heading">{{/isPageHeading}}
|
|
5
|
-
{{legend}}
|
|
6
|
-
{{#isPageHeading}}</h1>{{/isPageHeading}}
|
|
7
|
-
</legend>
|
|
8
|
-
{{#hint}}
|
|
9
|
-
<span id="{{key}}-hint" class="govuk-hint">{{hint}}</span>
|
|
10
|
-
{{/hint}}
|
|
11
|
-
{{#error}}
|
|
12
|
-
<p id="{{key}}-error" class="govuk-error-message">
|
|
13
|
-
<span class="govuk-visually-hidden">Error:</span> {{error.message}}
|
|
14
|
-
</p>
|
|
15
|
-
{{/error}}
|
|
16
|
-
<div id="{{key}}" class="govuk-amount-with-unit-select-input">
|
|
17
|
-
{{#input-amount-with-unit-select}}{{key}}{{/input-amount-with-unit-select}}
|
|
18
|
-
</div>
|
|
19
|
-
</fieldset>
|
|
20
|
-
</div>
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const _ = require('lodash');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Translates a field property (given its key and a translation function),
|
|
7
|
-
* returning null if there is no translation (enabling conditional statements to branch if there is no translation).
|
|
8
|
-
* @param {string} key - The key of the field property to translate
|
|
9
|
-
* (E.G. 'fields.amountWithUnitSelect-amount.label' - references the amount field label).
|
|
10
|
-
* @param {function(string): string} translate - The translation function to apply.
|
|
11
|
-
* @returns {string|null} Returns the translation or null if there is no translation.
|
|
12
|
-
*/
|
|
13
|
-
const conditionalTranslate = (key, translate) => {
|
|
14
|
-
let result = translate(key);
|
|
15
|
-
if (result === key) {
|
|
16
|
-
result = null;
|
|
17
|
-
}
|
|
18
|
-
return result;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Gets the classname specified for the field's legend.
|
|
23
|
-
* Defaults to an empty string if not specified.
|
|
24
|
-
* @param {object} field - The field with (potentially) the legend classname.
|
|
25
|
-
* @returns {string} The classname specified for the field's legend text.
|
|
26
|
-
*/
|
|
27
|
-
const getLegendClassName = field =>
|
|
28
|
-
field?.legend?.className || '';
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Gets a boolean determining if the field's heading has been set to be the page heading.
|
|
32
|
-
* @param {Object} field - The field with the heading to (potentially) make the page title.
|
|
33
|
-
* @returns {string} A boolean that indicates if the field's heading is also the page heading.
|
|
34
|
-
*/
|
|
35
|
-
const getIsPageHeading = field =>
|
|
36
|
-
field?.isPageHeading || '';
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Creates a new object with the component's assigned 'amount' and 'unit' values.
|
|
40
|
-
* It does this by looking for the 'amountWithUnitSelect-amount' and 'amountWithUnitSelect-unit' fields in req.body
|
|
41
|
-
* and making a new object that copies those 2 fields and values, and removes the 'amountWithUnitSelect-' suffix.
|
|
42
|
-
* @param {Object} body - The submitted request's body (req.body) containing K:V pairs
|
|
43
|
-
* (E.G. 'amountWithUnitSelect-amount : 12').
|
|
44
|
-
* @param {Object} fields - The set of fields relevant to this component (I.E. fields defined in ./fields.js).
|
|
45
|
-
* @param {string} key - The grouped child fields' parent name/key ('amountWithUnitSelect' in this case).
|
|
46
|
-
* @returns {{ amount: string, unit: string }} Returns a map of values in the format { amount: '1', unit: 'litres' }.
|
|
47
|
-
*/
|
|
48
|
-
const getParts = (body, fields, key) =>
|
|
49
|
-
_.mapKeys(_.pick(body, Object.keys(fields)), (value, fieldKey) =>
|
|
50
|
-
fieldKey.replace(`${key}-`, '')
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Splits the AmountWithUnitSelect value (usually in the format '[Amount]-[Unit]') by the last hyphen in the text
|
|
55
|
-
* into an Array with 2 elements (the amount and unit - the value before and after the hyphen respectively).
|
|
56
|
-
* Returns an empty string for each element if in an unexpected format.
|
|
57
|
-
* @param {string} amountWithUnitSelectVal - The amountWithUnitSelect value (E.G. '1-Litre').
|
|
58
|
-
* @returns {string[]} Returns an array in format [amount, unit].
|
|
59
|
-
*/
|
|
60
|
-
const getAmountWithUnitSelectValues = amountWithUnitSelectVal => {
|
|
61
|
-
const splitPointIndex = amountWithUnitSelectVal.lastIndexOf('-');
|
|
62
|
-
return splitPointIndex === -1 ?
|
|
63
|
-
['', ''] :
|
|
64
|
-
[amountWithUnitSelectVal.substring(0, splitPointIndex), amountWithUnitSelectVal.substring(splitPointIndex + 1)];
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Returns a map of the component's fields' keys and their assigned values.
|
|
69
|
-
* (E.G. If amountWithUnitSelectVal = 1-L,
|
|
70
|
-
* it returns { amountWithUnitSelect-amount: '1', amountWithUnitSelect-unit: 'L' }).
|
|
71
|
-
* @param {string} amountWithUnitSelectVal - AmountWithUnitSelect value in the format '[Amount]-[Unit]'.
|
|
72
|
-
* @param {Object} fields - The component's child field definitions and configurations.
|
|
73
|
-
* @returns {amountWithUnitSelect-amount: string, amountWithUnitSelect-unit: string} Returns a map of K:V pairs
|
|
74
|
-
* for each child field.
|
|
75
|
-
*/
|
|
76
|
-
const getPartsFromAmountWithUnitSelect = (amountWithUnitSelectVal, fields) =>
|
|
77
|
-
getAmountWithUnitSelectValues(amountWithUnitSelectVal).reduce(
|
|
78
|
-
(obj, value, index) => Object.assign({},
|
|
79
|
-
obj,
|
|
80
|
-
{[fields[index]]: value }),
|
|
81
|
-
{});
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Translates the labels of the child fields of the AmountWithUnitSelect component.
|
|
85
|
-
* Depending on which exists, labels are set in the following order of precedence:
|
|
86
|
-
* 1. A specific child-component translation (I.E. amountWithUnitSelect-amount.label).
|
|
87
|
-
* 2. A parent-component translation for the child field (I.E. amountWithUnitSelect.amountLabel).
|
|
88
|
-
* 3. A specific child-component (non-translated) label configuration.
|
|
89
|
-
* 4. A parent-component (non-translated) label configuration for the child field.
|
|
90
|
-
* @param {Object} req - The form's request object.
|
|
91
|
-
* @param {Object} fields - The component's child field definitions and configurations.
|
|
92
|
-
* @param {string} pKey - The parent component's key (E.G. 'amountWithUnitSelect').
|
|
93
|
-
* @param {string[]} keys - The list of child components (by keys) to translate (E.G. ['amount', 'unit']).
|
|
94
|
-
*/
|
|
95
|
-
const translateLabels = (req, fields, pKey, keys) => {
|
|
96
|
-
keys.forEach(key => {
|
|
97
|
-
fields[`${pKey}-${key}`].label =
|
|
98
|
-
conditionalTranslate(`fields.${pKey}-${key}.label`, req.translate) ||
|
|
99
|
-
conditionalTranslate(`fields.${pKey}.${key}Label`, req.translate) ||
|
|
100
|
-
fields[`${pKey}-${key}`].label ||
|
|
101
|
-
req.form.options.fields[`${pKey}`]?.[`${key}Label`];
|
|
102
|
-
});
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Adds the component's child fields to the request's form options fields (req.form.options.fields).
|
|
107
|
-
* @param {Object} req - The form's request object.
|
|
108
|
-
* @param {Object} fields - The component's child field definitions and configurations.
|
|
109
|
-
* @param {string} key - The parent component's key (E.G. 'amountWithUnitSelect').
|
|
110
|
-
*/
|
|
111
|
-
const addChildFieldsToRequestForm = (req, fields, key) => {
|
|
112
|
-
Object.assign(req.form.options.fields, _.mapValues(fields, (v, k) => {
|
|
113
|
-
const rawKey = k.replace(`${key}-`, '');
|
|
114
|
-
const labelKey = `fields.${key}.parts.${rawKey}`;
|
|
115
|
-
const label = req.translate(labelKey);
|
|
116
|
-
|
|
117
|
-
return Object.assign({}, v, {
|
|
118
|
-
label: label === labelKey ? v.label : label
|
|
119
|
-
});
|
|
120
|
-
}));
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Sets a default null option for a select component if undefined.
|
|
125
|
-
* The default null option has a label of 'Select...' and a value of an empty string.
|
|
126
|
-
* @param {Object[]} options - The dropdown menu options.
|
|
127
|
-
* @returns {Object[]} Returns the options with the null/default option resolved.
|
|
128
|
-
*/
|
|
129
|
-
const resolveNullOption = options => {
|
|
130
|
-
const nullOptionLabel = options.find(opt => opt.null !== undefined && Object.keys(opt).length === 1);
|
|
131
|
-
const nonNullOptions = options.filter(opt => opt.null === undefined || Object.keys(opt).length !== 1);
|
|
132
|
-
|
|
133
|
-
return [{label: (nullOptionLabel !== undefined ? nullOptionLabel.null : 'Select...'), value: ''}]
|
|
134
|
-
.concat(nonNullOptions);
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Translates the unit field's/component's options (dropdown menu options) for the AmountWithUnitSelect component.
|
|
139
|
-
* Also resolves the null/default select option's label and value.
|
|
140
|
-
* @param {Object} req - The form's request object.
|
|
141
|
-
* @param {Object} fields - The component's child field definitions and configurations.
|
|
142
|
-
* @param {Object} options - The component's configuration options.
|
|
143
|
-
* @param {String} key - The parent component's key (E.G. 'amountWithUnitSelect').
|
|
144
|
-
*/
|
|
145
|
-
const translateUnitOptions = (req, fields, options, key) => {
|
|
146
|
-
// sets unit options as either translations (if they exist) or default untranslated options
|
|
147
|
-
const optionsToDisplay = conditionalTranslate(`fields.${key}-unit.options`, req.translate) ||
|
|
148
|
-
conditionalTranslate(`fields.${key}.options`, req.translate) ||
|
|
149
|
-
options.options;
|
|
150
|
-
|
|
151
|
-
// resolves the null/default select option
|
|
152
|
-
fields[`${key}-unit`].options = resolveNullOption(optionsToDisplay);
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Constructs an object with field data required to render the AmountWithUnitSelect component.
|
|
157
|
-
* @param {Object} req - The form's request object.
|
|
158
|
-
* @param {Object} fields - The component's child field definitions and configurations.
|
|
159
|
-
* @param {Object} options - The component's configuration options.
|
|
160
|
-
* @param {string} key - The parent component's key.
|
|
161
|
-
* @returns {Object} Returns an object with field data required to render the component.
|
|
162
|
-
*/
|
|
163
|
-
const constructFieldToRender = (req, fields, options, key) => {
|
|
164
|
-
addChildFieldsToRequestForm(req, fields, key);
|
|
165
|
-
|
|
166
|
-
const reqForm = req.form;
|
|
167
|
-
const legend = conditionalTranslate(`fields.${key}.legend`, req.translate) ||
|
|
168
|
-
reqForm.options.fields[`${key}`]?.legend;
|
|
169
|
-
const hint = conditionalTranslate(`fields.${key}.hint`, req.translate) ||
|
|
170
|
-
reqForm.options.fields[`${key}`]?.hint;
|
|
171
|
-
const legendClassName = getLegendClassName(options);
|
|
172
|
-
const isPageHeading = getIsPageHeading(options);
|
|
173
|
-
const error = reqForm.errors &&
|
|
174
|
-
(reqForm.errors[key] || reqForm.errors[`${key}-amount`] || reqForm.errors[`${key}-unit`]);
|
|
175
|
-
|
|
176
|
-
return { key, legend, legendClassName, isPageHeading, hint, error };
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
module.exports = {
|
|
180
|
-
conditionalTranslate,
|
|
181
|
-
getLegendClassName,
|
|
182
|
-
getIsPageHeading,
|
|
183
|
-
getParts,
|
|
184
|
-
getAmountWithUnitSelectValues,
|
|
185
|
-
getPartsFromAmountWithUnitSelect,
|
|
186
|
-
translateLabels,
|
|
187
|
-
addChildFieldsToRequestForm,
|
|
188
|
-
resolveNullOption,
|
|
189
|
-
translateUnitOptions,
|
|
190
|
-
constructFieldToRender
|
|
191
|
-
};
|