import { AbstractType, InjectionToken, StaticProvider, Type } from '@angular/core'; import { AbstractControlOptions, AsyncValidatorFn, ValidatorFn } from '@angular/forms'; import { BaseDefinitionMetadata } from '@rxap/definition'; import { Constructor, NonEmpty } from '@rxap/utilities'; import { RxapFormArray } from './form-array'; import { RxapFormControl } from './form-control'; import type { RxapFormGroup } from './form-group'; import { AbstractControl, ControlOptions } from './types'; export interface InjectableValidator { validate?: ValidatorFn; asyncValidate?: AsyncValidatorFn; } /** * @internal */ export interface _RxapAbstractControlOptions extends AbstractControlOptions { state?: any | (() => any); injectValidators?: Array | InjectionToken | AbstractType>; controlType?: Constructor; disabled?: boolean; readonly?: boolean; } export type RxapAbstractControlOptions = Record> = _RxapAbstractControlOptions & T; export interface RxapAbstractControlOptionsWithDefinition extends RxapAbstractControlOptions { definition: Constructor; } export interface FormDefinition = any> { rxapFormGroup: RxapFormGroup; /** * used to access the form definition metadata type save */ rxapMetadata: FormDefinitionMetadata; /** * The Reuse hook is called when the instance is reused. * And can be used to reset or alter the local state of the instance. */ rxapReuse?(): void; /** * Called to get the value that should be submitted. If not defined * the value property of the root RxapFormGroup instance will be used */ getSubmitValue?(): JSON; /** * Called to get the value that should be submitted. If not defined * the value property of the root RxapFormGroup instance will be used */ toJSON?(): JSON; } export interface FormDefinitionArray extends Array { rxapFormArray: RxapFormArray; } export type FormType = Partial> & { [K in keyof T]: T[K] extends (infer U)[] ? FormDefinitionArray> | FormDefinitionArray> | RxapFormControl | RxapFormArray : T[K] extends Record ? (FormType & Partial>) | RxapFormControl : RxapFormControl>; }; export interface FormOptions extends RxapAbstractControlOptions { id: string; } export interface FormDefinitionMetadata extends BaseDefinitionMetadata, FormOptions { providers?: StaticProvider[]; /** * true - after 5000ms the form is automatically submitted if valid * number - after the set ms the is automatically submitted if valid */ autoSubmit?: boolean | number; } export type ChangeFn = (value: T, emitViewToModelChange: boolean) => void; export type SetValueFn = (value: T, options?: ControlOptions) => void; export type FormBuilderFn | FormType) = FormDefinition, Data = Record> = (state: any, options: { controlId: string; }) => T | RxapFormControl; export type ControlInsertedFn = (index: number, controlOrDefinition: AbstractControl | FormDefinition) => void; export type ControlRemovedFn = (index: number) => void; export interface FormArrayOptions extends RxapAbstractControlOptions { builder: FormBuilderFn; controlId: string; controlInsertedFn: ControlInsertedFn; controlRemovedFn: ControlRemovedFn; } export interface FormGroupOptions extends RxapAbstractControlOptions { controlId: string; }