@uplink-protocol/form-controller
Version:
Reactive multi-step form controller with dynamic validation and state management
81 lines (80 loc) • 3.56 kB
TypeScript
import { BaseService } from './base.service';
import { ConfigService } from './config.service';
/**
* Service for managing form data and submission
*/
export declare class FormService extends BaseService<Record<string, Record<string, any>>> {
private configService;
private fieldErrorsService;
constructor(initialFormData: Record<string, Record<string, any>>, configService: ConfigService, fieldErrorsService: BaseService<Record<string, Record<string, string>>>, stepsValidityService: BaseService<Record<string, boolean>>);
/**
* Update a field value
* @param stepId Step ID
* @param fieldId Field ID
* @param value New field value
*/
updateField(stepId: string, fieldId: string, value: any): void;
/**
* Get form data for a specific step
* @param stepId Step ID
* @returns Step data or empty object if step doesn't exist
*/
getStepData(stepId: string): Record<string, any>;
/**
* Get all form data
* @returns Complete form data
*/
getAllData(): Record<string, Record<string, any>>;
/**
* Get flattened form data (merged from all steps)
* @returns Flattened form data
*/
getFlatData(): Record<string, any>;
/**
* Update a field value and validate it immediately
*
* @param stepId Step ID
* @param fieldId Field ID
* @param value New field value
* @param markFieldTouched Function to mark the field as touched
* @param validateField Function to validate the field
* @param validateStepWithTouched Function to validate the step with touched fields only
*/
updateFieldWithValidation(stepId: string, fieldId: string, value: any, markFieldTouched: (stepId: string, fieldId: string, touched: boolean) => void, validateField: (stepId: string, fieldId: string, value: any, showErrors: boolean) => boolean, validateStepWithTouched: (stepId: string) => boolean): void;
/**
* Reset form data to initial values and optionally reset related state
*
* @param defaultValues Optional custom default values
* @param resetTouchTracking Function to reset touch tracking state
* @param validateForm Function to validate the form
*/
resetFormWithState(defaultValues?: Record<string, any>, resetTouchTracking?: () => void, validateForm?: (showErrors: boolean) => boolean): void;
/**
* Handle form submission logic with integrated validation
* @param validateAndMarkTouched Function to validate the entire form and mark fields touched
* @param findFirstInvalidStep Function to find the first invalid step
* @param goToStep Function to navigate to a step by index
* @returns Submission result object
*/
submitForm(validateAndMarkTouched: () => boolean, findFirstInvalidStep: () => number, goToStep: (index: number) => void): {
success: boolean;
data?: any;
errors?: any;
};
/**
* Completely reset the form, touch tracking, and validation state
* @param resetTouchTracking Function to reset touch tracking
* @param validateForm Function to validate the entire form
*/
completeReset(resetTouchTracking: () => void, validateForm: () => void): void;
/**
* Get the ConfigService associated with this FormService
* @returns The ConfigService instance
*/
getConfigService(): ConfigService;
/**
* Reset form data to initial values
* @param defaultValues Optional custom default values
*/
resetForm(defaultValues?: Record<string, any>): void;
}