All files / src/types forms.ts

0% Statements 0/464
100% Branches 0/0
0% Functions 0/1
0% Lines 0/464

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
/**
 * Form Domain Types
 *
 * Central location for all form-related types including field props,
 * validation, and form state management.
 *
 * @example
 * ```tsx
 * import { FormFieldProps, ValidationRule } from '@/types/forms';
 *
 * const field: FormFieldProps = {
 *   name: 'email',
 *   label: 'Email Address',
 *   type: 'email',
 *   required: true,
 *   // ...
 * };
 * ```
 */

// =============================================================================
// FORM FIELD TYPES
// =============================================================================

/**
 * Input field types
 */
export type InputFieldType =
  | 'text'
  | 'email'
  | 'password'
  | 'number'
  | 'tel'
  | 'url'
  | 'search'
  | 'date'
  | 'time'
  | 'datetime-local'
  | 'month'
  | 'week'
  | 'color'
  | 'file'
  | 'hidden'
  | 'range'
  | 'checkbox'
  | 'radio';

/**
 * Field validation state
 */
export type FieldState = 'default' | 'success' | 'error' | 'warning';

/**
 * Form field base props
 */
export interface FormFieldBaseProps {
  /** Field name (for form data) */
  name: string;
  /** Field label */
  label?: string;
  /** Placeholder text */
  placeholder?: string;
  /** Helper text below field */
  helperText?: string;
  /** Error message */
  error?: string;
  /** Field is required */
  required?: boolean;
  /** Field is disabled */
  disabled?: boolean;
  /** Field is readonly */
  readOnly?: boolean;
  /** Field validation state */
  state?: FieldState;
  /** Autocomplete attribute */
  autoComplete?: string;
  /** Field class name */
  className?: string;
}

/**
 * Text input field props
 */
export interface TextInputFieldProps extends FormFieldBaseProps {
  type: Extract<InputFieldType, 'text' | 'email' | 'password' | 'tel' | 'url' | 'search'>;
  value: string;
  onChange: (value: string) => void;
  onBlur?: () => void;
  maxLength?: number;
  minLength?: number;
  pattern?: string;
  leftIcon?: React.ReactNode;
  rightIcon?: React.ReactNode;
}

/**
 * Number input field props
 */
export interface NumberInputFieldProps extends FormFieldBaseProps {
  type: 'number';
  value: number | '';
  onChange: (value: number | '') => void;
  onBlur?: () => void;
  min?: number;
  max?: number;
  step?: number;
}

/**
 * Textarea field props
 */
export interface TextareaFieldProps extends FormFieldBaseProps {
  value: string;
  onChange: (value: string) => void;
  onBlur?: () => void;
  rows?: number;
  cols?: number;
  maxLength?: number;
  minLength?: number;
}

/**
 * Select field option
 */
export interface SelectOption<T = string> {
  value: T;
  label: string;
  disabled?: boolean;
  group?: string;
}

/**
 * Select field props
 */
export interface SelectFieldProps<T = string> extends FormFieldBaseProps {
  value: T;
  onChange: (value: T) => void;
  onBlur?: () => void;
  options: SelectOption<T>[];
  multiple?: boolean;
}

/**
 * Checkbox field props
 */
export interface CheckboxFieldProps extends FormFieldBaseProps {
  checked: boolean;
  onChange: (checked: boolean) => void;
  onBlur?: () => void;
  indeterminate?: boolean;
}

/**
 * Radio field option
 */
export interface RadioOption<T = string> {
  value: T;
  label: string;
  description?: string;
  disabled?: boolean;
}

/**
 * Radio group field props
 */
export interface RadioGroupFieldProps<T = string> extends FormFieldBaseProps {
  value: T;
  onChange: (value: T) => void;
  onBlur?: () => void;
  options: RadioOption<T>[];
  orientation?: 'horizontal' | 'vertical';
}

/**
 * File input field props
 */
export interface FileInputFieldProps extends FormFieldBaseProps {
  accept?: string;
  multiple?: boolean;
  maxSize?: number; // in bytes
  maxFiles?: number;
  onChange: (files: File[]) => void;
  onBlur?: () => void;
  preview?: boolean;
}

/**
 * Date input field props
 */
export interface DateInputFieldProps extends FormFieldBaseProps {
  type: Extract<InputFieldType, 'date' | 'time' | 'datetime-local' | 'month' | 'week'>;
  value: string;
  onChange: (value: string) => void;
  onBlur?: () => void;
  min?: string;
  max?: string;
}

// =============================================================================
// VALIDATION TYPES
// =============================================================================

/**
 * Validation rule type
 */
export type ValidationRuleType =
  | 'required'
  | 'email'
  | 'url'
  | 'min'
  | 'max'
  | 'minLength'
  | 'maxLength'
  | 'pattern'
  | 'custom';

/**
 * Validation rule
 */
export interface ValidationRule {
  type: ValidationRuleType;
  value?: unknown;
  message: string;
}

/**
 * Field validation result
 */
export interface FieldValidationResult {
  isValid: boolean;
  errors: string[];
}

/**
 * Form validation result
 */
export interface FormValidationResult<T = Record<string, unknown>> {
  isValid: boolean;
  errors: Partial<Record<keyof T, string[]>>;
}

/**
 * Validator function
 */
export type ValidatorFunction<T = unknown> = (value: T) => boolean | string | Promise<boolean | string>;

// =============================================================================
// FORM STATE TYPES
// =============================================================================

/**
 * Form field state
 */
export interface FieldStateData {
  value: unknown;
  error?: string;
  touched: boolean;
  dirty: boolean;
  validating: boolean;
}

/**
 * Form state
 */
export interface FormState<T = Record<string, unknown>> {
  values: T;
  errors: Partial<Record<keyof T, string>>;
  touched: Partial<Record<keyof T, boolean>>;
  dirty: Partial<Record<keyof T, boolean>>;
  isSubmitting: boolean;
  isValidating: boolean;
  isValid: boolean;
  submitCount: number;
}

/**
 * Form submit handler
 */
export type FormSubmitHandler<T = Record<string, unknown>> = (
  values: T,
  formState: FormState<T>
) => void | Promise<void>;

/**
 * Form change handler
 */
export type FormChangeHandler<T = Record<string, unknown>> = (
  name: keyof T,
  value: unknown
) => void;

/**
 * Form blur handler
 */
export type FormBlurHandler<T = Record<string, unknown>> = (
  name: keyof T
) => void;

/**
 * Form reset handler
 */
export type FormResetHandler<T = Record<string, unknown>> = (
  values?: Partial<T>
) => void;

// =============================================================================
// FORM CONFIGURATION
// =============================================================================

/**
 * Form field configuration
 */
export interface FormFieldConfig {
  name: string;
  label?: string;
  type: string;
  required?: boolean;
  disabled?: boolean;
  placeholder?: string;
  helperText?: string;
  defaultValue?: unknown;
  validation?: ValidationRule[];
}

/**
 * Form configuration
 */
export interface FormConfig<T = Record<string, unknown>> {
  fields: FormFieldConfig[];
  initialValues?: Partial<T>;
  validateOnChange?: boolean;
  validateOnBlur?: boolean;
  validateOnSubmit?: boolean;
  onSubmit: FormSubmitHandler<T>;
  onReset?: FormResetHandler<T>;
}

// =============================================================================
// FORM HOOK RETURN TYPES
// =============================================================================

/**
 * useForm hook return type
 */
export interface UseFormReturn<T = Record<string, unknown>> {
  // State
  values: T;
  errors: Partial<Record<keyof T, string>>;
  touched: Partial<Record<keyof T, boolean>>;
  dirty: Partial<Record<keyof T, boolean>>;
  isSubmitting: boolean;
  isValidating: boolean;
  isValid: boolean;

  // Handlers
  handleChange: FormChangeHandler<T>;
  handleBlur: FormBlurHandler<T>;
  handleSubmit: (e?: React.FormEvent) => Promise<void>;
  handleReset: FormResetHandler<T>;

  // Field helpers
  setFieldValue: (name: keyof T, value: unknown) => void;
  setFieldError: (name: keyof T, error: string) => void;
  setFieldTouched: (name: keyof T, touched: boolean) => void;

  // Form helpers
  setValues: (values: Partial<T>) => void;
  setErrors: (errors: Partial<Record<keyof T, string>>) => void;
  resetForm: (values?: Partial<T>) => void;
  validateForm: () => Promise<FormValidationResult<T>>;
  validateField: (name: keyof T) => Promise<FieldValidationResult>;

  // Field registration
  register: (name: keyof T) => FormFieldBaseProps;
  getFieldProps: (name: keyof T) => FormFieldBaseProps & {
    value: unknown;
    onChange: (value: unknown) => void;
    onBlur: () => void;
  };
}

// =============================================================================
// FORM BUILDER TYPES
// =============================================================================

/**
 * Form layout
 */
export type FormLayout = 'vertical' | 'horizontal' | 'inline';

/**
 * Form builder props
 */
export interface FormBuilderProps {
  config: FormConfig;
  layout?: FormLayout;
  className?: string;
  submitButtonText?: string;
  resetButtonText?: string;
  showResetButton?: boolean;
}

// =============================================================================
// MULTI-STEP FORM TYPES
// =============================================================================

/**
 * Form step configuration
 */
export interface FormStepConfig {
  id: string;
  title: string;
  description?: string;
  fields: FormFieldConfig[];
  validation?: ValidationRule[];
  canSkip?: boolean;
}

/**
 * Multi-step form state
 */
export interface MultiStepFormState<T = Record<string, unknown>> extends FormState<T> {
  currentStep: number;
  totalSteps: number;
  completedSteps: number[];
  canGoNext: boolean;
  canGoPrevious: boolean;
}

/**
 * useMultiStepForm hook return type
 */
export interface UseMultiStepFormReturn<T = Record<string, unknown>> extends UseFormReturn<T> {
  currentStep: number;
  totalSteps: number;
  goToStep: (step: number) => void;
  nextStep: () => void;
  previousStep: () => void;
  isFirstStep: boolean;
  isLastStep: boolean;
  currentStepConfig: FormStepConfig;
}

// =============================================================================
// FORM ERROR TYPES
// =============================================================================

/**
 * Form error
 */
export interface FormError {
  field: string;
  message: string;
  type?: ValidationRuleType;
}

/**
 * Form submission error
 */
export interface FormSubmissionError {
  message: string;
  fieldErrors?: FormError[];
  generalError?: string;
}