From 5711daf91f333cc49c120e742686708a50caf7f8 Mon Sep 17 00:00:00 2001 From: whitewater design Date: Thu, 28 Aug 2025 18:50:40 +0100 Subject: [PATCH 1/2] refactor: DF-380 - Add component dot getDisplayStringFromFormValue & getContextValueFormFormValue --- .../engine/components/CheckboxesField.ts | 25 +++++++++++++------ .../engine/components/DatePartsField.ts | 23 ++++++++++++----- .../engine/components/FileUploadField.ts | 17 ++++++++++--- .../engine/components/FormComponent.ts | 22 ++++++++++++---- .../engine/components/ListFormComponent.ts | 13 +++++++--- .../engine/components/MonthYearField.ts | 18 ++++++++++--- .../engine/components/UkAddressField.ts | 22 +++++++++++----- 7 files changed, 106 insertions(+), 34 deletions(-) diff --git a/src/server/plugins/engine/components/CheckboxesField.ts b/src/server/plugins/engine/components/CheckboxesField.ts index 30707c3d1..68950eaab 100644 --- a/src/server/plugins/engine/components/CheckboxesField.ts +++ b/src/server/plugins/engine/components/CheckboxesField.ts @@ -65,12 +65,9 @@ export class CheckboxesField extends SelectionControlField { return this.isValue(value) ? value : undefined } - getDisplayStringFromState(state: FormSubmissionState) { + getDisplayStringFromFormValue(selected: (string | number | boolean)[]) { const { items } = this - // Selected checkbox values - const selected = this.getFormValueFromState(state) ?? [] - // Map selected values to text return items .filter((item) => selected.includes(item.value)) @@ -78,9 +75,9 @@ export class CheckboxesField extends SelectionControlField { .join(', ') } - getContextValueFromState(state: FormSubmissionState) { - const values = this.getFormValueFromState(state) - + getContextValueFormFormValue( + values: (string | number | boolean)[] | undefined + ): (string | number | boolean)[] { /** * For evaluation context purposes, optional {@link CheckboxesField} * with an undefined value (i.e. nothing selected) should default to []. @@ -95,6 +92,20 @@ export class CheckboxesField extends SelectionControlField { return values ?? [] } + getDisplayStringFromState(state: FormSubmissionState) { + // Selected checkbox values + const selected = this.getFormValueFromState(state) ?? [] + + // Map selected values to text + return this.getDisplayStringFromFormValue(selected) + } + + getContextValueFromState(state: FormSubmissionState) { + const values = this.getFormValueFromState(state) + + return this.getContextValueFormFormValue(values) + } + isValue(value?: FormStateValue | FormState): value is Item['value'][] { if (!Array.isArray(value)) { return false diff --git a/src/server/plugins/engine/components/DatePartsField.ts b/src/server/plugins/engine/components/DatePartsField.ts index ca356f88a..4446fdb6f 100644 --- a/src/server/plugins/engine/components/DatePartsField.ts +++ b/src/server/plugins/engine/components/DatePartsField.ts @@ -109,19 +109,24 @@ export class DatePartsField extends FormComponent { return this.isState(value) ? value : undefined } - getDisplayStringFromState(state: FormSubmissionState) { - const value = this.getFormValueFromState(state) - - if (!value) { + getDisplayStringFromFormValue(formValue: DatePartsState | undefined) { + if (!formValue) { return '' } - return format(`${value.year}-${value.month}-${value.day}`, 'd MMMM yyyy') + return format( + `${formValue.year}-${formValue.month}-${formValue.day}`, + 'd MMMM yyyy' + ) } - getContextValueFromState(state: FormSubmissionState) { + getDisplayStringFromState(state: FormSubmissionState) { const value = this.getFormValueFromState(state) + return this.getDisplayStringFromFormValue(value) + } + + getContextValueFormFormValue(value: DatePartsState | undefined) { if ( !value || !isValid( @@ -139,6 +144,12 @@ export class DatePartsField extends FormComponent { return format(`${value.year}-${value.month}-${value.day}`, 'yyyy-MM-dd') } + getContextValueFromState(state: FormSubmissionState) { + const value = this.getFormValueFromState(state) + + return this.getContextValueFormFormValue(value) + } + getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) { const { collection, name } = this diff --git a/src/server/plugins/engine/components/FileUploadField.ts b/src/server/plugins/engine/components/FileUploadField.ts index dcbbb0833..34aa2aa08 100644 --- a/src/server/plugins/engine/components/FileUploadField.ts +++ b/src/server/plugins/engine/components/FileUploadField.ts @@ -149,8 +149,7 @@ export class FileUploadField extends FormComponent { return this.isValue(value) ? value : undefined } - getDisplayStringFromState(state: FormSubmissionState) { - const files = this.getFormValueFromState(state) + getDisplayStringFromFormValue(files: FileState[] | undefined): string { if (!files?.length) { return '' } @@ -159,11 +158,23 @@ export class FileUploadField extends FormComponent { return `Uploaded ${files.length} ${unit}` } - getContextValueFromState(state: FormSubmissionState) { + getDisplayStringFromState(state: FormSubmissionState) { const files = this.getFormValueFromState(state) + + return this.getDisplayStringFromFormValue(files) + } + + getContextValueFormFormValue( + files: UploadState | undefined + ): string[] | null { return files?.map(({ status }) => status.form.file.fileId) ?? null } + getContextValueFromState(state: FormSubmissionState) { + const files = this.getFormValueFromState(state) + return this.getContextValueFormFormValue(files) + } + getViewModel( payload: FormPayload, errors?: FormSubmissionError[], diff --git a/src/server/plugins/engine/components/FormComponent.ts b/src/server/plugins/engine/components/FormComponent.ts index c71698d6d..3c2bcd88f 100644 --- a/src/server/plugins/engine/components/FormComponent.ts +++ b/src/server/plugins/engine/components/FormComponent.ts @@ -157,17 +157,21 @@ export class FormComponent extends ComponentBase { } } - getDisplayStringFromState(state: FormSubmissionState): string { - const value = this.getFormValueFromState(state) + getDisplayStringFromFormValue(value: FormValue | FormPayload): string { + // Map selected values to text // eslint-disable-next-line @typescript-eslint/no-base-to-string return this.isValue(value) ? value.toString() : '' } - getContextValueFromState( - state: FormSubmissionState - ): Item['value'] | Item['value'][] | null { + getDisplayStringFromState(state: FormSubmissionState): string { const value = this.getFormValueFromState(state) + // eslint-disable-next-line @typescript-eslint/no-base-to-string + return this.getDisplayStringFromFormValue(value) + } + getContextValueFormFormValue( + value: FormValue | FormPayload + ): Item['value'] | Item['value'][] | null { // Filter object field values if (this.isState(value)) { const values = Object.values(value).filter(isFormValue) @@ -182,6 +186,14 @@ export class FormComponent extends ComponentBase { return this.isValue(value) ? value : null } + getContextValueFromState( + state: FormSubmissionState + ): Item['value'] | Item['value'][] | null { + const value = this.getFormValueFromState(state) + + return this.getContextValueFormFormValue(value) + } + isValue( value?: FormStateValue | FormState ): value is NonNullable { diff --git a/src/server/plugins/engine/components/ListFormComponent.ts b/src/server/plugins/engine/components/ListFormComponent.ts index d4ec34374..3d8b91db1 100644 --- a/src/server/plugins/engine/components/ListFormComponent.ts +++ b/src/server/plugins/engine/components/ListFormComponent.ts @@ -99,11 +99,11 @@ export class ListFormComponent extends FormComponent { return selected.at(0)?.value } - getDisplayStringFromState(state: FormSubmissionState) { + getDisplayStringFromFormValue( + value: string | number | boolean | Item['value'][] | undefined + ): string { const { items } = this - // Allow for array values via subclass - const value = this.getFormValueFromState(state) const values = [value ?? []].flat() return items @@ -112,6 +112,13 @@ export class ListFormComponent extends FormComponent { .join(', ') } + getDisplayStringFromState(state: FormSubmissionState) { + // Allow for array values via subclass + const value = this.getFormValueFromState(state) + + return this.getDisplayStringFromFormValue(value) + } + getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) { const { items: listItems } = this diff --git a/src/server/plugins/engine/components/MonthYearField.ts b/src/server/plugins/engine/components/MonthYearField.ts index 0fb311757..1ae45f47a 100644 --- a/src/server/plugins/engine/components/MonthYearField.ts +++ b/src/server/plugins/engine/components/MonthYearField.ts @@ -103,9 +103,7 @@ export class MonthYearField extends FormComponent { return MonthYearField.isMonthYear(value) ? value : undefined } - getDisplayStringFromState(state: FormSubmissionState) { - const value = this.getFormValueFromState(state) - + getDisplayStringFromFormValue(value: MonthYearState | undefined): string { if (!value) { return '' } @@ -117,9 +115,15 @@ export class MonthYearField extends FormComponent { return `${monthString} ${value.year}` } - getContextValueFromState(state: FormSubmissionState) { + getDisplayStringFromState(state: FormSubmissionState) { const value = this.getFormValueFromState(state) + return this.getDisplayStringFromFormValue(value) + } + + getContextValueFormFormValue( + value: MonthYearState | undefined + ): string | null { if ( !value || !isValid( @@ -137,6 +141,12 @@ export class MonthYearField extends FormComponent { return format(`${value.year}-${value.month}-01`, 'yyyy-MM') } + getContextValueFromState(state: FormSubmissionState) { + const value = this.getFormValueFromState(state) + + return this.getContextValueFormFormValue(value) + } + getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) { const { collection, name } = this diff --git a/src/server/plugins/engine/components/UkAddressField.ts b/src/server/plugins/engine/components/UkAddressField.ts index 4f7b12363..738a075d8 100644 --- a/src/server/plugins/engine/components/UkAddressField.ts +++ b/src/server/plugins/engine/components/UkAddressField.ts @@ -110,18 +110,28 @@ export class UkAddressField extends FormComponent { return this.isState(value) ? value : undefined } - getDisplayStringFromState(state: FormSubmissionState) { - return this.getContextValueFromState(state)?.join(', ') ?? '' + getContextValueFormFormValue(value: UkAddressState | undefined) { + if (!value) { + return null + } + + return Object.values(value).filter(Boolean) } getContextValueFromState(state: FormSubmissionState) { const value = this.getFormValueFromState(state) - if (!value) { - return null - } + return this.getContextValueFormFormValue(value) + } - return Object.values(value).filter(Boolean) + getDisplayStringFromFormValue(value: UkAddressState | undefined): string { + return this.getContextValueFormFormValue(value)?.join(', ') ?? '' + } + + getDisplayStringFromState(state: FormSubmissionState) { + const value = this.getFormValueFromState(state) + + return this.getDisplayStringFromFormValue(value) } /** From c510ab71e64e513bfdee1b5d9e9381249469b65f Mon Sep 17 00:00:00 2001 From: whitewater design Date: Thu, 28 Aug 2025 19:21:28 +0100 Subject: [PATCH 2/2] refactor: DF-380 - Typo getContextValueFromFormValue --- src/server/plugins/engine/components/CheckboxesField.ts | 4 ++-- src/server/plugins/engine/components/DatePartsField.ts | 4 ++-- src/server/plugins/engine/components/FileUploadField.ts | 4 ++-- src/server/plugins/engine/components/FormComponent.ts | 4 ++-- src/server/plugins/engine/components/MonthYearField.ts | 4 ++-- src/server/plugins/engine/components/UkAddressField.ts | 6 +++--- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/server/plugins/engine/components/CheckboxesField.ts b/src/server/plugins/engine/components/CheckboxesField.ts index 68950eaab..c3c7579c6 100644 --- a/src/server/plugins/engine/components/CheckboxesField.ts +++ b/src/server/plugins/engine/components/CheckboxesField.ts @@ -75,7 +75,7 @@ export class CheckboxesField extends SelectionControlField { .join(', ') } - getContextValueFormFormValue( + getContextValueFromFormValue( values: (string | number | boolean)[] | undefined ): (string | number | boolean)[] { /** @@ -103,7 +103,7 @@ export class CheckboxesField extends SelectionControlField { getContextValueFromState(state: FormSubmissionState) { const values = this.getFormValueFromState(state) - return this.getContextValueFormFormValue(values) + return this.getContextValueFromFormValue(values) } isValue(value?: FormStateValue | FormState): value is Item['value'][] { diff --git a/src/server/plugins/engine/components/DatePartsField.ts b/src/server/plugins/engine/components/DatePartsField.ts index 4446fdb6f..98fbded70 100644 --- a/src/server/plugins/engine/components/DatePartsField.ts +++ b/src/server/plugins/engine/components/DatePartsField.ts @@ -126,7 +126,7 @@ export class DatePartsField extends FormComponent { return this.getDisplayStringFromFormValue(value) } - getContextValueFormFormValue(value: DatePartsState | undefined) { + getContextValueFromFormValue(value: DatePartsState | undefined) { if ( !value || !isValid( @@ -147,7 +147,7 @@ export class DatePartsField extends FormComponent { getContextValueFromState(state: FormSubmissionState) { const value = this.getFormValueFromState(state) - return this.getContextValueFormFormValue(value) + return this.getContextValueFromFormValue(value) } getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) { diff --git a/src/server/plugins/engine/components/FileUploadField.ts b/src/server/plugins/engine/components/FileUploadField.ts index 34aa2aa08..ec2f1be33 100644 --- a/src/server/plugins/engine/components/FileUploadField.ts +++ b/src/server/plugins/engine/components/FileUploadField.ts @@ -164,7 +164,7 @@ export class FileUploadField extends FormComponent { return this.getDisplayStringFromFormValue(files) } - getContextValueFormFormValue( + getContextValueFromFormValue( files: UploadState | undefined ): string[] | null { return files?.map(({ status }) => status.form.file.fileId) ?? null @@ -172,7 +172,7 @@ export class FileUploadField extends FormComponent { getContextValueFromState(state: FormSubmissionState) { const files = this.getFormValueFromState(state) - return this.getContextValueFormFormValue(files) + return this.getContextValueFromFormValue(files) } getViewModel( diff --git a/src/server/plugins/engine/components/FormComponent.ts b/src/server/plugins/engine/components/FormComponent.ts index 3c2bcd88f..ff04cccab 100644 --- a/src/server/plugins/engine/components/FormComponent.ts +++ b/src/server/plugins/engine/components/FormComponent.ts @@ -169,7 +169,7 @@ export class FormComponent extends ComponentBase { return this.getDisplayStringFromFormValue(value) } - getContextValueFormFormValue( + getContextValueFromFormValue( value: FormValue | FormPayload ): Item['value'] | Item['value'][] | null { // Filter object field values @@ -191,7 +191,7 @@ export class FormComponent extends ComponentBase { ): Item['value'] | Item['value'][] | null { const value = this.getFormValueFromState(state) - return this.getContextValueFormFormValue(value) + return this.getContextValueFromFormValue(value) } isValue( diff --git a/src/server/plugins/engine/components/MonthYearField.ts b/src/server/plugins/engine/components/MonthYearField.ts index 1ae45f47a..d5828fab3 100644 --- a/src/server/plugins/engine/components/MonthYearField.ts +++ b/src/server/plugins/engine/components/MonthYearField.ts @@ -121,7 +121,7 @@ export class MonthYearField extends FormComponent { return this.getDisplayStringFromFormValue(value) } - getContextValueFormFormValue( + getContextValueFromFormValue( value: MonthYearState | undefined ): string | null { if ( @@ -144,7 +144,7 @@ export class MonthYearField extends FormComponent { getContextValueFromState(state: FormSubmissionState) { const value = this.getFormValueFromState(state) - return this.getContextValueFormFormValue(value) + return this.getContextValueFromFormValue(value) } getViewModel(payload: FormPayload, errors?: FormSubmissionError[]) { diff --git a/src/server/plugins/engine/components/UkAddressField.ts b/src/server/plugins/engine/components/UkAddressField.ts index 738a075d8..aa73936f8 100644 --- a/src/server/plugins/engine/components/UkAddressField.ts +++ b/src/server/plugins/engine/components/UkAddressField.ts @@ -110,7 +110,7 @@ export class UkAddressField extends FormComponent { return this.isState(value) ? value : undefined } - getContextValueFormFormValue(value: UkAddressState | undefined) { + getContextValueFromFormValue(value: UkAddressState | undefined) { if (!value) { return null } @@ -121,11 +121,11 @@ export class UkAddressField extends FormComponent { getContextValueFromState(state: FormSubmissionState) { const value = this.getFormValueFromState(state) - return this.getContextValueFormFormValue(value) + return this.getContextValueFromFormValue(value) } getDisplayStringFromFormValue(value: UkAddressState | undefined): string { - return this.getContextValueFormFormValue(value)?.join(', ') ?? '' + return this.getContextValueFromFormValue(value)?.join(', ') ?? '' } getDisplayStringFromState(state: FormSubmissionState) {