@defra/forms-engine-plugin 4.17.1 → 4.19.0
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/.server/server/plugins/engine/components/FormComponent.d.ts +1 -0
- package/.server/server/plugins/engine/components/FormComponent.js +10 -1
- package/.server/server/plugins/engine/components/FormComponent.js.map +1 -1
- package/.server/server/plugins/engine/components/UkAddressField.d.ts +1 -2
- package/.server/server/plugins/engine/components/UkAddressField.js +3 -6
- package/.server/server/plugins/engine/components/UkAddressField.js.map +1 -1
- package/.server/server/plugins/engine/components/helpers/telephone.js.map +1 -1
- package/.server/server/plugins/engine/views/file-upload.html +1 -1
- package/.server/server/plugins/engine/views/item-delete.html +1 -1
- package/.server/server/plugins/engine/views/partials/form.html +1 -1
- package/.server/server/plugins/engine/views/repeat-list-summary.html +1 -1
- package/.server/server/plugins/engine/views/summary.html +1 -1
- package/.server/server/plugins/postcode-lookup/views/postcode-lookup-details.html +1 -1
- package/package.json +1 -1
- package/src/server/plugins/engine/components/FormComponent.ts +14 -4
- package/src/server/plugins/engine/components/UkAddressField.ts +4 -11
- package/src/server/plugins/engine/components/helpers/telephone.ts +5 -1
- package/src/server/plugins/engine/views/file-upload.html +1 -1
- package/src/server/plugins/engine/views/item-delete.html +1 -1
- package/src/server/plugins/engine/views/partials/form.html +1 -1
- package/src/server/plugins/engine/views/repeat-list-summary.html +1 -1
- package/src/server/plugins/engine/views/summary.html +1 -1
- package/src/server/plugins/postcode-lookup/views/postcode-lookup-details.html +1 -1
|
@@ -9,6 +9,7 @@ export declare class FormComponent extends ComponentBase {
|
|
|
9
9
|
isFormComponent: boolean;
|
|
10
10
|
isAppendageStateSingleObject: boolean;
|
|
11
11
|
constructor(def: FormComponentsDef, props: ConstructorParameters<typeof ComponentBase>[1]);
|
|
12
|
+
getLabel(def: FormComponentsDef): string;
|
|
12
13
|
get keys(): string[];
|
|
13
14
|
getFormDataFromState(state: FormSubmissionState): FormPayload;
|
|
14
15
|
getFormValueFromState(state: FormSubmissionState): FormValue | FormPayload;
|
|
@@ -14,7 +14,16 @@ export class FormComponent extends ComponentBase {
|
|
|
14
14
|
} = def;
|
|
15
15
|
this.type = type;
|
|
16
16
|
this.hint = hint;
|
|
17
|
-
this.label =
|
|
17
|
+
this.label = this.getLabel(def);
|
|
18
|
+
}
|
|
19
|
+
getLabel(def) {
|
|
20
|
+
if ('errorDescription' in def && def.errorDescription) {
|
|
21
|
+
return def.errorDescription;
|
|
22
|
+
}
|
|
23
|
+
if ('shortDescription' in def && def.shortDescription) {
|
|
24
|
+
return def.shortDescription;
|
|
25
|
+
}
|
|
26
|
+
return def.title;
|
|
18
27
|
}
|
|
19
28
|
get keys() {
|
|
20
29
|
const {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FormComponent.js","names":["ComponentBase","optionalText","FormComponent","type","hint","label","isFormComponent","isAppendageStateSingleObject","constructor","def","props","shortDescription","title","keys","collection","name","fields","map","getFormDataFromState","state","getFormValue","getFormValueFromState","value","isValue","undefined","getStateFromValidForm","payload","getErrors","errors","list","filter","error","path","includes","length","getFirstError","getViewErrors","firstError","getViewModel","options","viewModel","isRequired","required","hideOptional","text","componentErrors","componentError","errorMessage","id","getDisplayStringFromFormValue","toString","getDisplayStringFromState","getContextValueFromFormValue","isState","values","Object","isFormValue","Array","isArray","getContextValueFromState","isFormState","getAllPossibleErrors","baseErrors","advancedSettingsErrors","onSubmit","_request","_metadata","_context","Promise","resolve","isRepeatState","every","isRepeatValue","itemId","isUploadState","isUploadValue","isGeospatialState","isGeospatialValue","uploadId"],"sources":["../../../../../src/server/plugins/engine/components/FormComponent.ts"],"sourcesContent":["import {\n type FormComponentsDef,\n type FormMetadata,\n type Item\n} from '@defra/forms-model'\n\nimport { ComponentBase } from '~/src/server/plugins/engine/components/ComponentBase.js'\nimport { optionalText } from '~/src/server/plugins/engine/components/constants.js'\nimport {\n type FormContext,\n type FormRequestPayload\n} from '~/src/server/plugins/engine/types/index.js'\nimport {\n type ErrorMessageTemplateList,\n type Feature,\n type FileState,\n type FormPayload,\n type FormState,\n type FormStateValue,\n type FormSubmissionError,\n type FormSubmissionState,\n type FormValue,\n type GeospatialState,\n type RepeatItemState,\n type RepeatListState,\n type UploadState\n} from '~/src/server/plugins/engine/types.js'\n\nexport class FormComponent extends ComponentBase {\n type: FormComponentsDef['type']\n hint: FormComponentsDef['hint']\n label: string\n\n isFormComponent = true\n isAppendageStateSingleObject = false\n\n constructor(\n def: FormComponentsDef,\n props: ConstructorParameters<typeof ComponentBase>[1]\n ) {\n super(def, props)\n\n const { hint, type } = def\n\n this.type = type\n this.hint = hint\n this.label =\n 'shortDescription' in def && def.shortDescription\n ? def.shortDescription\n : def.title\n }\n\n get keys() {\n const { collection, name } = this\n\n if (collection) {\n const { fields } = collection\n return [name, ...fields.map(({ name }) => name)]\n }\n\n return [name]\n }\n\n getFormDataFromState(state: FormSubmissionState): FormPayload {\n const { collection, name } = this\n\n if (collection) {\n return collection.getFormDataFromState(state)\n }\n\n return {\n [name]: this.getFormValue(state[name])\n }\n }\n\n getFormValueFromState(state: FormSubmissionState): FormValue | FormPayload {\n const { collection, name } = this\n\n if (collection) {\n return collection.getFormValueFromState(state)\n }\n\n return this.getFormValue(state[name])\n }\n\n getFormValue(value?: FormStateValue | FormState) {\n return this.isValue(value) ? value : undefined\n }\n\n getStateFromValidForm(payload: FormPayload): FormState {\n const { collection, name } = this\n\n if (collection) {\n return collection.getStateFromValidForm(payload)\n }\n\n return {\n [name]: this.getFormValue(payload[name]) ?? null\n }\n }\n\n getErrors(errors?: FormSubmissionError[]): FormSubmissionError[] | undefined {\n const { name } = this\n\n // Filter component and child errors only\n const list = errors?.filter(\n (error) =>\n error.name === name ||\n error.path.includes(name) ||\n this.keys.includes(error.name)\n )\n\n if (!list?.length) {\n return\n }\n\n return list\n }\n\n getFirstError(\n errors?: FormSubmissionError[]\n ): FormSubmissionError | undefined {\n return this.getErrors(errors)?.[0]\n }\n\n getViewErrors(\n errors?: FormSubmissionError[]\n ): FormSubmissionError[] | undefined {\n const firstError = this.getFirstError(errors)\n return firstError && [firstError]\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { hint, name, options = {}, title, viewModel } = this\n\n const isRequired = !('required' in options) || options.required !== false\n const hideOptional = 'optionalText' in options && options.optionalText\n const label = `${title}${!isRequired && !hideOptional ? optionalText : ''}`\n\n if (hint) {\n viewModel.hint = {\n text: hint\n }\n }\n\n // Filter component errors only\n const componentErrors = this.getErrors(errors)\n const componentError = this.getFirstError(componentErrors)\n\n if (componentErrors) {\n viewModel.errors = componentErrors\n }\n\n if (componentError) {\n viewModel.errorMessage = {\n text: componentError.text\n }\n }\n\n return {\n ...viewModel,\n label: {\n text: label\n },\n id: name,\n name,\n value: payload[name]\n }\n }\n\n getDisplayStringFromFormValue(value: FormValue | FormPayload): string {\n // Map selected values to text\n // eslint-disable-next-line @typescript-eslint/no-base-to-string\n return this.isValue(value) ? value.toString() : ''\n }\n\n getDisplayStringFromState(state: FormSubmissionState): string {\n const value = this.getFormValueFromState(state)\n return this.getDisplayStringFromFormValue(value)\n }\n\n getContextValueFromFormValue(\n value: FormValue | FormPayload\n ): Item['value'] | Item['value'][] | null {\n // Filter object field values\n if (this.isState(value)) {\n const values = Object.values(value).filter(isFormValue)\n return values.length ? values : null\n }\n\n // Filter array field values\n if (this.isValue(value) && Array.isArray(value)) {\n return value.filter(isFormValue)\n }\n\n return this.isValue(value) ? value : null\n }\n\n getContextValueFromState(\n state: FormSubmissionState\n ): Item['value'] | Item['value'][] | null {\n const value = this.getFormValueFromState(state)\n\n return this.getContextValueFromFormValue(value)\n }\n\n isValue(\n value?: FormStateValue | FormState\n ): value is NonNullable<FormStateValue> {\n return isFormValue(value)\n }\n\n isState(value?: FormStateValue | FormState): value is FormState {\n return isFormState(value)\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return FormComponent.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 advancedSettingsErrors: []\n }\n }\n\n onSubmit(\n _request: FormRequestPayload,\n _metadata: FormMetadata,\n _context: FormContext\n ): Promise<void> {\n return Promise.resolve()\n }\n}\n\n/**\n * Check for form value\n */\nexport function isFormValue(\n value?: unknown\n): value is string | number | boolean {\n return (\n (typeof value === 'string' && value.length > 0) ||\n typeof value === 'number' ||\n typeof value === 'boolean'\n )\n}\n\n/**\n * Check for form state with nested values\n */\nexport function isFormState(value?: unknown): value is FormState {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) {\n return false\n }\n\n // Skip empty objects\n return !!Object.values(value).length\n}\n\n/**\n * Check for repeat list state\n */\nexport function isRepeatState(value?: unknown): value is RepeatListState {\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(isRepeatValue)\n}\n\n/**\n * Check for repeat list value\n */\nexport function isRepeatValue(value?: unknown): value is RepeatItemState {\n return isFormState(value) && typeof value.itemId === 'string'\n}\n\n/**\n * Check for upload state\n */\nexport function isUploadState(value?: unknown): value is UploadState {\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(isUploadValue)\n}\n\n/**\n * Check for geospatial state\n */\nexport function isGeospatialState(value?: unknown): value is GeospatialState {\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(isGeospatialValue)\n}\n\n/**\n * Check for upload state value\n */\nexport function isUploadValue(value?: unknown): value is FileState {\n return isFormState(value) && typeof value.uploadId === 'string'\n}\n\n/**\n * Check for geospatial state value\n */\nexport function isGeospatialValue(value?: unknown): value is Feature {\n return (\n isFormState(value) &&\n typeof value.type === 'string' &&\n value.type === 'Feature'\n )\n}\n"],"mappings":"AAMA,SAASA,aA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;AACA;AACA;AACA,OAAO,SAASC,iBAAiBA,CAACtD,KAAe,EAA4B;EAC3E,IAAI,CAACmC,KAAK,CAACC,OAAO,CAACpC,KAAK,CAAC,EAAE;IACzB,OAAO,KAAK;EACd;;EAEA;EACA,IAAI,CAACA,KAAK,CAACY,MAAM,EAAE;IACjB,OAAO,IAAI;EACb;EAEA,OAAOZ,KAAK,CAACiD,KAAK,CAACM,iBAAiB,CAAC;AACvC;;AAEA;AACA;AACA;AACA,OAAO,SAASF,aAAaA,CAACrD,KAAe,EAAsB;EACjE,OAAOsC,WAAW,CAACtC,KAAK,CAAC,IAAI,OAAOA,KAAK,CAACwD,QAAQ,KAAK,QAAQ;AACjE;;AAEA;AACA;AACA;AACA,OAAO,SAASD,iBAAiBA,CAACvD,KAAe,EAAoB;EACnE,OACEsC,WAAW,CAACtC,KAAK,CAAC,IAClB,OAAOA,KAAK,CAACnB,IAAI,KAAK,QAAQ,IAC9BmB,KAAK,CAACnB,IAAI,KAAK,SAAS;AAE5B","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"FormComponent.js","names":["ComponentBase","optionalText","FormComponent","type","hint","label","isFormComponent","isAppendageStateSingleObject","constructor","def","props","getLabel","errorDescription","shortDescription","title","keys","collection","name","fields","map","getFormDataFromState","state","getFormValue","getFormValueFromState","value","isValue","undefined","getStateFromValidForm","payload","getErrors","errors","list","filter","error","path","includes","length","getFirstError","getViewErrors","firstError","getViewModel","options","viewModel","isRequired","required","hideOptional","text","componentErrors","componentError","errorMessage","id","getDisplayStringFromFormValue","toString","getDisplayStringFromState","getContextValueFromFormValue","isState","values","Object","isFormValue","Array","isArray","getContextValueFromState","isFormState","getAllPossibleErrors","baseErrors","advancedSettingsErrors","onSubmit","_request","_metadata","_context","Promise","resolve","isRepeatState","every","isRepeatValue","itemId","isUploadState","isUploadValue","isGeospatialState","isGeospatialValue","uploadId"],"sources":["../../../../../src/server/plugins/engine/components/FormComponent.ts"],"sourcesContent":["import {\n type FormComponentsDef,\n type FormMetadata,\n type Item\n} from '@defra/forms-model'\n\nimport { ComponentBase } from '~/src/server/plugins/engine/components/ComponentBase.js'\nimport { optionalText } from '~/src/server/plugins/engine/components/constants.js'\nimport {\n type FormContext,\n type FormRequestPayload\n} from '~/src/server/plugins/engine/types/index.js'\nimport {\n type ErrorMessageTemplateList,\n type Feature,\n type FileState,\n type FormPayload,\n type FormState,\n type FormStateValue,\n type FormSubmissionError,\n type FormSubmissionState,\n type FormValue,\n type GeospatialState,\n type RepeatItemState,\n type RepeatListState,\n type UploadState\n} from '~/src/server/plugins/engine/types.js'\n\nexport class FormComponent extends ComponentBase {\n type: FormComponentsDef['type']\n hint: FormComponentsDef['hint']\n label: string\n\n isFormComponent = true\n isAppendageStateSingleObject = false\n\n constructor(\n def: FormComponentsDef,\n props: ConstructorParameters<typeof ComponentBase>[1]\n ) {\n super(def, props)\n\n const { hint, type } = def\n\n this.type = type\n this.hint = hint\n\n this.label = this.getLabel(def)\n }\n\n getLabel(def: FormComponentsDef) {\n if ('errorDescription' in def && def.errorDescription) {\n return def.errorDescription\n }\n\n if ('shortDescription' in def && def.shortDescription) {\n return def.shortDescription\n }\n\n return def.title\n }\n\n get keys() {\n const { collection, name } = this\n\n if (collection) {\n const { fields } = collection\n return [name, ...fields.map(({ name }) => name)]\n }\n\n return [name]\n }\n\n getFormDataFromState(state: FormSubmissionState): FormPayload {\n const { collection, name } = this\n\n if (collection) {\n return collection.getFormDataFromState(state)\n }\n\n return {\n [name]: this.getFormValue(state[name])\n }\n }\n\n getFormValueFromState(state: FormSubmissionState): FormValue | FormPayload {\n const { collection, name } = this\n\n if (collection) {\n return collection.getFormValueFromState(state)\n }\n\n return this.getFormValue(state[name])\n }\n\n getFormValue(value?: FormStateValue | FormState) {\n return this.isValue(value) ? value : undefined\n }\n\n getStateFromValidForm(payload: FormPayload): FormState {\n const { collection, name } = this\n\n if (collection) {\n return collection.getStateFromValidForm(payload)\n }\n\n return {\n [name]: this.getFormValue(payload[name]) ?? null\n }\n }\n\n getErrors(errors?: FormSubmissionError[]): FormSubmissionError[] | undefined {\n const { name } = this\n\n // Filter component and child errors only\n const list = errors?.filter(\n (error) =>\n error.name === name ||\n error.path.includes(name) ||\n this.keys.includes(error.name)\n )\n\n if (!list?.length) {\n return\n }\n\n return list\n }\n\n getFirstError(\n errors?: FormSubmissionError[]\n ): FormSubmissionError | undefined {\n return this.getErrors(errors)?.[0]\n }\n\n getViewErrors(\n errors?: FormSubmissionError[]\n ): FormSubmissionError[] | undefined {\n const firstError = this.getFirstError(errors)\n return firstError && [firstError]\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { hint, name, options = {}, title, viewModel } = this\n\n const isRequired = !('required' in options) || options.required !== false\n const hideOptional = 'optionalText' in options && options.optionalText\n const label = `${title}${!isRequired && !hideOptional ? optionalText : ''}`\n\n if (hint) {\n viewModel.hint = {\n text: hint\n }\n }\n\n // Filter component errors only\n const componentErrors = this.getErrors(errors)\n const componentError = this.getFirstError(componentErrors)\n\n if (componentErrors) {\n viewModel.errors = componentErrors\n }\n\n if (componentError) {\n viewModel.errorMessage = {\n text: componentError.text\n }\n }\n\n return {\n ...viewModel,\n label: {\n text: label\n },\n id: name,\n name,\n value: payload[name]\n }\n }\n\n getDisplayStringFromFormValue(value: FormValue | FormPayload): string {\n // Map selected values to text\n // eslint-disable-next-line @typescript-eslint/no-base-to-string\n return this.isValue(value) ? value.toString() : ''\n }\n\n getDisplayStringFromState(state: FormSubmissionState): string {\n const value = this.getFormValueFromState(state)\n return this.getDisplayStringFromFormValue(value)\n }\n\n getContextValueFromFormValue(\n value: FormValue | FormPayload\n ): Item['value'] | Item['value'][] | null {\n // Filter object field values\n if (this.isState(value)) {\n const values = Object.values(value).filter(isFormValue)\n return values.length ? values : null\n }\n\n // Filter array field values\n if (this.isValue(value) && Array.isArray(value)) {\n return value.filter(isFormValue)\n }\n\n return this.isValue(value) ? value : null\n }\n\n getContextValueFromState(\n state: FormSubmissionState\n ): Item['value'] | Item['value'][] | null {\n const value = this.getFormValueFromState(state)\n\n return this.getContextValueFromFormValue(value)\n }\n\n isValue(\n value?: FormStateValue | FormState\n ): value is NonNullable<FormStateValue> {\n return isFormValue(value)\n }\n\n isState(value?: FormStateValue | FormState): value is FormState {\n return isFormState(value)\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return FormComponent.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 advancedSettingsErrors: []\n }\n }\n\n onSubmit(\n _request: FormRequestPayload,\n _metadata: FormMetadata,\n _context: FormContext\n ): Promise<void> {\n return Promise.resolve()\n }\n}\n\n/**\n * Check for form value\n */\nexport function isFormValue(\n value?: unknown\n): value is string | number | boolean {\n return (\n (typeof value === 'string' && value.length > 0) ||\n typeof value === 'number' ||\n typeof value === 'boolean'\n )\n}\n\n/**\n * Check for form state with nested values\n */\nexport function isFormState(value?: unknown): value is FormState {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) {\n return false\n }\n\n // Skip empty objects\n return !!Object.values(value).length\n}\n\n/**\n * Check for repeat list state\n */\nexport function isRepeatState(value?: unknown): value is RepeatListState {\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(isRepeatValue)\n}\n\n/**\n * Check for repeat list value\n */\nexport function isRepeatValue(value?: unknown): value is RepeatItemState {\n return isFormState(value) && typeof value.itemId === 'string'\n}\n\n/**\n * Check for upload state\n */\nexport function isUploadState(value?: unknown): value is UploadState {\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(isUploadValue)\n}\n\n/**\n * Check for geospatial state\n */\nexport function isGeospatialState(value?: unknown): value is GeospatialState {\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(isGeospatialValue)\n}\n\n/**\n * Check for upload state value\n */\nexport function isUploadValue(value?: unknown): value is FileState {\n return isFormState(value) && typeof value.uploadId === 'string'\n}\n\n/**\n * Check for geospatial state value\n */\nexport function isGeospatialValue(value?: unknown): value is Feature {\n return (\n isFormState(value) &&\n typeof value.type === 'string' &&\n value.type === 'Feature'\n )\n}\n"],"mappings":"AAMA,SAASA,aAAa;AACtB,SAASC,YAAY;AAqBrB,OAAO,MAAMC,aAAa,SAASF,aAAa,CAAC;EAC/CG,IAAI;EACJC,IAAI;EACJC,KAAK;EAELC,eAAe,GAAG,IAAI;EACtBC,4BAA4B,GAAG,KAAK;EAEpCC,WAAWA,CACTC,GAAsB,EACtBC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,EAAEC,KAAK,CAAC;IAEjB,MAAM;MAAEN,IAAI;MAAED;IAAK,CAAC,GAAGM,GAAG;IAE1B,IAAI,CAACN,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACC,IAAI,GAAGA,IAAI;IAEhB,IAAI,CAACC,KAAK,GAAG,IAAI,CAACM,QAAQ,CAACF,GAAG,CAAC;EACjC;EAEAE,QAAQA,CAACF,GAAsB,EAAE;IAC/B,IAAI,kBAAkB,IAAIA,GAAG,IAAIA,GAAG,CAACG,gBAAgB,EAAE;MACrD,OAAOH,GAAG,CAACG,gBAAgB;IAC7B;IAEA,IAAI,kBAAkB,IAAIH,GAAG,IAAIA,GAAG,CAACI,gBAAgB,EAAE;MACrD,OAAOJ,GAAG,CAACI,gBAAgB;IAC7B;IAEA,OAAOJ,GAAG,CAACK,KAAK;EAClB;EAEA,IAAIC,IAAIA,CAAA,EAAG;IACT,MAAM;MAAEC,UAAU;MAAEC;IAAK,CAAC,GAAG,IAAI;IAEjC,IAAID,UAAU,EAAE;MACd,MAAM;QAAEE;MAAO,CAAC,GAAGF,UAAU;MAC7B,OAAO,CAACC,IAAI,EAAE,GAAGC,MAAM,CAACC,GAAG,CAAC,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b;EAEA,OAAOZ,KAAK,CAACiD,KAAK,CAACC,aAAa,CAAC;AACnC;;AAEA;AACA;AACA;AACA,OAAO,SAASA,aAAaA,CAAClD,KAAe,EAA4B;EACvE,OAAOsC,WAAW,CAACtC,KAAK,CAAC,IAAI,OAAOA,KAAK,CAACmD,MAAM,KAAK,QAAQ;AAC/D;;AAEA;AACA;AACA;AACA,OAAO,SAASC,aAAaA,CAACpD,KAAe,EAAwB;EACnE,IAAI,CAACmC,KAAK,CAACC,OAAO,CAACpC,KAAK,CAAC,EAAE;IACzB,OAAO,KAAK;EACd;;EAEA;EACA,IAAI,CAACA,KAAK,CAACY,MAAM,EAAE;IACjB,OAAO,IAAI;EACb;EAEA,OAAOZ,KAAK,CAACiD,KAAK,CAACI,aAAa,CAAC;AACnC;;AAEA;AACA;AACA;AACA,OAAO,SAASC,iBAAiBA,CAACtD,KAAe,EAA4B;EAC3E,IAAI,CAACmC,KAAK,CAACC,OAAO,CAACpC,KAAK,CAAC,EAAE;IACzB,OAAO,KAAK;EACd;;EAEA;EACA,IAAI,CAACA,KAAK,CAACY,MAAM,EAAE;IACjB,OAAO,IAAI;EACb;EAEA,OAAOZ,KAAK,CAACiD,KAAK,CAACM,iBAAiB,CAAC;AACvC;;AAEA;AACA;AACA;AACA,OAAO,SAASF,aAAaA,CAACrD,KAAe,EAAsB;EACjE,OAAOsC,WAAW,CAACtC,KAAK,CAAC,IAAI,OAAOA,KAAK,CAACwD,QAAQ,KAAK,QAAQ;AACjE;;AAEA;AACA;AACA;AACA,OAAO,SAASD,iBAAiBA,CAACvD,KAAe,EAAoB;EACnE,OACEsC,WAAW,CAACtC,KAAK,CAAC,IAClB,OAAOA,KAAK,CAACrB,IAAI,KAAK,QAAQ,IAC9BqB,KAAK,CAACrB,IAAI,KAAK,SAAS;AAE5B","ignoreList":[]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type UkAddressFieldComponent } from '@defra/forms-model';
|
|
2
2
|
import { type ObjectSchema } from 'joi';
|
|
3
3
|
import { ComponentCollection } from '../../../../server/plugins/engine/components/ComponentCollection.js';
|
|
4
4
|
import { FormComponent } from '../../../../server/plugins/engine/components/FormComponent.js';
|
|
@@ -9,7 +9,6 @@ export declare class UkAddressField extends FormComponent {
|
|
|
9
9
|
formSchema: ObjectSchema<FormPayload>;
|
|
10
10
|
stateSchema: ObjectSchema<FormState>;
|
|
11
11
|
collection: ComponentCollection;
|
|
12
|
-
shortDescription: FormComponentsDef['shortDescription'];
|
|
13
12
|
constructor(def: UkAddressFieldComponent, props: ConstructorParameters<typeof FormComponent>[1]);
|
|
14
13
|
getFormValueFromState(state: FormSubmissionState): UkAddressState | undefined;
|
|
15
14
|
getContextValueFromFormValue(value: UkAddressState | undefined): string[] | null;
|
|
@@ -5,13 +5,11 @@ import { FormComponent, isFormState } from "./FormComponent.js";
|
|
|
5
5
|
import { TextField } from "./TextField.js";
|
|
6
6
|
import { dispatch } from "../../postcode-lookup/routes/index.js";
|
|
7
7
|
export class UkAddressField extends FormComponent {
|
|
8
|
-
shortDescription;
|
|
9
8
|
constructor(def, props) {
|
|
10
9
|
super(def, props);
|
|
11
10
|
const {
|
|
12
11
|
name,
|
|
13
|
-
options
|
|
14
|
-
shortDescription
|
|
12
|
+
options
|
|
15
13
|
} = def;
|
|
16
14
|
const isRequired = options.required !== false;
|
|
17
15
|
const hideOptional = !!options.optionalText;
|
|
@@ -94,7 +92,6 @@ export class UkAddressField extends FormComponent {
|
|
|
94
92
|
this.options = options;
|
|
95
93
|
this.formSchema = this.collection.formSchema;
|
|
96
94
|
this.stateSchema = this.collection.stateSchema;
|
|
97
|
-
this.shortDescription = shortDescription;
|
|
98
95
|
}
|
|
99
96
|
getFormValueFromState(state) {
|
|
100
97
|
const value = super.getFormValueFromState(state);
|
|
@@ -129,13 +126,13 @@ export class UkAddressField extends FormComponent {
|
|
|
129
126
|
if (this.shouldUsePostcodeLookup() && uniqueErrors?.length) {
|
|
130
127
|
const {
|
|
131
128
|
name,
|
|
132
|
-
|
|
129
|
+
label
|
|
133
130
|
} = this;
|
|
134
131
|
return [{
|
|
135
132
|
name,
|
|
136
133
|
path: [name],
|
|
137
134
|
href: `#${name}`,
|
|
138
|
-
text: `Enter ${lowerFirst(
|
|
135
|
+
text: `Enter ${lowerFirst(label)}`
|
|
139
136
|
}];
|
|
140
137
|
}
|
|
141
138
|
return uniqueErrors;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UkAddressField.js","names":["ComponentType","lowerFirst","ComponentCollection","FormComponent","isFormState","TextField","dispatch","UkAddressField","shortDescription","constructor","def","props","name","options","isRequired","required","hideOptional","optionalText","hideTitle","collection","type","title","schema","classes","max","autocomplete","regex","parent","formSchema","stateSchema","getFormValueFromState","state","value","isState","undefined","getContextValueFromFormValue","Object","entries","filter","key","Boolean","map","getContextValueFromState","getDisplayStringFromFormValue","join","getDisplayStringFromState","getViewErrors","errors","uniqueErrors","getErrors","error","index","self","findIndex","err","shouldUsePostcodeLookup","length","path","href","text","getViewModel","payload","viewModel","fieldset","hint","label","legend","id","attributes","components","uprn","at","Error","model","formGroup","usePostcodeLookup","isUkAddress","getAllPossibleErrors","ordnanceSurveyApiKey","baseErrors","template","advancedSettingsErrors","isText","addressLine1","town","postcode","dispatcher","request","h","args","controller","component","formName","componentName","componentHint","componentTitle","step","actionArgs","sourceUrl"],"sources":["../../../../../src/server/plugins/engine/components/UkAddressField.ts"],"sourcesContent":["import {\n ComponentType,\n type FormComponentsDef,\n type UkAddressFieldComponent\n} from '@defra/forms-model'\nimport { type ObjectSchema } from 'joi'\nimport lowerFirst from 'lodash/lowerFirst.js'\n\nimport { ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js'\nimport {\n FormComponent,\n isFormState\n} from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { TextField } from '~/src/server/plugins/engine/components/TextField.js'\nimport { type QuestionPageController } from '~/src/server/plugins/engine/pageControllers/QuestionPageController.js'\nimport {\n type FormRequestPayload,\n type FormResponseToolkit\n} from '~/src/server/plugins/engine/types/index.js'\nimport {\n type ErrorMessageTemplateList,\n type FormPayload,\n type FormState,\n type FormStateValue,\n type FormSubmissionError,\n type FormSubmissionState,\n type PostcodeLookupExternalArgs\n} from '~/src/server/plugins/engine/types.js'\nimport { dispatch } from '~/src/server/plugins/postcode-lookup/routes/index.js'\n\nexport class UkAddressField extends FormComponent {\n declare options: UkAddressFieldComponent['options']\n declare formSchema: ObjectSchema<FormPayload>\n declare stateSchema: ObjectSchema<FormState>\n declare collection: ComponentCollection\n\n shortDescription: FormComponentsDef['shortDescription']\n\n constructor(\n def: UkAddressFieldComponent,\n props: ConstructorParameters<typeof FormComponent>[1]\n ) {\n super(def, props)\n\n const { name, options, shortDescription } = def\n\n const isRequired = options.required !== false\n const hideOptional = !!options.optionalText\n const hideTitle = !!options.hideTitle\n\n this.collection = new ComponentCollection(\n [\n {\n type: ComponentType.TextField,\n name: `${name}__uprn`,\n title: 'UPRN',\n schema: {},\n options: {\n required: false,\n classes: 'hidden'\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__addressLine1`,\n title: 'Address line 1',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-line1',\n required: isRequired,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__addressLine2`,\n title: 'Address line 2',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-line2',\n required: false,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__town`,\n title: 'Town or city',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-level2',\n classes: 'govuk-!-width-two-thirds',\n required: isRequired,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__county`,\n title: 'County',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-level1',\n required: false,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__postcode`,\n title: 'Postcode',\n schema: {\n regex: '^[a-zA-Z]{1,2}\\\\d[a-zA-Z\\\\d]?\\\\s?\\\\d[a-zA-Z]{2}$'\n },\n options: {\n autocomplete: 'postal-code',\n classes: 'govuk-input--width-10',\n required: isRequired,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n }\n ],\n { ...props, parent: this }\n )\n\n this.options = options\n this.formSchema = this.collection.formSchema\n this.stateSchema = this.collection.stateSchema\n this.shortDescription = shortDescription\n }\n\n getFormValueFromState(state: FormSubmissionState) {\n const value = super.getFormValueFromState(state)\n return this.isState(value) ? value : undefined\n }\n\n getContextValueFromFormValue(value: UkAddressState | undefined) {\n if (!value) {\n return null\n }\n\n return Object.entries(value)\n .filter(([key, value]) => key !== 'uprn' && Boolean(value))\n .map(([, value]) => value)\n }\n\n getContextValueFromState(state: FormSubmissionState) {\n const value = this.getFormValueFromState(state)\n\n return this.getContextValueFromFormValue(value)\n }\n\n getDisplayStringFromFormValue(value: UkAddressState | undefined): string {\n return this.getContextValueFromFormValue(value)?.join(', ') ?? ''\n }\n\n getDisplayStringFromState(state: FormSubmissionState) {\n const value = this.getFormValueFromState(state)\n\n return this.getDisplayStringFromFormValue(value)\n }\n\n /**\n * Returns one error per child field\n */\n getViewErrors(\n errors?: FormSubmissionError[]\n ): FormSubmissionError[] | undefined {\n const uniqueErrors = this.getErrors(errors)?.filter(\n (error, index, self) =>\n index === self.findIndex((err) => err.name === error.name)\n )\n\n // When using postcode lookup, the address fields are hidden\n // so we replace any individual validation messages with a single one\n if (this.shouldUsePostcodeLookup() && uniqueErrors?.length) {\n const { name, shortDescription } = this\n\n return [\n {\n name,\n path: [name],\n href: `#${name}`,\n text: `Enter ${lowerFirst(shortDescription)}`\n }\n ]\n }\n\n return uniqueErrors\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { collection, name, options } = this\n\n const viewModel = super.getViewModel(payload, errors)\n let { fieldset, hint, label } = viewModel\n\n fieldset ??= {\n legend: {\n text: label.text,\n\n /**\n * For screen readers, only hide legend visually. This can be overridden\n * by single component {@link QuestionPageController | `showTitle` handling}\n */\n classes: options.hideTitle\n ? 'govuk-visually-hidden'\n : 'govuk-fieldset__legend--m'\n }\n }\n\n if (hint) {\n hint.id ??= `${name}-hint`\n fieldset.attributes ??= {\n 'aria-describedby': hint.id\n }\n }\n\n const components = collection.getViewModel(payload, errors)\n\n // Hide UPRN\n const uprn = components.at(0)\n\n if (!uprn) {\n throw new Error('No UPRN')\n }\n\n uprn.model.formGroup = { classes: 'app-hidden' }\n\n // Postcode lookup\n const usePostcodeLookup = this.shouldUsePostcodeLookup()\n\n const value = usePostcodeLookup\n ? this.getDisplayStringFromState(payload)\n : undefined\n\n return {\n ...viewModel,\n value,\n fieldset,\n components,\n usePostcodeLookup\n }\n }\n\n isState(value?: FormStateValue | FormState): value is UkAddressState {\n return UkAddressField.isUkAddress(value)\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return UkAddressField.getAllPossibleErrors()\n }\n\n private shouldUsePostcodeLookup() {\n return !!(this.options.usePostcodeLookup && this.model.ordnanceSurveyApiKey)\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: 'Enter address line 1' },\n { type: 'required', template: 'Enter town or city' },\n { type: 'required', template: 'Enter postcode' },\n { type: 'format', template: 'Enter valid postcode' }\n ],\n advancedSettingsErrors: []\n }\n }\n\n static isUkAddress(\n value?: FormStateValue | FormState\n ): value is UkAddressState {\n return (\n isFormState(value) &&\n TextField.isText(value.addressLine1) &&\n TextField.isText(value.town) &&\n TextField.isText(value.postcode)\n )\n }\n\n // eslint-disable-next-line @typescript-eslint/require-await\n static async dispatcher(\n request: FormRequestPayload,\n h: FormResponseToolkit,\n args: PostcodeLookupExternalArgs\n ) {\n const { controller, component } = args\n\n return dispatch(request, h, {\n formName: controller.model.name,\n componentName: component.name,\n componentHint: component.hint,\n componentTitle: component.title || controller.title,\n step: args.actionArgs.step,\n sourceUrl: args.sourceUrl\n })\n }\n}\n\nexport interface UkAddressState extends Record<string, string> {\n uprn: string\n addressLine1: string\n addressLine2: string\n town: string\n county: string\n postcode: string\n}\n"],"mappings":"AAAA,SACEA,aAAa,QAGR,oBAAoB;AAE3B,OAAOC,UAAU,MAAM,sBAAsB;AAE7C,SAASC,mBAAmB;AAC5B,SACEC,aAAa,EACbC,WAAW;AAEb,SAASC,SAAS;AAelB,SAASC,QAAQ;AAEjB,OAAO,MAAMC,cAAc,SAASJ,aAAa,CAAC;EAMhDK,gBAAgB;EAEhBC,WAAWA,CACTC,GAA4B,EAC5BC,KAAqD,EACrD;IACA,KAAK,CAACD,GAAG,EAAEC,KAAK,CAAC;IAEjB,MAAM;MAAEC,IAAI;MAAEC,OAAO;MAAEL;IAAiB,CAAC,GAAGE,GAAG;IAE/C,MAAMI,UAAU,GAAGD,OAAO,CAACE,QAAQ,KAAK,KAAK;IAC7C,MAAMC,YAAY,GAAG,CAAC,CAACH,OAAO,CAACI,YAAY;IAC3C,MAAMC,SAAS,GAAG,CAAC,CAACL,OAAO,CAACK,SAAS;IAErC,IAAI,CAACC,UAAU,GAAG,IAAIjB,mBAAmB,CACvC,CACE;MACEkB,IAAI,EAAEpB,aAAa,CAACK,SAAS;MAC7BO,IAAI,EAAE,GAAGA,IAAI,QAAQ;MACrBS,KAAK,EAAE,MAAM;MACbC,MAAM,EAAE,CAAC,CAAC;MACVT,OAAO,EAAE;QACPE,QAAQ,EAAE,KAAK;QACfQ,OAAO,EAAE;MACX;IACF,CAAC,EACD;MACEH,IAAI,EAAEpB,aAAa,CAACK,SAAS;MAC7BO,IAAI,EAAE,GAAGA,IAAI,gBAAgB;MAC7BS,KAAK,EAAE,gBAAgB;MACvBC,MAAM,EAAE;QAAEE,GAAG,EAAE;MAAI,CAA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D,OAAO;MACLE,UAAU,EAAE,CACV;QAAE3D,IAAI,EAAE,UAAU;QAAE4D,QAAQ,EAAE;MAAuB,CAAC,EACtD;QAAE5D,IAAI,EAAE,UAAU;QAAE4D,QAAQ,EAAE;MAAqB,CAAC,EACpD;QAAE5D,IAAI,EAAE,UAAU;QAAE4D,QAAQ,EAAE;MAAiB,CAAC,EAChD;QAAE5D,IAAI,EAAE,QAAQ;QAAE4D,QAAQ,EAAE;MAAuB,CAAC,CACrD;MACDC,sBAAsB,EAAE;IAC1B,CAAC;EACH;EAEA,OAAOL,WAAWA,CAChB5C,KAAkC,EACT;IACzB,OACE5B,WAAW,CAAC4B,KAAK,CAAC,IAClB3B,SAAS,CAAC6E,MAAM,CAAClD,KAAK,CAACmD,YAAY,CAAC,IACpC9E,SAAS,CAAC6E,MAAM,CAAClD,KAAK,CAACoD,IAAI,CAAC,IAC5B/E,SAAS,CAAC6E,MAAM,CAAClD,KAAK,CAACqD,QAAQ,CAAC;EAEpC;;EAEA;EACA,aAAaC,UAAUA,CACrBC,OAA2B,EAC3BC,CAAsB,EACtBC,IAAgC,EAChC;IACA,MAAM;MAAEC,UAAU;MAAEC;IAAU,CAAC,GAAGF,IAAI;IAEtC,OAAOnF,QAAQ,CAACiF,OAAO,EAAEC,CAAC,EAAE;MAC1BI,QAAQ,EAAEF,UAAU,CAACjB,KAAK,CAAC7D,IAAI;MAC/BiF,aAAa,EAAEF,SAAS,CAAC/E,IAAI;MAC7BkF,aAAa,EAAEH,SAAS,CAAC3B,IAAI;MAC7B+B,cAAc,EAAEJ,SAAS,CAACtE,KAAK,IAAIqE,UAAU,CAACrE,KAAK;MACnD2E,IAAI,EAAEP,IAAI,CAACQ,UAAU,CAACD,IAAI;MAC1BE,SAAS,EAAET,IAAI,CAACS;IAClB,CAAC,CAAC;EACJ;AACF","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"UkAddressField.js","names":["ComponentType","lowerFirst","ComponentCollection","FormComponent","isFormState","TextField","dispatch","UkAddressField","constructor","def","props","name","options","isRequired","required","hideOptional","optionalText","hideTitle","collection","type","title","schema","classes","max","autocomplete","regex","parent","formSchema","stateSchema","getFormValueFromState","state","value","isState","undefined","getContextValueFromFormValue","Object","entries","filter","key","Boolean","map","getContextValueFromState","getDisplayStringFromFormValue","join","getDisplayStringFromState","getViewErrors","errors","uniqueErrors","getErrors","error","index","self","findIndex","err","shouldUsePostcodeLookup","length","label","path","href","text","getViewModel","payload","viewModel","fieldset","hint","legend","id","attributes","components","uprn","at","Error","model","formGroup","usePostcodeLookup","isUkAddress","getAllPossibleErrors","ordnanceSurveyApiKey","baseErrors","template","advancedSettingsErrors","isText","addressLine1","town","postcode","dispatcher","request","h","args","controller","component","formName","componentName","componentHint","componentTitle","step","actionArgs","sourceUrl"],"sources":["../../../../../src/server/plugins/engine/components/UkAddressField.ts"],"sourcesContent":["import { ComponentType, type UkAddressFieldComponent } from '@defra/forms-model'\nimport { type ObjectSchema } from 'joi'\nimport lowerFirst from 'lodash/lowerFirst.js'\n\nimport { ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js'\nimport {\n FormComponent,\n isFormState\n} from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { TextField } from '~/src/server/plugins/engine/components/TextField.js'\nimport { type QuestionPageController } from '~/src/server/plugins/engine/pageControllers/QuestionPageController.js'\nimport {\n type FormRequestPayload,\n type FormResponseToolkit\n} from '~/src/server/plugins/engine/types/index.js'\nimport {\n type ErrorMessageTemplateList,\n type FormPayload,\n type FormState,\n type FormStateValue,\n type FormSubmissionError,\n type FormSubmissionState,\n type PostcodeLookupExternalArgs\n} from '~/src/server/plugins/engine/types.js'\nimport { dispatch } from '~/src/server/plugins/postcode-lookup/routes/index.js'\n\nexport class UkAddressField extends FormComponent {\n declare options: UkAddressFieldComponent['options']\n declare formSchema: ObjectSchema<FormPayload>\n declare stateSchema: ObjectSchema<FormState>\n declare collection: ComponentCollection\n\n constructor(\n def: UkAddressFieldComponent,\n props: ConstructorParameters<typeof FormComponent>[1]\n ) {\n super(def, props)\n\n const { name, options } = def\n\n const isRequired = options.required !== false\n const hideOptional = !!options.optionalText\n const hideTitle = !!options.hideTitle\n\n this.collection = new ComponentCollection(\n [\n {\n type: ComponentType.TextField,\n name: `${name}__uprn`,\n title: 'UPRN',\n schema: {},\n options: {\n required: false,\n classes: 'hidden'\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__addressLine1`,\n title: 'Address line 1',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-line1',\n required: isRequired,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__addressLine2`,\n title: 'Address line 2',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-line2',\n required: false,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__town`,\n title: 'Town or city',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-level2',\n classes: 'govuk-!-width-two-thirds',\n required: isRequired,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__county`,\n title: 'County',\n schema: { max: 100 },\n options: {\n autocomplete: 'address-level1',\n required: false,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n },\n {\n type: ComponentType.TextField,\n name: `${name}__postcode`,\n title: 'Postcode',\n schema: {\n regex: '^[a-zA-Z]{1,2}\\\\d[a-zA-Z\\\\d]?\\\\s?\\\\d[a-zA-Z]{2}$'\n },\n options: {\n autocomplete: 'postal-code',\n classes: 'govuk-input--width-10',\n required: isRequired,\n optionalText: !isRequired && (hideOptional || !hideTitle)\n }\n }\n ],\n { ...props, parent: this }\n )\n\n this.options = options\n this.formSchema = this.collection.formSchema\n this.stateSchema = this.collection.stateSchema\n }\n\n getFormValueFromState(state: FormSubmissionState) {\n const value = super.getFormValueFromState(state)\n return this.isState(value) ? value : undefined\n }\n\n getContextValueFromFormValue(value: UkAddressState | undefined) {\n if (!value) {\n return null\n }\n\n return Object.entries(value)\n .filter(([key, value]) => key !== 'uprn' && Boolean(value))\n .map(([, value]) => value)\n }\n\n getContextValueFromState(state: FormSubmissionState) {\n const value = this.getFormValueFromState(state)\n\n return this.getContextValueFromFormValue(value)\n }\n\n getDisplayStringFromFormValue(value: UkAddressState | undefined): string {\n return this.getContextValueFromFormValue(value)?.join(', ') ?? ''\n }\n\n getDisplayStringFromState(state: FormSubmissionState) {\n const value = this.getFormValueFromState(state)\n\n return this.getDisplayStringFromFormValue(value)\n }\n\n /**\n * Returns one error per child field\n */\n getViewErrors(\n errors?: FormSubmissionError[]\n ): FormSubmissionError[] | undefined {\n const uniqueErrors = this.getErrors(errors)?.filter(\n (error, index, self) =>\n index === self.findIndex((err) => err.name === error.name)\n )\n\n // When using postcode lookup, the address fields are hidden\n // so we replace any individual validation messages with a single one\n if (this.shouldUsePostcodeLookup() && uniqueErrors?.length) {\n const { name, label } = this\n\n return [\n {\n name,\n path: [name],\n href: `#${name}`,\n text: `Enter ${lowerFirst(label)}`\n }\n ]\n }\n\n return uniqueErrors\n }\n\n getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) {\n const { collection, name, options } = this\n\n const viewModel = super.getViewModel(payload, errors)\n let { fieldset, hint, label } = viewModel\n\n fieldset ??= {\n legend: {\n text: label.text,\n\n /**\n * For screen readers, only hide legend visually. This can be overridden\n * by single component {@link QuestionPageController | `showTitle` handling}\n */\n classes: options.hideTitle\n ? 'govuk-visually-hidden'\n : 'govuk-fieldset__legend--m'\n }\n }\n\n if (hint) {\n hint.id ??= `${name}-hint`\n fieldset.attributes ??= {\n 'aria-describedby': hint.id\n }\n }\n\n const components = collection.getViewModel(payload, errors)\n\n // Hide UPRN\n const uprn = components.at(0)\n\n if (!uprn) {\n throw new Error('No UPRN')\n }\n\n uprn.model.formGroup = { classes: 'app-hidden' }\n\n // Postcode lookup\n const usePostcodeLookup = this.shouldUsePostcodeLookup()\n\n const value = usePostcodeLookup\n ? this.getDisplayStringFromState(payload)\n : undefined\n\n return {\n ...viewModel,\n value,\n fieldset,\n components,\n usePostcodeLookup\n }\n }\n\n isState(value?: FormStateValue | FormState): value is UkAddressState {\n return UkAddressField.isUkAddress(value)\n }\n\n /**\n * For error preview page that shows all possible errors on a component\n */\n getAllPossibleErrors(): ErrorMessageTemplateList {\n return UkAddressField.getAllPossibleErrors()\n }\n\n private shouldUsePostcodeLookup() {\n return !!(this.options.usePostcodeLookup && this.model.ordnanceSurveyApiKey)\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: 'Enter address line 1' },\n { type: 'required', template: 'Enter town or city' },\n { type: 'required', template: 'Enter postcode' },\n { type: 'format', template: 'Enter valid postcode' }\n ],\n advancedSettingsErrors: []\n }\n }\n\n static isUkAddress(\n value?: FormStateValue | FormState\n ): value is UkAddressState {\n return (\n isFormState(value) &&\n TextField.isText(value.addressLine1) &&\n TextField.isText(value.town) &&\n TextField.isText(value.postcode)\n )\n }\n\n // eslint-disable-next-line @typescript-eslint/require-await\n static async dispatcher(\n request: FormRequestPayload,\n h: FormResponseToolkit,\n args: PostcodeLookupExternalArgs\n ) {\n const { controller, component } = args\n\n return dispatch(request, h, {\n formName: controller.model.name,\n componentName: component.name,\n componentHint: component.hint,\n componentTitle: component.title || controller.title,\n step: args.actionArgs.step,\n sourceUrl: args.sourceUrl\n })\n }\n}\n\nexport interface UkAddressState extends Record<string, string> {\n uprn: string\n addressLine1: string\n addressLine2: string\n town: string\n county: string\n postcode: string\n}\n"],"mappings":"AAAA,SAASA,aAAa,QAAsC,oBAAoB;AAEhF,OAAOC,UAAU,MAAM,sBAAsB;AAE7C,SAASC,mBAAmB;AAC5B,SACEC,aAAa,EACbC,WAAW;AAEb,SAASC,SAAS;AAelB,SAASC,QAAQ;AAEjB,OAAO,MAAMC,cAAc,SAASJ,aAAa,CAAC;EAMhDK,WAAWA,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AC,IAC5B9E,SAAS,CAAC4E,MAAM,CAAClD,KAAK,CAACqD,QAAQ,CAAC;EAEpC;;EAEA;EACA,aAAaC,UAAUA,CACrBC,OAA2B,EAC3BC,CAAsB,EACtBC,IAAgC,EAChC;IACA,MAAM;MAAEC,UAAU;MAAEC;IAAU,CAAC,GAAGF,IAAI;IAEtC,OAAOlF,QAAQ,CAACgF,OAAO,EAAEC,CAAC,EAAE;MAC1BI,QAAQ,EAAEF,UAAU,CAACjB,KAAK,CAAC7D,IAAI;MAC/BiF,aAAa,EAAEF,SAAS,CAAC/E,IAAI;MAC7BkF,aAAa,EAAEH,SAAS,CAAC1B,IAAI;MAC7B8B,cAAc,EAAEJ,SAAS,CAACtE,KAAK,IAAIqE,UAAU,CAACrE,KAAK;MACnD2E,IAAI,EAAEP,IAAI,CAACQ,UAAU,CAACD,IAAI;MAC1BE,SAAS,EAAET,IAAI,CAACS;IAClB,CAAC,CAAC;EACJ;AACF","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telephone.js","names":["TelephoneNumberFieldOptionsFormatEnum","LibPhoneNumber","JoiBase","messageTemplate","phoneUtil","PhoneNumberUtil","getInstance","COUNTRY","INVALID_ERROR_CODE","isUKNumber","value","isValidNumberForRegion","joi","extend","type","base","string","messages","format","rules","phoneNumber","method","$_addRule","name","args","ref","assert","valid","International","UK","validate","error","parsed","parse","isValidNumber","isUK"],"sources":["../../../../../../src/server/plugins/engine/components/helpers/telephone.ts"],"sourcesContent":["import { TelephoneNumberFieldOptionsFormatEnum } from '@defra/forms-model'\nimport LibPhoneNumber from 'google-libphonenumber'\nimport JoiBase, { type LanguageMessages } from 'joi'\n\nimport { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'\n\nconst phoneUtil = LibPhoneNumber.PhoneNumberUtil.getInstance()\n\nexport const COUNTRY = 'GB'\nexport const INVALID_ERROR_CODE = 'phoneNumber.invalid'\n\nexport const isUKNumber = (value: LibPhoneNumber.PhoneNumber) => {\n return phoneUtil.isValidNumberForRegion(value, COUNTRY)\n}\n\nexport const joi = JoiBase.extend({\n type: 'string',\n base: JoiBase.string(),\n messages: {\n [INVALID_ERROR_CODE]: messageTemplate.format\n } as LanguageMessages,\n rules: {\n phoneNumber: {\n method({\n format\n }: { format?: TelephoneNumberFieldOptionsFormatEnum } = {}) {\n return this.$_addRule({\n name: 'phoneNumber',\n args: { format }\n })\n },\n args: [\n {\n name: 'format',\n ref: true,\n assert: JoiBase.valid(\n TelephoneNumberFieldOptionsFormatEnum.International,\n TelephoneNumberFieldOptionsFormatEnum.UK\n )\n }\n ],\n validate(value
|
|
1
|
+
{"version":3,"file":"telephone.js","names":["TelephoneNumberFieldOptionsFormatEnum","LibPhoneNumber","JoiBase","messageTemplate","phoneUtil","PhoneNumberUtil","getInstance","COUNTRY","INVALID_ERROR_CODE","isUKNumber","value","isValidNumberForRegion","joi","extend","type","base","string","messages","format","rules","phoneNumber","method","$_addRule","name","args","ref","assert","valid","International","UK","validate","error","parsed","parse","isValidNumber","isUK"],"sources":["../../../../../../src/server/plugins/engine/components/helpers/telephone.ts"],"sourcesContent":["import { TelephoneNumberFieldOptionsFormatEnum } from '@defra/forms-model'\nimport LibPhoneNumber from 'google-libphonenumber'\nimport JoiBase, { type LanguageMessages } from 'joi'\n\nimport { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'\n\nconst phoneUtil = LibPhoneNumber.PhoneNumberUtil.getInstance()\n\nexport const COUNTRY = 'GB'\nexport const INVALID_ERROR_CODE = 'phoneNumber.invalid'\n\nexport const isUKNumber = (value: LibPhoneNumber.PhoneNumber) => {\n return phoneUtil.isValidNumberForRegion(value, COUNTRY)\n}\n\nexport const joi = JoiBase.extend({\n type: 'string',\n base: JoiBase.string(),\n messages: {\n [INVALID_ERROR_CODE]: messageTemplate.format\n } as LanguageMessages,\n rules: {\n phoneNumber: {\n method({\n format\n }: { format?: TelephoneNumberFieldOptionsFormatEnum } = {}) {\n return this.$_addRule({\n name: 'phoneNumber',\n args: { format }\n })\n },\n args: [\n {\n name: 'format',\n ref: true,\n assert: JoiBase.valid(\n TelephoneNumberFieldOptionsFormatEnum.International,\n TelephoneNumberFieldOptionsFormatEnum.UK\n )\n }\n ],\n validate(\n value: string,\n { error }: JoiBase.CustomHelpers,\n args: { format?: TelephoneNumberFieldOptionsFormatEnum }\n ) {\n const format = args.format\n\n try {\n const parsed = phoneUtil.parse(value, COUNTRY)\n\n if (!phoneUtil.isValidNumber(parsed)) {\n return error(INVALID_ERROR_CODE)\n }\n\n if (format) {\n const isUK = isUKNumber(parsed)\n\n if (\n (!isUK && format === TelephoneNumberFieldOptionsFormatEnum.UK) ||\n (isUK &&\n format === TelephoneNumberFieldOptionsFormatEnum.International)\n ) {\n return error(INVALID_ERROR_CODE)\n }\n }\n\n return value\n } catch {\n return error(INVALID_ERROR_CODE)\n }\n }\n }\n }\n}) as JoiBase.Root\n"],"mappings":"AAAA,SAASA,qCAAqC,QAAQ,oBAAoB;AAC1E,OAAOC,cAAc,MAAM,uBAAuB;AAClD,OAAOC,OAAO,MAAiC,KAAK;AAEpD,SAASC,eAAe;AAExB,MAAMC,SAAS,GAAGH,cAAc,CAACI,eAAe,CAACC,WAAW,CAAC,CAAC;AAE9D,OAAO,MAAMC,OAAO,GAAG,IAAI;AAC3B,OAAO,MAAMC,kBAAkB,GAAG,qBAAqB;AAEvD,OAAO,MAAMC,UAAU,GAAIC,KAAiC,IAAK;EAC/D,OAAON,SAAS,CAACO,sBAAsB,CAACD,KAAK,EAAEH,OAAO,CAAC;AACzD,CAAC;AAED,OAAO,MAAMK,GAAG,GAAGV,OAAO,CAACW,MAAM,CAAC;EAChCC,IAAI,EAAE,QAAQ;EACdC,IAAI,EAAEb,OAAO,CAACc,MAAM,CAAC,CAAC;EACtBC,QAAQ,EAAE;IACR,CAACT,kBAAkB,GAAGL,eAAe,CAACe;EACxC,CAAqB;EACrBC,KAAK,EAAE;IACLC,WAAW,EAAE;MACXC,MAAMA,CAAC;QACLH;MACkD,CAAC,GAAG,CAAC,CAAC,EAAE;QAC1D,OAAO,IAAI,CAACI,SAAS,CAAC;UACpBC,IAAI,EAAE,aAAa;UACnBC,IAAI,EAAE;YAAEN;UAAO;QACjB,CAAC,CAAC;MACJ,CAAC;MACDM,IAAI,EAAE,CACJ;QACED,IAAI,EAAE,QAAQ;QACdE,GAAG,EAAE,IAAI;QACTC,MAAM,EAAExB,OAAO,CAACyB,KAAK,CACnB3B,qCAAqC,CAAC4B,aAAa,EACnD5B,qCAAqC,CAAC6B,EACxC;MACF,CAAC,CACF;MACDC,QAAQA,CACNpB,KAAa,EACb;QAAEqB;MAA6B,CAAC,EAChCP,IAAwD,EACxD;QACA,MAAMN,MAAM,GAAGM,IAAI,CAACN,MAAM;QAE1B,IAAI;UACF,MAAMc,MAAM,GAAG5B,SAAS,CAAC6B,KAAK,CAACvB,KAAK,EAAEH,OAAO,CAAC;UAE9C,IAAI,CAACH,SAAS,CAAC8B,aAAa,CAACF,MAAM,CAAC,EAAE;YACpC,OAAOD,KAAK,CAACvB,kBAAkB,CAAC;UAClC;UAEA,IAAIU,MAAM,EAAE;YACV,MAAMiB,IAAI,GAAG1B,UAAU,CAACuB,MAAM,CAAC;YAE/B,IACG,CAACG,IAAI,IAAIjB,MAAM,KAAKlB,qCAAqC,CAAC6B,EAAE,IAC5DM,IAAI,IACHjB,MAAM,KAAKlB,qCAAqC,CAAC4B,aAAc,EACjE;cACA,OAAOG,KAAK,CAACvB,kBAAkB,CAAC;YAClC;UACF;UAEA,OAAOE,KAAK;QACd,CAAC,CAAC,MAAM;UACN,OAAOqB,KAAK,CAACvB,kBAAkB,CAAC;QAClC;MACF;IACF;EACF;AACF,CAAC,CAAiB","ignoreList":[]}
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
<div class="govuk-error-summary-container"></div>
|
|
11
11
|
|
|
12
|
-
<form method="post" {%- if formAction and not context.isForceAccess %} action="{{ formAction }}" enctype="multipart/form-data" data-upload-id="{{ uploadId }}" {% if proxyUrl %}data-proxy-url="{{ proxyUrl }}"{% endif %} {%- endif %} novalidate>
|
|
12
|
+
<form method="post" {%- if formAction and not context.isForceAccess %} action="{{ formAction }}" enctype="multipart/form-data" data-upload-id="{{ uploadId }}" {% if proxyUrl %}data-proxy-url="{{ proxyUrl }}"{% endif %} {%- else %} action="#" {%- endif %} novalidate>
|
|
13
13
|
{{ govukFileUpload(formComponent.model) }}
|
|
14
14
|
|
|
15
15
|
{% if formAction %}
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
{{ govukSummaryList(paymentDetails.summaryList) }}
|
|
58
58
|
{% endif %}
|
|
59
59
|
|
|
60
|
-
<form method="post" novalidate>
|
|
60
|
+
<form method="post" action="#" novalidate>
|
|
61
61
|
<input type="hidden" name="crumb" value="{{ crumb }}">
|
|
62
62
|
|
|
63
63
|
{% block customPageContent %}{% endblock %}
|
package/package.json
CHANGED
|
@@ -44,10 +44,20 @@ export class FormComponent extends ComponentBase {
|
|
|
44
44
|
|
|
45
45
|
this.type = type
|
|
46
46
|
this.hint = hint
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
|
|
48
|
+
this.label = this.getLabel(def)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getLabel(def: FormComponentsDef) {
|
|
52
|
+
if ('errorDescription' in def && def.errorDescription) {
|
|
53
|
+
return def.errorDescription
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if ('shortDescription' in def && def.shortDescription) {
|
|
57
|
+
return def.shortDescription
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return def.title
|
|
51
61
|
}
|
|
52
62
|
|
|
53
63
|
get keys() {
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ComponentType,
|
|
3
|
-
type FormComponentsDef,
|
|
4
|
-
type UkAddressFieldComponent
|
|
5
|
-
} from '@defra/forms-model'
|
|
1
|
+
import { ComponentType, type UkAddressFieldComponent } from '@defra/forms-model'
|
|
6
2
|
import { type ObjectSchema } from 'joi'
|
|
7
3
|
import lowerFirst from 'lodash/lowerFirst.js'
|
|
8
4
|
|
|
@@ -34,15 +30,13 @@ export class UkAddressField extends FormComponent {
|
|
|
34
30
|
declare stateSchema: ObjectSchema<FormState>
|
|
35
31
|
declare collection: ComponentCollection
|
|
36
32
|
|
|
37
|
-
shortDescription: FormComponentsDef['shortDescription']
|
|
38
|
-
|
|
39
33
|
constructor(
|
|
40
34
|
def: UkAddressFieldComponent,
|
|
41
35
|
props: ConstructorParameters<typeof FormComponent>[1]
|
|
42
36
|
) {
|
|
43
37
|
super(def, props)
|
|
44
38
|
|
|
45
|
-
const { name, options
|
|
39
|
+
const { name, options } = def
|
|
46
40
|
|
|
47
41
|
const isRequired = options.required !== false
|
|
48
42
|
const hideOptional = !!options.optionalText
|
|
@@ -126,7 +120,6 @@ export class UkAddressField extends FormComponent {
|
|
|
126
120
|
this.options = options
|
|
127
121
|
this.formSchema = this.collection.formSchema
|
|
128
122
|
this.stateSchema = this.collection.stateSchema
|
|
129
|
-
this.shortDescription = shortDescription
|
|
130
123
|
}
|
|
131
124
|
|
|
132
125
|
getFormValueFromState(state: FormSubmissionState) {
|
|
@@ -174,14 +167,14 @@ export class UkAddressField extends FormComponent {
|
|
|
174
167
|
// When using postcode lookup, the address fields are hidden
|
|
175
168
|
// so we replace any individual validation messages with a single one
|
|
176
169
|
if (this.shouldUsePostcodeLookup() && uniqueErrors?.length) {
|
|
177
|
-
const { name,
|
|
170
|
+
const { name, label } = this
|
|
178
171
|
|
|
179
172
|
return [
|
|
180
173
|
{
|
|
181
174
|
name,
|
|
182
175
|
path: [name],
|
|
183
176
|
href: `#${name}`,
|
|
184
|
-
text: `Enter ${lowerFirst(
|
|
177
|
+
text: `Enter ${lowerFirst(label)}`
|
|
185
178
|
}
|
|
186
179
|
]
|
|
187
180
|
}
|
|
@@ -39,7 +39,11 @@ export const joi = JoiBase.extend({
|
|
|
39
39
|
)
|
|
40
40
|
}
|
|
41
41
|
],
|
|
42
|
-
validate(
|
|
42
|
+
validate(
|
|
43
|
+
value: string,
|
|
44
|
+
{ error }: JoiBase.CustomHelpers,
|
|
45
|
+
args: { format?: TelephoneNumberFieldOptionsFormatEnum }
|
|
46
|
+
) {
|
|
43
47
|
const format = args.format
|
|
44
48
|
|
|
45
49
|
try {
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
<div class="govuk-error-summary-container"></div>
|
|
11
11
|
|
|
12
|
-
<form method="post" {%- if formAction and not context.isForceAccess %} action="{{ formAction }}" enctype="multipart/form-data" data-upload-id="{{ uploadId }}" {% if proxyUrl %}data-proxy-url="{{ proxyUrl }}"{% endif %} {%- endif %} novalidate>
|
|
12
|
+
<form method="post" {%- if formAction and not context.isForceAccess %} action="{{ formAction }}" enctype="multipart/form-data" data-upload-id="{{ uploadId }}" {% if proxyUrl %}data-proxy-url="{{ proxyUrl }}"{% endif %} {%- else %} action="#" {%- endif %} novalidate>
|
|
13
13
|
{{ govukFileUpload(formComponent.model) }}
|
|
14
14
|
|
|
15
15
|
{% if formAction %}
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
{{ govukSummaryList(paymentDetails.summaryList) }}
|
|
58
58
|
{% endif %}
|
|
59
59
|
|
|
60
|
-
<form method="post" novalidate>
|
|
60
|
+
<form method="post" action="#" novalidate>
|
|
61
61
|
<input type="hidden" name="crumb" value="{{ crumb }}">
|
|
62
62
|
|
|
63
63
|
{% block customPageContent %}{% endblock %}
|