import * as i0 from "@angular/core"; import { EventEmitter, OnDestroy, OnInit } from "@angular/core"; import { FormControl, FormGroup, FormRecord, ValidatorFn } from "@angular/forms"; import { Subject } from "rxjs"; import { IAbstractFormArrayControlValidation, IAbstractFormBaseFormErrorSummaryMessage, IAbstractFormControlErrorMessage } from "@hmcts/opal-frontend-common/components/abstract/interfaces"; import { UtilsService } from "@hmcts/opal-frontend-common/services/utils-service"; interface IAbstractFormBaseFieldError { [key: string]: { message: string; priority: number; }; } interface IAbstractFormBaseFieldErrors { [key: string]: IAbstractFormBaseFieldError; } interface IAbstractFormBaseFormError { fieldId: string; message: string | null; priority: number; type: string | null; } interface IAbstractFormBaseForm { formData: T; nestedFlow: boolean; } interface IAbstractFormBaseHandleRouteOptions { nonRelative?: boolean; event?: Event; routeData?: any; fragment?: string; } export declare abstract class AbstractFormBaseComponent implements OnInit, OnDestroy { private readonly changeDetectorRef; private readonly router; private readonly activatedRoute; protected unsavedChanges: EventEmitter; protected formSubmit: EventEmitter>; protected readonly utilsService: UtilsService; protected fieldErrors: IAbstractFormBaseFieldErrors; protected formSubmitted: boolean; protected readonly ngUnsubscribe: Subject; form: FormGroup; formControlErrorMessages: IAbstractFormControlErrorMessage; formErrorSummaryMessage: IAbstractFormBaseFormErrorSummaryMessage[]; formErrors: IAbstractFormBaseFormError[]; constructor(); /** * Scrolls to a specific form field and attempts to focus on it. * * This method locates the target field or its associated label/legend * based on the provided `fieldId`. It ensures that the field or its * nested focusable element is brought into view and focused, if possible. * * The scrolling behavior is smooth, and the method prioritizes the following * elements in order: * 1. A label associated with an autocomplete field (`label[for="${fieldId}-autocomplete"]`). * 2. A regular label associated with the field (`label[for="${fieldId}"]`). * 3. A legend within a fieldset associated with the field (`[id="${fieldId}"] .govuk-fieldset__legend`). * 4. The field itself or its nested focusable element. * * @param fieldId - The ID of the form field to scroll to and focus on. */ private scroll; /** * Determines whether a given HTML element can receive focus. * * An element is considered focusable if: * - It has a `tabIndex` greater than or equal to 0 and does not have the `disabled` attribute. * - It is an anchor (``) element with a valid `href` attribute. * - It is one of the following tags: `INPUT`, `SELECT`, `TEXTAREA`, or `BUTTON`, * and it is not disabled. * * @param element - The HTML element to check for focusability. * @returns `true` if the element can receive focus, otherwise `false`. */ private canReceiveFocus; /** * Returns the highest priority form error from the given error keys and field errors. * @param errorKeys - An array of error keys. * @param fieldErrors - An object containing field errors. * @returns The highest priority form error or null if no errors are found. */ private getHighestPriorityError; /** * Retrieves the details of the highest priority form error for a given control path. * @param controlPath - The path to the control in the form. * @returns The details of the highest priority form error, or null if there are no errors. */ private getFieldErrorDetails; /** * Retrieves all form errors from the provided form and its nested form groups. * * @param form - The form group to retrieve errors from. * @param controlPath - An optional array representing the path to the current control within the form group. * @returns An array of form errors, each containing the field ID, error message, priority, and type. */ private getFormErrors; /** * Sets the error messages for the form controls and error summary based on the provided form errors. * @param formErrors - An array of form errors containing field IDs and error messages. */ private setErrorMessages; /** * Gets the indexes of the date fields to remove based on the form error summary message. * @returns An array of indexes representing the date fields to remove. */ private getDateFieldsToRemoveIndexes; /** * Returns an array of indices corresponding to the positions of the given field IDs in the form error summary message array. * * @param fieldIds - An array of field IDs to search for in the form error summary message array. * @param formErrorSummaryMessage - An array of form error summary messages. * @returns An array of indices corresponding to the positions of the field IDs in the form error summary message array. */ private getFormErrorSummaryIndex; /** * Removes error summary messages from the given array based on the specified indexes. * * @param formErrorSummaryMessage - The array of error summary messages. * @param indexes - The indexes of the error summary messages to be removed. * @returns The updated array of error summary messages. */ private removeErrorSummaryMessages; /** * Splits the form errors into two arrays based on the provided field IDs. * Errors with field IDs included in the fieldIds array will be moved to the removedFormErrors array, * while the remaining errors will be moved to the cleanFormErrors array. * * @param fieldIds - An array of field IDs to filter the form errors. * @param formErrors - An array of form errors to be split. * @returns An array containing two arrays: cleanFormErrors and removedFormErrors. */ private splitFormErrors; /** * Manipulates the error message for specific fields in the formErrors array. * @param fields - An array of field IDs to target for error message manipulation. * @param messageOverride - The new error message to be used for the targeted fields. * @param errorType - The type of error to match for the targeted fields. * @param formErrors - An array of IFormError objects representing the form errors. * @returns An array of IFormError objects with the manipulated error messages. */ private manipulateFormErrorMessage; /** * Retrieves the form errors with the highest priority. * * @param formErrors - An array of form errors. * @returns An array of form errors with the highest priority. */ private getHighPriorityFormErrors; /** * Focuses on the error summary element and scrolls to the top of the page. * * This method first triggers change detection to ensure the view is up-to-date. * It then selects the error summary element with the class 'govuk-error-summary'. * If the error summary element is found, it sets focus on it without scrolling the page. * Finally, it calls a utility service to scroll to the top of the page. * * @private */ private focusAndScrollToErrorSummary; /** * Setup listener for the form value changes and to emit hasUnsavedChanges */ private setupListener; /** * Adds controls to a form group. * * @param formGroup - The form group to add controls to. * @param controls - An array of form array control validations. * @param index - The index of the form array control. */ protected addControlsToFormGroup(formGroup: FormGroup, controls: IAbstractFormArrayControlValidation[], index: number): void; /** * Creates a form control with the specified validators and initial value. * * @param validators - An array of validator functions. * @param initialValue - The initial value for the form control. Defaults to null. * @returns The created form control. */ protected createFormControl(validators: ValidatorFn[], initialValue?: string | null): FormControl; /** * Handles the error messages and populates the relevant variables */ protected handleErrorMessages(): void; /** * Checks whether the form has been touched and submitted * * @returns boolean */ protected hasUnsavedChanges(): boolean; /** * Sets the value of a form control specified by its path and marks it as touched. * * @param value - The value to set for the form control. * @param controlPath - The dot-delimited path to the form control within the form group. */ protected setInputValue(value: string, controlPath: string): void; /** * Handles the form errors for the date input fields. * @param formErrors - An array of form errors. * @returns An array of form errors with the manipulated error messages. */ protected handleDateInputFormErrors(): IAbstractFormBaseFormError[]; /** * Sets the initial error messages for the form controls. * * @param form - The FormGroup instance. */ protected setInitialErrorMessages(): void; /** * Repopulates the search form with the data from the account enquiry search. * @param state - The state object containing the search form data. */ protected rePopulateForm(state: any): void; /** * Adds a new form control to the form group. * * @param controlName - The name of the control to add. * @param validators - An array of validators to apply to the control. */ protected createControl(controlName: string, validators: ValidatorFn[]): void; /** * Updates the validators of an existing form control. * * @param controlName - The name of the control to update. * @param validators - An array of validators to apply to the control. */ protected updateControl(controlName: string, validators: ValidatorFn[]): void; /** * Removes a control from the form. * * @param controlName - The name of the control to remove. */ protected removeControl(controlName: string): void; /** * Removes the error message associated with the specified control name from the formControlErrorMessages object. * * @param controlName - The name of the control for which to remove the error message. */ protected removeControlErrors(controlName: string): void; /** * Clears all error messages from the form. */ protected clearAllErrorMessages(): void; /** * Create a plain object from a FormRecord of FormControls. * * What it does: copies each control's current value onto a plain object. * When to use: when you need the raw values from a FormRecord (e.g. to send to an API). * * Example * ------- * // record: FormRecord> with a single control 'firstName' * const out = this.objectFromFormRecord(record, { * mapKey: k => k, // optional (defaults to identity) * mapValue: v => v ?? '' // turn null/undefined into '' * }); * // out -> { firstName: '' } * * @typeParam T - Control value type. * @typeParam U - Output value type (defaults to T). * @typeParam K - Output key type (defaults to string). Control names are strings; provide `mapKey` if you need numbers. */ protected objectFromFormRecord(controlRecord: FormRecord>, options?: { mapKey?: (key: string) => K; mapValue?: (value: T | null | undefined) => U; }): Record; /** * Patch a FormRecord of FormControls from a plain object. * * What it does: for each entry in `values`, finds the matching control by name * (optionally mapped via `mapKey`) and sets its value **only if** it changes. * When to use: when you need to patch many controls at once, without * triggering extra updates for unchanged values. * * Example * ------- * // controlRecord: FormRecord> with controls 'firstName', 'lastName' * this.patchFormRecordFromObject(controlRecord, { firstName: 'Ada', lastName: 'Lovelace' }, { * emitEvent: true, * isEqual: (a, b) => a === b, * // mapKey: k => `ctrl_${k as string}` // optional: map incoming keys to control names * }); * * @typeParam T - Control value type. * @typeParam K - Keys present in the `values` object (string or number). */ protected patchFormRecordFromObject(controlRecord: FormRecord>, values: Record, options?: { emitEvent?: boolean; /** How provided keys map to control names (defaults to String(key)). */ mapKey?: (key: K) => string; /** Equality check used to avoid unnecessary writes (defaults to Object.is). */ isEqual?: (a: T, b: T) => boolean; }): void; /** * Clears the search form. */ handleClearForm(): void; /** * Handles the scroll of the component error from the summary * * @param fieldId - Field id of the component */ scrollTo(fieldId: string): void; /** * Handles route with the supplied route * * @param route string of route * @param options optional parameters */ handleRoute(route: string, options?: IAbstractFormBaseHandleRouteOptions): void; /** * Handles the form submission event. * * @param event - The form submission event. * @returns void */ handleFormSubmit(event: SubmitEvent): void; /** * Handles the input event to convert all letters in the input value to uppercase. * This method is triggered by an input event and modifies the input value directly. * * @param event - The input event triggered by the user. */ handleUppercaseInputMask(event: Event): void; ngOnInit(): void; ngOnDestroy(): void; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵcmp: i0.ɵɵComponentDeclaration; }