@defra/forms-engine-plugin 4.0.6 → 4.0.8
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/.public/stylesheets/application.min.css +1 -1
- package/.public/stylesheets/application.min.css.map +1 -1
- package/.server/client/stylesheets/_location-input.scss +60 -0
- package/.server/client/stylesheets/application.scss +1 -0
- package/.server/client/stylesheets/shared.scss +1 -0
- package/.server/server/forms/components.json +7 -0
- package/.server/server/forms/register-as-a-unicorn-breeder.yaml +40 -1
- package/.server/server/plugins/engine/components/ComponentBase.d.ts +2 -2
- package/.server/server/plugins/engine/components/ComponentBase.js.map +1 -1
- package/.server/server/plugins/engine/components/ComponentCollection.js.map +1 -1
- package/.server/server/plugins/engine/components/DeclarationField.d.ts +81 -0
- package/.server/server/plugins/engine/components/DeclarationField.js +123 -0
- package/.server/server/plugins/engine/components/DeclarationField.js.map +1 -0
- package/.server/server/plugins/engine/components/EastingNorthingField.d.ts +121 -0
- package/.server/server/plugins/engine/components/EastingNorthingField.js +166 -0
- package/.server/server/plugins/engine/components/EastingNorthingField.js.map +1 -0
- package/.server/server/plugins/engine/components/LatLongField.d.ts +121 -0
- package/.server/server/plugins/engine/components/LatLongField.js +164 -0
- package/.server/server/plugins/engine/components/LatLongField.js.map +1 -0
- package/.server/server/plugins/engine/components/LocationFieldBase.d.ts +134 -0
- package/.server/server/plugins/engine/components/LocationFieldBase.js +85 -0
- package/.server/server/plugins/engine/components/LocationFieldBase.js.map +1 -0
- package/.server/server/plugins/engine/components/LocationFieldHelpers.d.ts +108 -0
- package/.server/server/plugins/engine/components/LocationFieldHelpers.js +96 -0
- package/.server/server/plugins/engine/components/LocationFieldHelpers.js.map +1 -0
- package/.server/server/plugins/engine/components/NationalGridFieldNumberField.d.ts +19 -0
- package/.server/server/plugins/engine/components/NationalGridFieldNumberField.js +40 -0
- package/.server/server/plugins/engine/components/NationalGridFieldNumberField.js.map +1 -0
- package/.server/server/plugins/engine/components/OsGridRefField.d.ts +19 -0
- package/.server/server/plugins/engine/components/OsGridRefField.js +56 -0
- package/.server/server/plugins/engine/components/OsGridRefField.js.map +1 -0
- package/.server/server/plugins/engine/components/helpers/components.d.ts +3 -4
- package/.server/server/plugins/engine/components/helpers/components.js +24 -29
- package/.server/server/plugins/engine/components/helpers/components.js.map +1 -1
- package/.server/server/plugins/engine/components/index.d.ts +5 -0
- package/.server/server/plugins/engine/components/index.js +5 -0
- package/.server/server/plugins/engine/components/index.js.map +1 -1
- package/.server/server/plugins/engine/components/markdownParser.d.ts +2 -0
- package/.server/server/plugins/engine/components/markdownParser.js +28 -0
- package/.server/server/plugins/engine/components/markdownParser.js.map +1 -0
- package/.server/server/plugins/engine/components/types.d.ts +10 -0
- package/.server/server/plugins/engine/components/types.js.map +1 -1
- package/.server/server/plugins/engine/pageControllers/helpers/pages.js +7 -0
- package/.server/server/plugins/engine/pageControllers/helpers/pages.js.map +1 -1
- package/.server/server/plugins/engine/pageControllers/validationOptions.js +1 -0
- package/.server/server/plugins/engine/pageControllers/validationOptions.js.map +1 -1
- package/.server/server/plugins/engine/types/index.d.ts +1 -1
- package/.server/server/plugins/engine/types/index.js.map +1 -1
- package/.server/server/plugins/engine/types.d.ts +2 -2
- package/.server/server/plugins/engine/types.js.map +1 -1
- package/.server/server/plugins/engine/views/components/_location-field-base.html +53 -0
- package/.server/server/plugins/engine/views/components/declarationfield.html +14 -0
- package/.server/server/plugins/engine/views/components/eastingnorthingfield.html +5 -0
- package/.server/server/plugins/engine/views/components/latlongfield.html +5 -0
- package/.server/server/plugins/engine/views/components/nationalgridfieldnumberfield.html +13 -0
- package/.server/server/plugins/engine/views/components/osgridreffield.html +13 -0
- package/.server/server/plugins/nunjucks/filters/field.d.ts +1 -1
- package/.server/server/plugins/nunjucks/filters/index.d.ts +1 -0
- package/.server/server/plugins/nunjucks/filters/index.js +1 -0
- package/.server/server/plugins/nunjucks/filters/index.js.map +1 -1
- package/.server/server/plugins/nunjucks/filters/merge.d.ts +7 -0
- package/.server/server/plugins/nunjucks/filters/merge.js +16 -0
- package/.server/server/plugins/nunjucks/filters/merge.js.map +1 -0
- package/.server/server/plugins/nunjucks/filters/merge.test.js +19 -0
- package/.server/server/plugins/nunjucks/filters/merge.test.js.map +1 -0
- package/package.json +3 -3
- package/src/client/stylesheets/_location-input.scss +60 -0
- package/src/client/stylesheets/application.scss +1 -0
- package/src/client/stylesheets/shared.scss +1 -0
- package/src/server/forms/components.json +7 -0
- package/src/server/forms/page-events.yaml +1 -1
- package/src/server/forms/register-as-a-unicorn-breeder.yaml +40 -1
- package/src/server/index.test.ts +1 -0
- package/src/server/plugins/engine/components/ComponentBase.ts +2 -1
- package/src/server/plugins/engine/components/ComponentCollection.ts +1 -0
- package/src/server/plugins/engine/components/DeclarationField.test.ts +426 -0
- package/src/server/plugins/engine/components/DeclarationField.ts +167 -0
- package/src/server/plugins/engine/components/EastingNorthingField.test.ts +665 -0
- package/src/server/plugins/engine/components/EastingNorthingField.ts +224 -0
- package/src/server/plugins/engine/components/LatLongField.test.ts +700 -0
- package/src/server/plugins/engine/components/LatLongField.ts +213 -0
- package/src/server/plugins/engine/components/LocationFieldBase.test.ts +253 -0
- package/src/server/plugins/engine/components/LocationFieldBase.ts +152 -0
- package/src/server/plugins/engine/components/LocationFieldHelpers.test.ts +338 -0
- package/src/server/plugins/engine/components/LocationFieldHelpers.ts +123 -0
- package/src/server/plugins/engine/components/NationalGridFieldNumberField.test.ts +438 -0
- package/src/server/plugins/engine/components/NationalGridFieldNumberField.ts +52 -0
- package/src/server/plugins/engine/components/OsGridRefField.test.ts +469 -0
- package/src/server/plugins/engine/components/OsGridRefField.ts +71 -0
- package/src/server/plugins/engine/components/helpers/components.test.ts +270 -0
- package/src/server/plugins/engine/components/helpers/components.ts +44 -47
- package/src/server/plugins/engine/components/helpers/helpers.test.ts +71 -1
- package/src/server/plugins/engine/components/index.ts +5 -0
- package/src/server/plugins/engine/components/markdownParser.ts +40 -0
- package/src/server/plugins/engine/components/types.ts +14 -0
- package/src/server/plugins/engine/models/SummaryViewModel.test.ts +76 -3
- package/src/server/plugins/engine/models/SummaryViewModel.ts +5 -1
- package/src/server/plugins/engine/outputFormatters/adapter/v1.location.test.ts +356 -0
- package/src/server/plugins/engine/pageControllers/helpers/helpers.test.ts +4 -0
- package/src/server/plugins/engine/pageControllers/helpers/pages.ts +8 -0
- package/src/server/plugins/engine/pageControllers/validationOptions.ts +4 -0
- package/src/server/plugins/engine/types/index.ts +2 -0
- package/src/server/plugins/engine/types.ts +4 -0
- package/src/server/plugins/engine/views/components/_location-field-base.html +53 -0
- package/src/server/plugins/engine/views/components/declarationfield.html +14 -0
- package/src/server/plugins/engine/views/components/eastingnorthingfield.html +5 -0
- package/src/server/plugins/engine/views/components/latlongfield.html +5 -0
- package/src/server/plugins/engine/views/components/nationalgridfieldnumberfield.html +13 -0
- package/src/server/plugins/engine/views/components/osgridreffield.html +13 -0
- package/src/server/plugins/nunjucks/filters/index.js +1 -0
- package/src/server/plugins/nunjucks/filters/merge.js +16 -0
- package/src/server/plugins/nunjucks/filters/merge.test.js +15 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
@use "govuk-frontend" as *;
|
|
2
|
+
|
|
3
|
+
.app-location-input {
|
|
4
|
+
@include govuk-clearfix;
|
|
5
|
+
font-size: 0; // removes whitespace caused by inline-block
|
|
6
|
+
margin-bottom: govuk-spacing(6);
|
|
7
|
+
|
|
8
|
+
&:has(.govuk-input--error) {
|
|
9
|
+
border-left: $govuk-border-width-form-group-error solid $govuk-error-colour;
|
|
10
|
+
padding-left: govuk-spacing(3);
|
|
11
|
+
margin-top: 0;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.govuk-hint:has(+ .app-location-input .govuk-input--error) {
|
|
16
|
+
border-left: $govuk-border-width-form-group-error solid $govuk-error-colour;
|
|
17
|
+
padding-left: govuk-spacing(3);
|
|
18
|
+
margin-bottom: 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.govuk-fieldset:has(.app-location-input .govuk-input--error) {
|
|
22
|
+
.govuk-fieldset__legend {
|
|
23
|
+
border-left: $govuk-border-width-form-group-error solid $govuk-error-colour;
|
|
24
|
+
padding-left: govuk-spacing(3);
|
|
25
|
+
margin-bottom: 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.govuk-fieldset__legend + .govuk-hint {
|
|
29
|
+
margin-top: 0;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.app-location-input__item {
|
|
34
|
+
display: inline-block;
|
|
35
|
+
margin-right: govuk-spacing(4);
|
|
36
|
+
margin-bottom: govuk-spacing(4);
|
|
37
|
+
|
|
38
|
+
&:last-child {
|
|
39
|
+
margin-right: 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@include govuk-media-query($from: tablet) {
|
|
43
|
+
margin-bottom: 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.govuk-form-group {
|
|
47
|
+
margin-bottom: 0;
|
|
48
|
+
display: inline-block;
|
|
49
|
+
width: auto;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.govuk-label {
|
|
53
|
+
display: block;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.govuk-input {
|
|
57
|
+
margin-bottom: 0;
|
|
58
|
+
width: auto;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -120,6 +120,13 @@
|
|
|
120
120
|
"content": "### This is a H3 in markdown\n\n[An internal link](http://localhost:3009/fictional-page)\n\n[An external link](https://defra.gov.uk/fictional-page)",
|
|
121
121
|
"options": {},
|
|
122
122
|
"schema": {}
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
"type": "DeclarationField",
|
|
126
|
+
"name": "declaration",
|
|
127
|
+
"title": "Declaration",
|
|
128
|
+
"content": "By submitting this form, I agree to:\n\n- Provide accurate and complete information\n- Comply with all applicable regulations\n- Accept responsibility for any false statements",
|
|
129
|
+
"hint": "Please read and confirm the following terms"
|
|
123
130
|
}
|
|
124
131
|
]
|
|
125
132
|
},
|
|
@@ -163,6 +163,34 @@ pages:
|
|
|
163
163
|
next:
|
|
164
164
|
- path: '/how-many-members-of-staff-will-look-after-the-unicorns'
|
|
165
165
|
components:
|
|
166
|
+
- name: dfGYuk
|
|
167
|
+
options: {}
|
|
168
|
+
schema: {}
|
|
169
|
+
type: EastingNorthingField
|
|
170
|
+
title: Easting and northing
|
|
171
|
+
hint:
|
|
172
|
+
This is an Easting and Northing component
|
|
173
|
+
- name: seTThb
|
|
174
|
+
options: {}
|
|
175
|
+
schema: {}
|
|
176
|
+
type: LatLongField
|
|
177
|
+
title: Latitute and longitude
|
|
178
|
+
hint:
|
|
179
|
+
This is an Latitute and Longitude component
|
|
180
|
+
- name: bhjloS
|
|
181
|
+
options: {}
|
|
182
|
+
schema: {}
|
|
183
|
+
type: NationalGridFieldNumberField
|
|
184
|
+
title: National grid field number
|
|
185
|
+
hint:
|
|
186
|
+
This is an National Grid Field Number component
|
|
187
|
+
- name: dfQQws
|
|
188
|
+
options: {}
|
|
189
|
+
schema: {}
|
|
190
|
+
type: OsGridRefField
|
|
191
|
+
title: Ordnance survey grid reference
|
|
192
|
+
hint:
|
|
193
|
+
This is an Ordnance survey Grid Reference component
|
|
166
194
|
- name: bClCvo
|
|
167
195
|
options: {}
|
|
168
196
|
schema: {}
|
|
@@ -191,7 +219,7 @@ pages:
|
|
|
191
219
|
controller: FileUploadPageController
|
|
192
220
|
section: Regnsa
|
|
193
221
|
next:
|
|
194
|
-
- path: '/
|
|
222
|
+
- path: '/declaration'
|
|
195
223
|
components:
|
|
196
224
|
- name: dLzALM
|
|
197
225
|
title: Documents
|
|
@@ -202,6 +230,17 @@ pages:
|
|
|
202
230
|
schema:
|
|
203
231
|
min: 1
|
|
204
232
|
max: 3
|
|
233
|
+
- title: Declaration
|
|
234
|
+
path: '/declaration'
|
|
235
|
+
section: section
|
|
236
|
+
components:
|
|
237
|
+
- name: diLmal
|
|
238
|
+
title: Declaration
|
|
239
|
+
type: DeclarationField
|
|
240
|
+
content: 'Fill in this field'
|
|
241
|
+
options: {}
|
|
242
|
+
next:
|
|
243
|
+
- path: '/summary'
|
|
205
244
|
conditions:
|
|
206
245
|
- displayName: Address is different
|
|
207
246
|
name: IrVmYz
|
|
@@ -13,7 +13,7 @@ export declare class ComponentBase {
|
|
|
13
13
|
name: ComponentDef['name'];
|
|
14
14
|
title: ComponentDef['title'];
|
|
15
15
|
schema?: Extract<ComponentDef, {
|
|
16
|
-
schema
|
|
16
|
+
schema?: object;
|
|
17
17
|
}>['schema'];
|
|
18
18
|
options?: Extract<ComponentDef, {
|
|
19
19
|
options: object;
|
|
@@ -30,4 +30,4 @@ export declare class ComponentBase {
|
|
|
30
30
|
});
|
|
31
31
|
get viewModel(): ViewModel;
|
|
32
32
|
}
|
|
33
|
-
export type ComponentSchema = ArraySchema<string> | ArraySchema<number> | ArraySchema<boolean> | ArraySchema<object> | BooleanSchema<string> | DateSchema | NumberSchema<string> | NumberSchema | ObjectSchema | StringSchema;
|
|
33
|
+
export type ComponentSchema = ArraySchema<string> | ArraySchema<number> | ArraySchema<boolean> | ArraySchema<object> | BooleanSchema<string> | BooleanSchema | DateSchema | NumberSchema<string> | NumberSchema | ObjectSchema | StringSchema;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ComponentBase.js","names":["isConditionalRevealType","joi","ComponentBase","page","parent","collection","type","name","title","schema","options","isFormComponent","model","formSchema","string","stateSchema","constructor","def","props","viewModel","attributes","autocomplete","classes","condition"],"sources":["../../../../../src/server/plugins/engine/components/ComponentBase.ts"],"sourcesContent":["import { isConditionalRevealType, type ComponentDef } from '@defra/forms-model'\nimport joi, {\n type ArraySchema,\n type BooleanSchema,\n type DateSchema,\n type NumberSchema,\n type ObjectSchema,\n type StringSchema\n} from 'joi'\n\nimport { type ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js'\nimport { type Component } from '~/src/server/plugins/engine/components/helpers/components.js'\nimport { type ViewModel } from '~/src/server/plugins/engine/components/types.js'\nimport { type FormModel } from '~/src/server/plugins/engine/models/index.js'\nimport { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers/pages.js'\n\nexport class ComponentBase {\n page?: PageControllerClass\n parent: Component | undefined\n collection: ComponentCollection | undefined\n\n type: ComponentDef['type']\n name: ComponentDef['name']\n title: ComponentDef['title']\n schema?: Extract<ComponentDef, { schema
|
|
1
|
+
{"version":3,"file":"ComponentBase.js","names":["isConditionalRevealType","joi","ComponentBase","page","parent","collection","type","name","title","schema","options","isFormComponent","model","formSchema","string","stateSchema","constructor","def","props","viewModel","attributes","autocomplete","classes","condition"],"sources":["../../../../../src/server/plugins/engine/components/ComponentBase.ts"],"sourcesContent":["import { isConditionalRevealType, type ComponentDef } from '@defra/forms-model'\nimport joi, {\n type ArraySchema,\n type BooleanSchema,\n type DateSchema,\n type NumberSchema,\n type ObjectSchema,\n type StringSchema\n} from 'joi'\n\nimport { type ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js'\nimport { type Component } from '~/src/server/plugins/engine/components/helpers/components.js'\nimport { type ViewModel } from '~/src/server/plugins/engine/components/types.js'\nimport { type FormModel } from '~/src/server/plugins/engine/models/index.js'\nimport { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers/pages.js'\n\nexport class ComponentBase {\n page?: PageControllerClass\n parent: Component | undefined\n collection: ComponentCollection | undefined\n\n type: ComponentDef['type']\n name: ComponentDef['name']\n title: ComponentDef['title']\n schema?: Extract<ComponentDef, { schema?: object }>['schema']\n options?: Extract<ComponentDef, { options: object }>['options']\n\n isFormComponent = false\n model: FormModel\n\n /** joi schemas based on a component defined in the form JSON. This validates a user's answer and is generated from {@link ComponentDef} */\n formSchema: ComponentSchema = joi.string()\n stateSchema: ComponentSchema = joi.string()\n\n constructor(\n def: ComponentDef,\n props: {\n page?: PageControllerClass\n parent?: Component\n model: FormModel\n }\n ) {\n this.type = def.type\n this.name = def.name\n this.title = def.title\n\n if ('schema' in def) {\n this.schema = def.schema\n }\n\n if ('options' in def) {\n this.options = def.options\n }\n\n this.page = props.page\n this.parent = props.parent\n this.model = props.model\n }\n\n get viewModel() {\n const { options, type } = this\n\n const viewModel: ViewModel = {\n attributes: {}\n }\n\n if (!options) {\n return viewModel\n }\n\n if ('autocomplete' in options) {\n viewModel.attributes.autocomplete = options.autocomplete\n }\n\n if ('classes' in options) {\n viewModel.classes = options.classes\n }\n\n if ('condition' in options && isConditionalRevealType(type)) {\n viewModel.condition = options.condition\n }\n\n return viewModel\n }\n}\n\nexport type ComponentSchema =\n | ArraySchema<string>\n | ArraySchema<number>\n | ArraySchema<boolean>\n | ArraySchema<object>\n | BooleanSchema<string>\n | BooleanSchema\n | DateSchema\n | NumberSchema<string>\n | NumberSchema\n | ObjectSchema\n | StringSchema\n"],"mappings":"AAAA,SAASA,uBAAuB,QAA2B,oBAAoB;AAC/E,OAAOC,GAAG,MAOH,KAAK;AAQZ,OAAO,MAAMC,aAAa,CAAC;EACzBC,IAAI;EACJC,MAAM;EACNC,UAAU;EAEVC,IAAI;EACJC,IAAI;EACJC,KAAK;EACLC,MAAM;EACNC,OAAO;EAEPC,eAAe,GAAG,KAAK;EACvBC,KAAK;;EAEL;EACAC,UAAU,GAAoBZ,GAAG,CAACa,MAAM,CAAC,CAAC;EAC1CC,WAAW,GAAoBd,GAAG,CAACa,MAAM,CAAC,CAAC;EAE3CE,WAAWA,CACTC,GAAiB,EACjBC,KAIC,EACD;IACA,IAAI,CAACZ,IAAI,GAAGW,GAAG,CAACX,IAAI;IACpB,IAAI,CAACC,IAAI,GAAGU,GAAG,CAACV,IAAI;IACpB,IAAI,CAACC,KAAK,GAAGS,GAAG,CAACT,KAAK;IAEtB,IAAI,QAAQ,IAAIS,GAAG,EAAE;MACnB,IAAI,CAACR,MAAM,GAAGQ,GAAG,CAACR,MAAM;IAC1B;IAEA,IAAI,SAAS,IAAIQ,GAAG,EAAE;MACpB,IAAI,CAACP,OAAO,GAAGO,GAAG,CAACP,OAAO;IAC5B;IAEA,IAAI,CAACP,IAAI,GAAGe,KAAK,CAACf,IAAI;IACtB,IAAI,CAACC,MAAM,GAAGc,KAAK,CAACd,MAAM;IAC1B,IAAI,CAACQ,KAAK,GAAGM,KAAK,CAACN,KAAK;EAC1B;EAEA,IAAIO,SAASA,CAAA,EAAG;IACd,MAAM;MAAET,OAAO;MAAEJ;IAAK,CAAC,GAAG,IAAI;IAE9B,MAAMa,SAAoB,GAAG;MAC3BC,UAAU,EAAE,CAAC;IACf,CAAC;IAED,IAAI,CAACV,OAAO,EAAE;MACZ,OAAOS,SAAS;IAClB;IAEA,IAAI,cAAc,IAAIT,OAAO,EAAE;MAC7BS,SAAS,CAACC,UAAU,CAACC,YAAY,GAAGX,OAAO,CAACW,YAAY;IAC1D;IAEA,IAAI,SAAS,IAAIX,OAAO,EAAE;MACxBS,SAAS,CAACG,OAAO,GAAGZ,OAAO,CAACY,OAAO;IACrC;IAEA,IAAI,WAAW,IAAIZ,OAAO,IAAIV,uBAAuB,CAACM,IAAI,CAAC,EAAE;MAC3Da,SAAS,CAACI,SAAS,GAAGb,OAAO,CAACa,SAAS;IACzC;IAEA,OAAOJ,SAAS;EAClB;AACF","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ComponentCollection.js","names":["joi","FormComponent","isFormState","isFormValue","createComponent","getErrors","validationOptions","opts","ComponentCollection","page","parent","components","fields","guidance","formSchema","stateSchema","constructor","defs","props","schema","map","def","filter","component","isFormComponent","object","required","field","collection","name","concat","keys","error","errors","flatMap","isErrorContext","local","title","missing","key","path","find","item","split","shift","child","label","length","peers","and","isPresent","custom","getFormDataFromState","state","payload","forEach","Object","assign","getFormValueFromState","value","entries","pop","getStateFromValidForm","getContextValueFromState","context","getFieldErrors","getViewErrors","getViewModel","query","result","type","model","validate","details","callback","list","fieldErrors","push"],"sources":["../../../../../src/server/plugins/engine/components/ComponentCollection.ts"],"sourcesContent":["import { type ComponentDef } from '@defra/forms-model'\nimport joi, {\n type CustomValidator,\n type ErrorReportCollection,\n type ObjectSchema\n} from 'joi'\n\nimport {\n FormComponent,\n isFormState,\n isFormValue\n} from '~/src/server/plugins/engine/components/FormComponent.js'\nimport {\n createComponent,\n type Component,\n type Field,\n type Guidance\n} from '~/src/server/plugins/engine/components/helpers/components.js'\nimport { type ComponentViewModel } from '~/src/server/plugins/engine/components/types.js'\nimport { getErrors } from '~/src/server/plugins/engine/helpers.js'\nimport { type FormModel } from '~/src/server/plugins/engine/models/index.js'\nimport { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers/pages.js'\nimport { validationOptions as opts } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'\nimport {\n type FormPayload,\n type FormState,\n type FormSubmissionError,\n type FormSubmissionState,\n type FormValidationResult\n} from '~/src/server/plugins/engine/types.js'\nimport { type FormQuery } from '~/src/server/routes/types.js'\n\nexport class ComponentCollection {\n page?: PageControllerClass\n parent?: Component\n\n components: Component[]\n fields: Field[]\n guidance: Guidance[]\n\n formSchema: ObjectSchema<FormPayload>\n stateSchema: ObjectSchema<FormSubmissionState>\n\n constructor(\n defs: ComponentDef[],\n props: {\n page?: PageControllerClass\n parent?: Component\n model: FormModel\n },\n schema?: {\n /**\n * Defines an all-or-nothing relationship between keys where if one\n * of the peers is present, all of them are required as well\n */\n peers?: string[]\n\n /**\n * Defines a custom validation rule for the object schema\n */\n custom?: CustomValidator\n }\n ) {\n const components = defs.map((def) => createComponent(def, props))\n\n const fields = components.filter(\n (component): component is Field => component.isFormComponent\n )\n\n const guidance = components.filter(\n (component): component is Guidance => !component.isFormComponent\n )\n\n let formSchema = joi.object<FormPayload>().required()\n let stateSchema = joi.object<FormSubmissionState>().required()\n\n // Add each field or concat collection\n for (const field of fields) {\n const { collection, name } = field\n\n formSchema = collection\n ? formSchema.concat(collection.formSchema)\n : formSchema.keys({ [name]: field.formSchema })\n\n stateSchema = collection\n ? stateSchema.concat(collection.stateSchema)\n : stateSchema.keys({ [name]: field.stateSchema })\n }\n\n // Add parent field title to collection field errors\n formSchema = formSchema.error((errors) => {\n return errors.flatMap((error) => {\n if (!isErrorContext(error.local) || error.local.title) {\n return error\n }\n\n // Use field key or first missing child field\n let { missing, key = missing?.[0] } = error.local\n\n // But avoid numeric key used by array payloads\n if (typeof key === 'number') {\n key = error.path[0]\n }\n\n // Find the parent field\n const parent = fields.find(\n (item) => item.name === key?.split('__').shift()\n )\n\n // Find the child field\n const child = (parent?.collection?.fields ?? fields).find(\n (item) => item.name === key\n )\n\n // Update error with child label\n if (child && (!error.local.label || error.local.label === 'value')) {\n error.local.label = child.title\n }\n\n // Fix error summary links for missing fields\n if (missing?.length) {\n error.path = missing\n error.local.key = missing[0]\n }\n\n // Update error with parent title\n error.local.title ??= parent?.label\n\n return error\n })\n })\n\n if (schema?.peers) {\n formSchema = formSchema.and(...schema.peers, {\n isPresent: isFormValue\n })\n }\n\n if (schema?.custom) {\n formSchema = formSchema.custom(schema.custom)\n }\n\n this.page = props.page\n this.parent = props.parent\n\n this.components = components\n this.fields = fields\n this.guidance = guidance\n\n this.formSchema = formSchema\n this.stateSchema = stateSchema\n }\n\n get keys() {\n return this.fields.flatMap((field) => {\n const { name, collection } = field\n\n if (collection) {\n const { fields } = collection\n return [name, ...fields.map(({ name }) => name)]\n }\n\n return [name]\n })\n }\n\n getFormDataFromState(state: FormSubmissionState) {\n const payload: FormPayload = {}\n\n this.fields.forEach((component) => {\n Object.assign(payload, component.getFormDataFromState(state))\n })\n\n return payload\n }\n\n getFormValueFromState(state: FormSubmissionState) {\n const payload: FormPayload = {}\n\n // Remove name prefix for formatted value\n for (const [name, value] of Object.entries(\n this.getFormDataFromState(state)\n )) {\n const key = name.split('__').pop()\n if (!key) {\n continue\n }\n\n payload[key] = value\n }\n\n return payload\n }\n\n getStateFromValidForm(payload: FormPayload) {\n const state: FormState = {}\n\n this.fields.forEach((component) => {\n Object.assign(state, component.getStateFromValidForm(payload))\n })\n\n return state\n }\n\n getContextValueFromState(state: FormSubmissionState) {\n const context: FormState = {}\n\n for (const component of this.fields) {\n context[component.name] = component.getContextValueFromState(state)\n }\n\n return context\n }\n\n /**\n * Get all errors for all fields in this collection\n */\n getErrors(errors?: FormSubmissionError[]): FormSubmissionError[] | undefined {\n return this.getFieldErrors((field) => field.getErrors(errors), errors)\n }\n\n /**\n * Get view errors for all fields in this collection.\n * For most fields this means filtering to the first error in the list.\n * Composite fields like UKAddress can choose to return more than one error.\n */\n getViewErrors(\n errors?: FormSubmissionError[]\n ): FormSubmissionError[] | undefined {\n return this.getFieldErrors((field) => field.getViewErrors(errors), errors)\n }\n\n getViewModel(\n payload: FormPayload,\n errors?: FormSubmissionError[],\n query: FormQuery = {}\n ) {\n const { components } = this\n\n const result: ComponentViewModel[] = components.map((component) => {\n const { isFormComponent, type } = component\n\n const model =\n component instanceof FormComponent\n ? component.getViewModel(payload, errors, query)\n : component.getViewModel()\n\n return { type, isFormComponent, model }\n })\n\n return result\n }\n\n /**\n * Validate form payload\n */\n validate(value: FormPayload = {}): FormValidationResult<FormPayload> {\n const result = this.formSchema.validate(value, opts)\n const details = result.error?.details\n\n return {\n value: (result.value ?? {}) as typeof value,\n errors: this.page?.getErrors(details) ?? getErrors(details)\n }\n }\n\n /**\n * Helper to get errors from all fields\n */\n private getFieldErrors(\n callback: (field: Field) => FormSubmissionError[] | undefined,\n errors?: FormSubmissionError[]\n ): FormSubmissionError[] | undefined {\n const { fields } = this\n\n if (!errors?.length) {\n return\n }\n\n const list: FormSubmissionError[] = []\n\n for (const field of fields) {\n const fieldErrors = callback(field)\n\n if (fieldErrors?.length) {\n list.push(...fieldErrors)\n }\n }\n\n if (!list.length) {\n return\n }\n\n return list\n }\n}\n\n/**\n * Check for field local state\n */\nexport function isErrorContext(\n value?: unknown\n): value is ErrorReportCollection['local'] {\n return isFormState(value) && typeof value.label === 'string'\n}\n"],"mappings":"AACA,OAAOA,GAAG,MAIH,KAAK;AA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F;IAEA,MAAM4B,IAA2B,GAAG,EAAE;IAEtC,KAAK,MAAMhD,KAAK,IAAIf,MAAM,EAAE;MAC1B,MAAMgE,WAAW,GAAGF,QAAQ,CAAC/C,KAAK,CAAC;MAEnC,IAAIiD,WAAW,EAAE7B,MAAM,EAAE;QACvB4B,IAAI,CAACE,IAAI,CAAC,GAAGD,WAAW,CAAC;MAC3B;IACF;IAEA,IAAI,CAACD,IAAI,CAAC5B,MAAM,EAAE;MAChB;IACF;IAEA,OAAO4B,IAAI;EACb;AACF;;AAEA;AACA;AACA;AACA,OAAO,SAASxC,cAAcA,CAC5BwB,KAAe,EAC0B;EACzC,OAAOzD,WAAW,CAACyD,KAAK,CAAC,IAAI,OAAOA,KAAK,CAACb,KAAK,KAAK,QAAQ;AAC9D","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"ComponentCollection.js","names":["joi","FormComponent","isFormState","isFormValue","createComponent","getErrors","validationOptions","opts","ComponentCollection","page","parent","components","fields","guidance","formSchema","stateSchema","constructor","defs","props","schema","map","def","filter","component","isFormComponent","object","required","field","collection","name","concat","keys","error","errors","flatMap","isErrorContext","local","title","missing","key","path","find","item","split","shift","child","label","length","peers","and","isPresent","custom","getFormDataFromState","state","payload","forEach","Object","assign","getFormValueFromState","value","entries","pop","getStateFromValidForm","getContextValueFromState","context","getFieldErrors","getViewErrors","getViewModel","query","result","type","model","validate","details","callback","list","fieldErrors","push"],"sources":["../../../../../src/server/plugins/engine/components/ComponentCollection.ts"],"sourcesContent":["import { type ComponentDef } from '@defra/forms-model'\nimport joi, {\n type CustomValidator,\n type ErrorReportCollection,\n type ObjectSchema\n} from 'joi'\n\nimport {\n FormComponent,\n isFormState,\n isFormValue\n} from '~/src/server/plugins/engine/components/FormComponent.js'\nimport {\n createComponent,\n type Component,\n type Field,\n type Guidance\n} from '~/src/server/plugins/engine/components/helpers/components.js'\nimport { type ComponentViewModel } from '~/src/server/plugins/engine/components/types.js'\nimport { getErrors } from '~/src/server/plugins/engine/helpers.js'\nimport { type FormModel } from '~/src/server/plugins/engine/models/index.js'\nimport { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers/pages.js'\nimport { validationOptions as opts } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'\nimport {\n type FormPayload,\n type FormState,\n type FormSubmissionError,\n type FormSubmissionState,\n type FormValidationResult\n} from '~/src/server/plugins/engine/types.js'\nimport { type FormQuery } from '~/src/server/routes/types.js'\n\nexport class ComponentCollection {\n page?: PageControllerClass\n parent?: Component\n\n components: Component[]\n fields: Field[]\n guidance: Guidance[]\n\n formSchema: ObjectSchema<FormPayload>\n stateSchema: ObjectSchema<FormSubmissionState>\n\n constructor(\n defs: ComponentDef[],\n props: {\n page?: PageControllerClass\n parent?: Component\n model: FormModel\n },\n schema?: {\n /**\n * Defines an all-or-nothing relationship between keys where if one\n * of the peers is present, all of them are required as well\n */\n peers?: string[]\n\n /**\n * Defines a custom validation rule for the object schema\n */\n custom?: CustomValidator\n }\n ) {\n const components = defs.map((def) => createComponent(def, props))\n\n const fields = components.filter(\n (component): component is Field => component.isFormComponent\n )\n\n const guidance = components.filter(\n (component): component is Guidance => !component.isFormComponent\n )\n\n let formSchema = joi.object<FormPayload>().required()\n let stateSchema = joi.object<FormSubmissionState>().required()\n\n // Add each field or concat collection\n for (const field of fields) {\n const { collection, name } = field\n\n formSchema = collection\n ? formSchema.concat(collection.formSchema)\n : formSchema.keys({ [name]: field.formSchema })\n\n stateSchema = collection\n ? stateSchema.concat(collection.stateSchema)\n : stateSchema.keys({ [name]: field.stateSchema })\n }\n\n // Add parent field title to collection field errors\n formSchema = formSchema.error((errors) => {\n return errors.flatMap((error) => {\n if (!isErrorContext(error.local) || error.local.title) {\n return error\n }\n\n // Use field key or first missing child field\n let { missing, key = missing?.[0] } = error.local\n\n // But avoid numeric key used by array payloads\n if (typeof key === 'number') {\n key = error.path[0]\n }\n\n // Find the parent field\n const parent = fields.find(\n (item) => item.name === key?.split('__').shift()\n )\n\n // Find the child field\n const child = (parent?.collection?.fields ?? fields).find(\n (item) => item.name === key\n )\n\n // Update error with child label\n if (child && (!error.local.label || error.local.label === 'value')) {\n error.local.label = child.title\n }\n\n // Fix error summary links for missing fields\n if (missing?.length) {\n error.path = missing\n error.local.key = missing[0]\n }\n\n // Update error with parent title\n error.local.title ??= parent?.label\n\n return error\n })\n })\n\n if (schema?.peers) {\n formSchema = formSchema.and(...schema.peers, {\n isPresent: isFormValue\n })\n }\n\n if (schema?.custom) {\n formSchema = formSchema.custom(schema.custom)\n }\n\n this.page = props.page\n this.parent = props.parent\n\n this.components = components\n this.fields = fields\n this.guidance = guidance\n\n this.formSchema = formSchema\n this.stateSchema = stateSchema\n }\n\n get keys() {\n return this.fields.flatMap((field) => {\n const { name, collection } = field\n\n if (collection) {\n const { fields } = collection\n return [name, ...fields.map(({ name }) => name)]\n }\n\n return [name]\n })\n }\n\n getFormDataFromState(state: FormSubmissionState) {\n const payload: FormPayload = {}\n\n this.fields.forEach((component) => {\n Object.assign(payload, component.getFormDataFromState(state))\n })\n\n return payload\n }\n\n getFormValueFromState(state: FormSubmissionState) {\n const payload: FormPayload = {}\n\n // Remove name prefix for formatted value\n for (const [name, value] of Object.entries(\n this.getFormDataFromState(state)\n )) {\n const key = name.split('__').pop()\n if (!key) {\n continue\n }\n\n payload[key] = value\n }\n\n return payload\n }\n\n getStateFromValidForm(payload: FormPayload) {\n const state: FormState = {}\n\n this.fields.forEach((component) => {\n Object.assign(state, component.getStateFromValidForm(payload))\n })\n\n return state\n }\n\n getContextValueFromState(state: FormSubmissionState) {\n const context: FormState = {}\n\n for (const component of this.fields) {\n context[component.name] = component.getContextValueFromState(state)\n }\n\n return context\n }\n\n /**\n * Get all errors for all fields in this collection\n */\n getErrors(errors?: FormSubmissionError[]): FormSubmissionError[] | undefined {\n return this.getFieldErrors((field) => field.getErrors(errors), errors)\n }\n\n /**\n * Get view errors for all fields in this collection.\n * For most fields this means filtering to the first error in the list.\n * Composite fields like UKAddress can choose to return more than one error.\n */\n getViewErrors(\n errors?: FormSubmissionError[]\n ): FormSubmissionError[] | undefined {\n return this.getFieldErrors((field) => field.getViewErrors(errors), errors)\n }\n\n getViewModel(\n payload: FormPayload,\n errors?: FormSubmissionError[],\n query: FormQuery = {}\n ) {\n const { components } = this\n\n const result: ComponentViewModel[] = components.map((component) => {\n const { isFormComponent, type } = component\n\n const model =\n component instanceof FormComponent\n ? component.getViewModel(payload, errors, query)\n : component.getViewModel()\n\n return { type, isFormComponent, model }\n })\n\n return result\n }\n\n /**\n * Validate form payload\n */\n validate(value: FormPayload = {}): FormValidationResult<FormPayload> {\n const result = this.formSchema.validate(value, opts)\n\n const details = result.error?.details\n\n return {\n value: (result.value ?? {}) as typeof value,\n errors: this.page?.getErrors(details) ?? getErrors(details)\n }\n }\n\n /**\n * Helper to get errors from all fields\n */\n private getFieldErrors(\n callback: (field: Field) => FormSubmissionError[] | undefined,\n errors?: FormSubmissionError[]\n ): FormSubmissionError[] | undefined {\n const { fields } = this\n\n if (!errors?.length) {\n return\n }\n\n const list: FormSubmissionError[] = []\n\n for (const field of fields) {\n const fieldErrors = callback(field)\n\n if (fieldErrors?.length) {\n list.push(...fieldErrors)\n }\n }\n\n if (!list.length) {\n return\n }\n\n return list\n }\n}\n\n/**\n * Check for field local state\n */\nexport function isErrorContext(\n value?: unknown\n): value is ErrorReportCollection['local'] {\n return isFormState(value) && typeof value.label === 'string'\n}\n"],"mappings":"AACA,OAAOA,GAAG,MAIH,KAAK;AAEZ,SACEC,aAAa,EACbC,WAAW,EACXC,WAAW;AAEb,SACEC,eAAe;AAMjB,SAASC,SAAS;AAGlB,SAASC,iBAAiB,IAAIC,IAAI;AAUlC,OAAO,MAAMC,mBAAmB,CAAC;EAC/BC,IAAI;EACJC,MAAM;EAENC,UAAU;EACVC,MAAM;EACNC,QAAQ;EAERC,UAAU;EACVC,WAAW;EAEXC,WAAWA,CACTC,IAAoB,EACpBC,KAIC,EACDC,MAWC,EACD;IACA,MAAMR,UAAU,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O4B,IAAI;EACb;AACF;;AAEA;AACA;AACA;AACA,OAAO,SAASxC,cAAcA,CAC5BwB,KAAe,EAC0B;EACzC,OAAOzD,WAAW,CAACyD,KAAK,CAAC,IAAI,OAAOA,KAAK,CAACb,KAAK,KAAK,QAAQ;AAC9D","ignoreList":[]}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { type DeclarationFieldComponent, type Item } from '@defra/forms-model';
|
|
2
|
+
import { type ArraySchema, type BooleanSchema, type StringSchema } from 'joi';
|
|
3
|
+
import { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js';
|
|
4
|
+
import { type ErrorMessageTemplateList, type FormPayload, type FormState, type FormStateValue, type FormSubmissionError, type FormSubmissionState, type FormValue } from '~/src/server/plugins/engine/types.js';
|
|
5
|
+
export declare class DeclarationField extends FormComponent {
|
|
6
|
+
private readonly DEFAULT_DECLARATION_LABEL;
|
|
7
|
+
options: DeclarationFieldComponent['options'];
|
|
8
|
+
declarationConfirmationLabel: string;
|
|
9
|
+
formSchema: ArraySchema<StringSchema[]>;
|
|
10
|
+
stateSchema: BooleanSchema;
|
|
11
|
+
content: string;
|
|
12
|
+
constructor(def: DeclarationFieldComponent, props: ConstructorParameters<typeof FormComponent>[1]);
|
|
13
|
+
getFormValueFromState(state: FormSubmissionState): "true" | undefined;
|
|
14
|
+
getFormDataFromState(state: FormSubmissionState): FormPayload;
|
|
15
|
+
getStateFromValidForm(payload: FormPayload): FormState;
|
|
16
|
+
getContextValueFromFormValue(value: FormValue | FormPayload): boolean;
|
|
17
|
+
getFormValue(value?: FormStateValue | FormState): (string | number | boolean)[] | undefined;
|
|
18
|
+
getDisplayStringFromFormValue(value: FormValue | FormPayload): string;
|
|
19
|
+
getViewModel(payload: FormPayload, errors?: FormSubmissionError[]): {
|
|
20
|
+
hint: {
|
|
21
|
+
text: string;
|
|
22
|
+
} | undefined;
|
|
23
|
+
fieldset: {
|
|
24
|
+
legend: {
|
|
25
|
+
text: string;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
content: string;
|
|
29
|
+
values: FormValue;
|
|
30
|
+
items: {
|
|
31
|
+
text: string;
|
|
32
|
+
value: string;
|
|
33
|
+
}[];
|
|
34
|
+
label: {
|
|
35
|
+
text: string;
|
|
36
|
+
};
|
|
37
|
+
id: string;
|
|
38
|
+
name: string;
|
|
39
|
+
value: FormValue;
|
|
40
|
+
type?: string;
|
|
41
|
+
prefix?: import("./types.js").ComponentText;
|
|
42
|
+
suffix?: import("./types.js").ComponentText;
|
|
43
|
+
classes?: string;
|
|
44
|
+
condition?: string;
|
|
45
|
+
errors?: FormSubmissionError[];
|
|
46
|
+
errorMessage?: {
|
|
47
|
+
text: string;
|
|
48
|
+
};
|
|
49
|
+
summaryHtml?: string;
|
|
50
|
+
html?: string;
|
|
51
|
+
attributes: {
|
|
52
|
+
autocomplete?: string;
|
|
53
|
+
maxlength?: number;
|
|
54
|
+
multiple?: string;
|
|
55
|
+
accept?: string;
|
|
56
|
+
inputmode?: string;
|
|
57
|
+
};
|
|
58
|
+
maxlength?: number;
|
|
59
|
+
maxwords?: number;
|
|
60
|
+
rows?: number;
|
|
61
|
+
formGroup?: {
|
|
62
|
+
classes?: string;
|
|
63
|
+
attributes?: string | Record<string, string>;
|
|
64
|
+
};
|
|
65
|
+
components?: import("./types.js").ComponentViewModel[];
|
|
66
|
+
upload?: {
|
|
67
|
+
count: number;
|
|
68
|
+
summaryList: import("~/src/server/plugins/engine/types.js").SummaryList;
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
isValue(value?: FormStateValue | FormState): value is Item['value'][];
|
|
72
|
+
/**
|
|
73
|
+
* For error preview page that shows all possible errors on a component
|
|
74
|
+
*/
|
|
75
|
+
getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
76
|
+
/**
|
|
77
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
78
|
+
*/
|
|
79
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
80
|
+
static isBool(value?: FormStateValue | FormState): value is boolean;
|
|
81
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import joi from 'joi';
|
|
2
|
+
import { FormComponent, isFormValue } from "./FormComponent.js";
|
|
3
|
+
import { messageTemplate } from "../pageControllers/validationOptions.js";
|
|
4
|
+
export class DeclarationField extends FormComponent {
|
|
5
|
+
DEFAULT_DECLARATION_LABEL = 'I understand and agree';
|
|
6
|
+
constructor(def, props) {
|
|
7
|
+
super(def, props);
|
|
8
|
+
const {
|
|
9
|
+
options,
|
|
10
|
+
content
|
|
11
|
+
} = def;
|
|
12
|
+
let checkboxSchema = joi.string().valid('true');
|
|
13
|
+
if (options.required !== false) {
|
|
14
|
+
checkboxSchema = checkboxSchema.required();
|
|
15
|
+
}
|
|
16
|
+
const formSchema = joi.array().items(checkboxSchema, joi.string().valid('unchecked').strip()).label(this.label).single().messages({
|
|
17
|
+
'any.required': messageTemplate.declarationRequired,
|
|
18
|
+
'any.unknown': messageTemplate.declarationRequired,
|
|
19
|
+
'array.includesRequiredUnknowns': messageTemplate.declarationRequired
|
|
20
|
+
});
|
|
21
|
+
this.formSchema = formSchema;
|
|
22
|
+
this.stateSchema = joi.boolean().cast('string').label(this.label).required();
|
|
23
|
+
this.options = options;
|
|
24
|
+
this.content = content;
|
|
25
|
+
this.declarationConfirmationLabel = options.declarationConfirmationLabel ?? this.DEFAULT_DECLARATION_LABEL;
|
|
26
|
+
}
|
|
27
|
+
getFormValueFromState(state) {
|
|
28
|
+
const {
|
|
29
|
+
name
|
|
30
|
+
} = this;
|
|
31
|
+
return state[name] === true ? 'true' : undefined;
|
|
32
|
+
}
|
|
33
|
+
getFormDataFromState(state) {
|
|
34
|
+
const {
|
|
35
|
+
name
|
|
36
|
+
} = this;
|
|
37
|
+
return {
|
|
38
|
+
[name]: state[name] === true ? 'true' : undefined
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
getStateFromValidForm(payload) {
|
|
42
|
+
const {
|
|
43
|
+
name
|
|
44
|
+
} = this;
|
|
45
|
+
const payloadValue = payload[name];
|
|
46
|
+
const value = this.isValue(payloadValue) && payloadValue.length > 0 && payloadValue.every(v => {
|
|
47
|
+
return v === 'true';
|
|
48
|
+
});
|
|
49
|
+
return {
|
|
50
|
+
[name]: value
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
getContextValueFromFormValue(value) {
|
|
54
|
+
return value === 'true';
|
|
55
|
+
}
|
|
56
|
+
getFormValue(value) {
|
|
57
|
+
return this.isValue(value) ? value : undefined;
|
|
58
|
+
}
|
|
59
|
+
getDisplayStringFromFormValue(value) {
|
|
60
|
+
return value ? this.declarationConfirmationLabel : '';
|
|
61
|
+
}
|
|
62
|
+
getViewModel(payload, errors) {
|
|
63
|
+
const defaultDeclarationConfirmationLabel = 'I confirm that I understand and accept this declaration';
|
|
64
|
+
const {
|
|
65
|
+
title,
|
|
66
|
+
hint,
|
|
67
|
+
content,
|
|
68
|
+
declarationConfirmationLabel = defaultDeclarationConfirmationLabel
|
|
69
|
+
} = this;
|
|
70
|
+
return {
|
|
71
|
+
...super.getViewModel(payload, errors),
|
|
72
|
+
hint: hint ? {
|
|
73
|
+
text: hint
|
|
74
|
+
} : undefined,
|
|
75
|
+
fieldset: {
|
|
76
|
+
legend: {
|
|
77
|
+
text: title
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
content,
|
|
81
|
+
values: payload[this.name],
|
|
82
|
+
items: [{
|
|
83
|
+
text: declarationConfirmationLabel,
|
|
84
|
+
value: 'true'
|
|
85
|
+
}]
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
isValue(value) {
|
|
89
|
+
if (!Array.isArray(value)) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Skip checks when empty
|
|
94
|
+
if (!value.length) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
return value.every(isFormValue);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* For error preview page that shows all possible errors on a component
|
|
102
|
+
*/
|
|
103
|
+
getAllPossibleErrors() {
|
|
104
|
+
return DeclarationField.getAllPossibleErrors();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
109
|
+
*/
|
|
110
|
+
static getAllPossibleErrors() {
|
|
111
|
+
return {
|
|
112
|
+
baseErrors: [{
|
|
113
|
+
type: 'required',
|
|
114
|
+
template: messageTemplate.declarationRequired
|
|
115
|
+
}],
|
|
116
|
+
advancedSettingsErrors: []
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
static isBool(value) {
|
|
120
|
+
return isFormValue(value) && typeof value === 'boolean';
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=DeclarationField.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DeclarationField.js","names":["joi","FormComponent","isFormValue","messageTemplate","DeclarationField","DEFAULT_DECLARATION_LABEL","constructor","def","props","options","content","checkboxSchema","string","valid","required","formSchema","array","items","strip","label","single","messages","declarationRequired","stateSchema","boolean","cast","declarationConfirmationLabel","getFormValueFromState","state","name","undefined","getFormDataFromState","getStateFromValidForm","payload","payloadValue","value","isValue","length","every","v","getContextValueFromFormValue","getFormValue","getDisplayStringFromFormValue","getViewModel","errors","defaultDeclarationConfirmationLabel","title","hint","text","fieldset","legend","values","Array","isArray","getAllPossibleErrors","baseErrors","type","template","advancedSettingsErrors","isBool"],"sources":["../../../../../src/server/plugins/engine/components/DeclarationField.ts"],"sourcesContent":["import { type DeclarationFieldComponent, type Item } from '@defra/forms-model'\nimport joi, {\n type ArraySchema,\n type BooleanSchema,\n type StringSchema\n} from 'joi'\n\nimport {\n FormComponent,\n isFormValue\n} from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'\nimport {\n type ErrorMessageTemplateList,\n type FormPayload,\n type FormState,\n type FormStateValue,\n type FormSubmissionError,\n type FormSubmissionState,\n type FormValue\n} from '~/src/server/plugins/engine/types.js'\n\nexport class DeclarationField extends FormComponent {\n private readonly DEFAULT_DECLARATION_LABEL = 'I understand and agree'\n\n declare options: DeclarationFieldComponent['options']\n\n declare declarationConfirmationLabel: string\n\n declare formSchema: ArraySchema<StringSchema[]>\n declare stateSchema: BooleanSchema\n declare content: string\n\n constructor(\n def: DeclarationFieldComponent,\n props: ConstructorParameters<typeof FormComponent>[1]\n ) {\n super(def, props)\n\n const { options, content } = def\n\n let checkboxSchema = joi.string().valid('true')\n\n if (options.required !== false) {\n checkboxSchema = checkboxSchema.required()\n }\n\n const formSchema = joi\n .array()\n .items(checkboxSchema, joi.string().valid('unchecked').strip())\n .label(this.label)\n .single()\n .messages({\n 'any.required': messageTemplate.declarationRequired as string,\n 'any.unknown': messageTemplate.declarationRequired as string,\n 'array.includesRequiredUnknowns':\n messageTemplate.declarationRequired as string\n }) as ArraySchema<StringSchema[]>\n\n this.formSchema = formSchema\n this.stateSchema = joi.boolean().cast('string').label(this.label).required()\n\n this.options = options\n this.content = content\n this.declarationConfirmationLabel =\n options.declarationConfirmationLabel ?? this.DEFAULT_DECLARATION_LABEL\n }\n\n getFormValueFromState(state: FormSubmissionState) {\n const { name } = this\n return state[name] === true ? 'true' : undefined\n }\n\n getFormDataFromState(state: FormSubmissionState): FormPayload {\n const { name } = this\n return { [name]: state[name] === true ? 'true' : undefined }\n }\n\n getStateFromValidForm(payload: FormPayload): FormState {\n const { name } = this\n const payloadValue = payload[name]\n const value =\n this.isValue(payloadValue) &&\n payloadValue.length > 0 &&\n payloadValue.every((v) => {\n return v === 'true'\n })\n\n return { [name]: value }\n }\n\n getContextValueFromFormValue(value: FormValue | FormPayload): boolean {\n return value === 'true'\n }\n\n getFormValue(value?: FormStateValue | FormState) {\n return this.isValue(value) ? value : undefined\n }\n\n getDisplayStringFromFormValue(value: FormValue | FormPayload): string {\n return value ? this.declarationConfirmationLabel : ''\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const defaultDeclarationConfirmationLabel =\n 'I confirm that I understand and accept this declaration'\n const {\n title,\n hint,\n content,\n declarationConfirmationLabel = defaultDeclarationConfirmationLabel\n } = this\n return {\n ...super.getViewModel(payload, errors),\n hint: hint ? { text: hint } : undefined,\n fieldset: {\n legend: {\n text: title\n }\n },\n content,\n values: payload[this.name],\n items: [\n {\n text: declarationConfirmationLabel,\n value: 'true'\n }\n ]\n }\n }\n\n isValue(value?: FormStateValue | FormState): value is Item['value'][] {\n if (!Array.isArray(value)) {\n return false\n }\n\n // Skip checks when empty\n if (!value.length) {\n return true\n }\n\n return value.every(isFormValue)\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return DeclarationField.getAllPossibleErrors()\n }\n\n /**\n * Static version of getAllPossibleErrors that doesn't require a component instance.\n */\n static getAllPossibleErrors(): ErrorMessageTemplateList {\n return {\n baseErrors: [\n { type: 'required', template: messageTemplate.declarationRequired }\n ],\n advancedSettingsErrors: []\n }\n }\n\n static isBool(value?: FormStateValue | FormState): value is boolean {\n return isFormValue(value) && typeof value === 'boolean'\n }\n}\n"],"mappings":"AACA,OAAOA,GAAG,MAIH,KAAK;AAEZ,SACEC,aAAa,EACbC,WAAW;AAEb,SAASC,eAAe;AAWxB,OAAO,MAAMC,gBAAgB,SAASH,aAAa,CAAC;EACjCI,yBAAyB,GAAG,wBAAwB;EAUrEC,WAAWA,CACTC,GAA8B,EAC9BC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,EAAEC,KAAK,CAAC;IAEjB,MAAM;MAAEC,OAAO;MAAEC;IAAQ,CAAC,GAAGH,GAAG;IAEhC,IAAII,cAAc,GAAGX,GAAG,CAACY,MAAM,CAAC,CAAC,CAACC,KAAK,CAAC,MAAM,CAAC;IAE/C,IAAIJ,OAAO,CAACK,QAAQ,KAAK,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AEAC,OAAOA,CAACD,KAAkC,EAA4B;IACpE,IAAI,CAACiB,KAAK,CAACC,OAAO,CAAClB,KAAK,CAAC,EAAE;MACzB,OAAO,KAAK;IACd;;IAEA;IACA,IAAI,CAACA,KAAK,CAACE,MAAM,EAAE;MACjB,OAAO,IAAI;IACb;IAEA,OAAOF,KAAK,CAACG,KAAK,CAACpC,WAAW,CAAC;EACjC;;EAEA;AACF;AACA;EACEoD,oBAAoBA,CAAA,EAA6B;IAC/C,OAAOlD,gBAAgB,CAACkD,oBAAoB,CAAC,CAAC;EAChD;;EAEA;AACF;AACA;EACE,OAAOA,oBAAoBA,CAAA,EAA6B;IACtD,OAAO;MACLC,UAAU,EAAE,CACV;QAAEC,IAAI,EAAE,UAAU;QAAEC,QAAQ,EAAEtD,eAAe,CAACmB;MAAoB,CAAC,CACpE;MACDoC,sBAAsB,EAAE;IAC1B,CAAC;EACH;EAEA,OAAOC,MAAMA,CAACxB,KAAkC,EAAoB;IAClE,OAAOjC,WAAW,CAACiC,KAAK,CAAC,IAAI,OAAOA,KAAK,KAAK,SAAS;EACzD;AACF","ignoreList":[]}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { type EastingNorthingFieldComponent } from '@defra/forms-model';
|
|
2
|
+
import { type ObjectSchema } from 'joi';
|
|
3
|
+
import { ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js';
|
|
4
|
+
import { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js';
|
|
5
|
+
import { type EastingNorthingState } from '~/src/server/plugins/engine/components/types.js';
|
|
6
|
+
import { type ErrorMessageTemplateList, type FormPayload, type FormState, type FormStateValue, type FormSubmissionError, type FormSubmissionState } from '~/src/server/plugins/engine/types.js';
|
|
7
|
+
export declare class EastingNorthingField extends FormComponent {
|
|
8
|
+
options: EastingNorthingFieldComponent['options'];
|
|
9
|
+
formSchema: ObjectSchema<FormPayload>;
|
|
10
|
+
stateSchema: ObjectSchema<FormState>;
|
|
11
|
+
collection: ComponentCollection;
|
|
12
|
+
constructor(def: EastingNorthingFieldComponent, props: ConstructorParameters<typeof FormComponent>[1]);
|
|
13
|
+
getFormValueFromState(state: FormSubmissionState): EastingNorthingState | undefined;
|
|
14
|
+
getDisplayStringFromFormValue(value: EastingNorthingState | undefined): string;
|
|
15
|
+
getDisplayStringFromState(state: FormSubmissionState): string;
|
|
16
|
+
getContextValueFromFormValue(value: EastingNorthingState | undefined): string | null;
|
|
17
|
+
getContextValueFromState(state: FormSubmissionState): string | null;
|
|
18
|
+
getViewModel(payload: FormPayload, errors?: FormSubmissionError[]): {
|
|
19
|
+
fieldset: {
|
|
20
|
+
attributes?: string | Record<string, string>;
|
|
21
|
+
legend?: import("~/src/server/plugins/engine/components/types.js").Label;
|
|
22
|
+
};
|
|
23
|
+
items: import("~/src/server/plugins/engine/components/types.js").DateInputItem[];
|
|
24
|
+
label: import("~/src/server/plugins/engine/components/types.js").Label;
|
|
25
|
+
type?: string;
|
|
26
|
+
id: string;
|
|
27
|
+
name: string;
|
|
28
|
+
value: import("~/src/server/plugins/engine/types.js").FormValue;
|
|
29
|
+
hint?: {
|
|
30
|
+
id?: string;
|
|
31
|
+
text: string;
|
|
32
|
+
};
|
|
33
|
+
prefix?: import("~/src/server/plugins/engine/components/types.js").ComponentText;
|
|
34
|
+
suffix?: import("~/src/server/plugins/engine/components/types.js").ComponentText;
|
|
35
|
+
classes?: string;
|
|
36
|
+
condition?: string;
|
|
37
|
+
errors?: FormSubmissionError[];
|
|
38
|
+
errorMessage?: {
|
|
39
|
+
text: string;
|
|
40
|
+
};
|
|
41
|
+
summaryHtml?: string;
|
|
42
|
+
html?: string;
|
|
43
|
+
attributes: {
|
|
44
|
+
autocomplete?: string;
|
|
45
|
+
maxlength?: number;
|
|
46
|
+
multiple?: string;
|
|
47
|
+
accept?: string;
|
|
48
|
+
inputmode?: string;
|
|
49
|
+
};
|
|
50
|
+
content?: import("~/src/server/plugins/engine/components/types.js").Content | import("~/src/server/plugins/engine/components/types.js").Content[] | string;
|
|
51
|
+
maxlength?: number;
|
|
52
|
+
maxwords?: number;
|
|
53
|
+
rows?: number;
|
|
54
|
+
formGroup?: {
|
|
55
|
+
classes?: string;
|
|
56
|
+
attributes?: string | Record<string, string>;
|
|
57
|
+
};
|
|
58
|
+
components?: import("~/src/server/plugins/engine/components/types.js").ComponentViewModel[];
|
|
59
|
+
upload?: {
|
|
60
|
+
count: number;
|
|
61
|
+
summaryList: import("~/src/server/plugins/engine/types.js").SummaryList;
|
|
62
|
+
};
|
|
63
|
+
} | {
|
|
64
|
+
instructionText: string;
|
|
65
|
+
fieldset: {
|
|
66
|
+
attributes?: string | Record<string, string>;
|
|
67
|
+
legend?: import("~/src/server/plugins/engine/components/types.js").Label;
|
|
68
|
+
};
|
|
69
|
+
items: import("~/src/server/plugins/engine/components/types.js").DateInputItem[];
|
|
70
|
+
label: import("~/src/server/plugins/engine/components/types.js").Label;
|
|
71
|
+
type?: string;
|
|
72
|
+
id: string;
|
|
73
|
+
name: string;
|
|
74
|
+
value: import("~/src/server/plugins/engine/types.js").FormValue;
|
|
75
|
+
hint?: {
|
|
76
|
+
id?: string;
|
|
77
|
+
text: string;
|
|
78
|
+
};
|
|
79
|
+
prefix?: import("~/src/server/plugins/engine/components/types.js").ComponentText;
|
|
80
|
+
suffix?: import("~/src/server/plugins/engine/components/types.js").ComponentText;
|
|
81
|
+
classes?: string;
|
|
82
|
+
condition?: string;
|
|
83
|
+
errors?: FormSubmissionError[];
|
|
84
|
+
errorMessage?: {
|
|
85
|
+
text: string;
|
|
86
|
+
};
|
|
87
|
+
summaryHtml?: string;
|
|
88
|
+
html?: string;
|
|
89
|
+
attributes: {
|
|
90
|
+
autocomplete?: string;
|
|
91
|
+
maxlength?: number;
|
|
92
|
+
multiple?: string;
|
|
93
|
+
accept?: string;
|
|
94
|
+
inputmode?: string;
|
|
95
|
+
};
|
|
96
|
+
content?: import("~/src/server/plugins/engine/components/types.js").Content | import("~/src/server/plugins/engine/components/types.js").Content[] | string;
|
|
97
|
+
maxlength?: number;
|
|
98
|
+
maxwords?: number;
|
|
99
|
+
rows?: number;
|
|
100
|
+
formGroup?: {
|
|
101
|
+
classes?: string;
|
|
102
|
+
attributes?: string | Record<string, string>;
|
|
103
|
+
};
|
|
104
|
+
components?: import("~/src/server/plugins/engine/components/types.js").ComponentViewModel[];
|
|
105
|
+
upload?: {
|
|
106
|
+
count: number;
|
|
107
|
+
summaryList: import("~/src/server/plugins/engine/types.js").SummaryList;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
isState(value?: FormStateValue | FormState): value is EastingNorthingState;
|
|
111
|
+
/**
|
|
112
|
+
* For error preview page that shows all possible errors on a component
|
|
113
|
+
*/
|
|
114
|
+
getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
115
|
+
/**
|
|
116
|
+
* Static version of getAllPossibleErrors that doesn't require a component instance.
|
|
117
|
+
*/
|
|
118
|
+
static getAllPossibleErrors(): ErrorMessageTemplateList;
|
|
119
|
+
static isEastingNorthing(value?: FormStateValue | FormState): value is EastingNorthingState;
|
|
120
|
+
}
|
|
121
|
+
export declare function getValidatorEastingNorthing(component: EastingNorthingField): import("joi").CustomValidator;
|