UNPKG

signal-template-forms

Version:

A powerful, type-safe Angular forms library built with signals, providing reactive form management with excellent developer experience and performance.

140 lines (139 loc) 5.75 kB
import { type SignalFormContainer, type SignalFormField, type SignalFormFieldBuilderInput } from '../../models/signal-form.model'; /** * FieldFactory - Factory class for building Signal Form fields * * Responsible for converting field builder configurations into fully * initialized SignalFormField instances with reactive state management. * Handles different field types including nested forms, repeatable groups, * and fields with static or computed options. * * Features: * - Type-safe field construction with TypeScript generics * - Automatic reactive state binding (value, error, validation, etc.) * - Support for nested form groups and repeatable field arrays * - Dynamic and computed options for select-type fields * - Path tracking for nested field references * * @example * ```typescript * const textField = FieldFactory.build( * { name: 'email', type: FormFieldType.TEXT, label: 'Email' }, * { email: '' }, * formContainer, * 'user' * ); * ``` */ export declare class FieldFactory { /** * Builds a complete SignalFormField from a field configuration * * Converts field builder input into a reactive field with full state management. * Automatically detects field type (normal, nested group, or repeatable group) * and creates appropriate field structure with computed properties and signals. * * @template TModel - The TypeScript type of the form data model * @param field - Field configuration from form builder * @param model - Current form data model containing initial values * @param formRef - Reference to the parent form container * @param parentPath - Path prefix for nested fields (e.g., "user.address") * @returns Fully initialized SignalFormField with reactive state * * @example * ```typescript * // Build a simple text field * const nameField = FieldFactory.build( * { name: 'firstName', type: FormFieldType.TEXT, label: 'First Name' }, * { firstName: 'John' }, * formContainer * ); * * // Build a select field with options * const countryField = FieldFactory.build( * { * name: 'country', * type: FormFieldType.SELECT, * label: 'Country', * options: [{ value: 'US', label: 'United States' }] * }, * { country: 'US' }, * formContainer * ); * ``` */ static build<TModel>(field: SignalFormFieldBuilderInput<TModel>, model: TModel, formRef: SignalFormContainer<TModel>, parentPath?: string): SignalFormField<TModel>; /** * Type guard to check if a field is a repeatable group field * Repeatable groups contain arrays of form items that can be added/removed * * @template TModel - The form model type * @param field - Field configuration to check * @returns True if field is a repeatable group, false otherwise * @private */ private static isRepeatableGroupField; /** * Type guard to check if a field is a nested group field * Nested groups contain sub-forms with their own field collections * * @template TModel - The form model type * @param field - Field configuration to check * @returns True if field is a nested group, false otherwise * @private */ private static isNestedGroupField; /** * Type guard to check if a field supports options (select, radio, etc.) * Uses Extract to get only field types that have options property * * @template TModel - The form model type * @param field - Field configuration to check * @returns True if field supports options, false otherwise * @private */ private static hasOptions; /** * Checks if a field has computed/dynamic options configuration * Computed options change based on form state or other reactive values * * @template TModel - The form model type * @param field - Field configuration to check * @returns True if field has computed options, false otherwise * @private */ private static hasComputedOptions; /** * Builds a repeatable group field that manages an array of sub-forms * * Creates a field that contains multiple instances of the same form structure, * allowing users to add and remove items dynamically. Each item is a complete * form with its own validation and state management. * * @template TModel - The form model type * @param field - Repeatable group field configuration * @param rawValue - Current array value from the model * @param parentForm - Reference to the parent form container * @param referencePath - Path to this field for nested references * @param baseFieldState - Base reactive state for the field * @returns Configured repeatable group field with add/remove functionality * @private */ private static buildRepeatableGroup; /** * Builds a nested form group field that contains a sub-form * * Creates a field that embeds another complete form within the current form. * The nested form has its own fields, validation, and state management while * being part of the parent form's structure. * * @template TModel - The form model type * @param field - Nested group field configuration * @param rawValue - Current nested object value from the model * @param parentForm - Reference to the parent form container * @param referencePath - Path to this field for nested references * @param baseFieldState - Base reactive state for the field * @returns Configured nested form field with embedded sub-form * @private */ private static buildNestedForm; }