import { Boxed } from './boxing'; export declare type FormControlValueTypes = Boxed | string | number | boolean | null | undefined; export declare type FormControlId = string; /** * This type represents a collection of named errors. */ export interface ValidationErrors { readonly [key: string]: any; } export interface KeyValue { [key: string]: any; } /** * Base interface for all types of form states. */ export interface AbstractControlState { /** * The unique ID of the form state. Usually this is the name or index * of the control in the form value prefixed by the ID of the containing * group or array, e.g. `MY_FORM.someTextInput` or `MY_FORM.0`. */ readonly id: string; /** * The value of the form state. */ readonly value: TValue; /** * This property is `true` if the state does not have any errors. */ readonly isValid: boolean; /** * This property is `true` if the state has at least one error. */ readonly isInvalid: boolean; /** * The errors of the state. This property always has a value. * If the state has no errors the property is set to `{}`. */ readonly errors: ValidationErrors; /** * The names of all asynchronous validations currently running * for the state. */ readonly pendingValidations: ReadonlyArray; /** * This property indicates whether the control is currently being * asynchronously validated. */ readonly isValidationPending: boolean; /** * This property indicates whether the state is enabled. When it * is `false` the `errors` are always `{}` (i.e. the state is * always valid if disabled) and `pendingValidations` is always `[]` * (i.e. all pending validations are cancelled). */ readonly isEnabled: boolean; /** * This property indicates whether the state is disabled. When it * is `true` the `errors` are always `{}` (i.e. the state is * always valid if disabled) and `pendingValidations` is always `[]` * (i.e. all pending validations are cancelled). */ readonly isDisabled: boolean; /** * This property is set to `true` as soon as the state's value changes. */ readonly isDirty: boolean; /** * This property is `true` as long as the state's value never changed. */ readonly isPristine: boolean; /** * This property is set to `true` as soon as the state is touched. */ readonly isTouched: boolean; /** * This property is `true` as long as the state is not touched. */ readonly isUntouched: boolean; /** * This property is set to `true` as soon as the state is submitted. */ readonly isSubmitted: boolean; /** * This property is `true` as long as the state is not submitted. */ readonly isUnsubmitted: boolean; /** * This property is a container for user-defined metadata (e.g. if * you wanted to count the number of times a state's value has been * changed, what keys were pressed on an input, or how often a form * has been submitted etc.). While it is possible to store this kind * of information outside of **pure-forms** in your own state the * `userDefinedProperties` allow you to store your own metadata * directly in the state. */ readonly userDefinedProperties: KeyValue; } /** * State associated with a form control, i.e. an HTML form * element in the view (e.g. `input`, `select`, `textarea` etc.). */ export interface FormControlState extends AbstractControlState { /** * The value of the form state. Form controls only support values of * type `string`, `number`, `boolean`, `null`, and `undefined` to * keep the state string serializable. */ readonly value: TValue; /** * This property is `true` if the form control does not have any errors. */ readonly isValid: boolean; /** * This property is `true` if the form control has at least one error. */ readonly isInvalid: boolean; /** * The errors of the form control. This property always has a value. * If the control has no errors the property is set to `{}`. */ readonly errors: ValidationErrors; /** * The names of all asynchronous validations currently running for the * form control. */ readonly pendingValidations: ReadonlyArray; /** * This property indicates whether the control is currently being * asynchronously validated (i.e. this is `true` if and only if * `pendingValidations` is not empty). */ readonly isValidationPending: boolean; /** * This property indicates whether the form control is enabled. * When it is `false` the `errors` are always `{}` (i.e. the form * control is always valid if disabled) and `pendingValidations` * is always `[]` (i.e. all pending validations are cancelled). */ readonly isEnabled: boolean; /** * This property indicates whether the form control is disabled. * When it is `true` the `errors` are always `{}` (i.e. the form * control is always valid if disabled) and `pendingValidations` * is always `[]` (i.e. all pending validations are cancelled). */ readonly isDisabled: boolean; /** * This property is set to `true` as soon as the underlying * `FormViewAdapter` or `ControlValueAccessor` reports a new * value for the first time. */ readonly isDirty: boolean; /** * This property is `true` as long as the underlying * `FormViewAdapter` or `ControlValueAccessor` has never * reported a new value. */ readonly isPristine: boolean; /** * This property is set to `true` based on the rules of the * underlying `FormViewAdapter` (usually on `blur` for most form * elements). */ readonly isTouched: boolean; /** * This property is `true` as long as the control is not touched. */ readonly isUntouched: boolean; /** * This property is set to `true` as soon as the group or array * containing this form control is submitted. A form control can * never be submitted on its own. */ readonly isSubmitted: boolean; /** * This property is `true` as long as the state is not submitted. */ readonly isUnsubmitted: boolean; /** * This property is set to `true` if the form control currently * has focus. */ readonly isFocused: boolean; /** * This property is `true` if the control currently does not have * focus or focus tracking is not enabled for the form control. */ readonly isUnfocused: boolean; } /** * This type represents the child control states of a form group. */ export declare type FormGroupControls = { readonly [controlId in keyof TValue]: FormState; }; /** * Form groups are collections of named controls. Just like controls * groups are represented as plain state objects. The state of a * group is determined almost fully by its child states. */ export interface FormGroupState extends AbstractControlState { /** * The aggregated value of the form group. The value is computed by * aggregating the values of all children, e.g. * * ```typescript * { * child1: 'some value', * child2: { * nestedChild: 10, * }, * } * ``` */ readonly value: TValue; /** * This property is `true` if the form group does not have any errors * itself and none of its children have any errors. */ readonly isValid: boolean; /** * This property is `true` if the form group or any of its children * have at least one error. */ readonly isInvalid: boolean; /** * The errors of the form group. This property is computed by merging * the errors of the group with the errors of all its children where * the child errors are a property of the `errors` object prefixed with * an underscore, e.g. * * ``` * { * groupError: true, * _child: { * childError: true, * }, * } * ``` * * If neither the group nor any children have errors the property is * set to `{}`. */ readonly errors: ValidationErrors; /** * The names of all asynchronous validations currently running for the * form group. */ readonly pendingValidations: ReadonlyArray; /** * This property indicates whether the group or any of its children * are currently being asynchronously validated. */ readonly isValidationPending: boolean; /** * This property indicates whether the form group is enabled. It is * `true` if and only if at least one of its child states is * enabled. When it is `false` the `errors` are always `{}` (i.e. * the form group is always valid if disabled) and `pendingValidations` * is always `[]` (i.e. all pending validations are cancelled). */ readonly isEnabled: boolean; /** * This property indicates whether the form group is disabled. It is * `true` if and only if all of its child state are disabled. When * it is `true` the `errors` are always `{}` (i.e. the form group * is always valid if disabled) and `pendingValidations` is always * `[]` (i.e. all pending validations are cancelled). */ readonly isDisabled: boolean; /** * This property is `true` if and only if at least one of the form * group's child states is marked as dirty. */ readonly isDirty: boolean; /** * This property is `true` if and only if all of the form group's * child states are pristine. */ readonly isPristine: boolean; /** * This property is `true` if and only if at least one of the form * group's child states is marked as touched. */ readonly isTouched: boolean; /** * This property is `true` if and only if all of the form group's * child states are untouched. */ readonly isUntouched: boolean; /** * This property is set to `true` as soon as the form group is * submitted. */ readonly isSubmitted: boolean; /** * This property is `true` as long as the state is not submitted. */ readonly isUnsubmitted: boolean; /** * This property contains all child states of the form group. As * you may have noticed the type of each child state is * `AbstractControlState` which sometimes forces you to cast the * state explicitly. It is not possible to improve this typing * until [conditional mapped types](https://github.com/Microsoft/TypeScript/issues/12424) * are added to TypeScript. */ readonly controls: FormGroupControls; } /** * Form arrays are collections of controls. They are represented as * plain state arrays. The state of an array is determined almost * fully by its child states. */ export interface FormArrayState extends AbstractControlState { /** * The aggregated value of the form array. The value is computed by * aggregating the values of all children into an array. */ readonly value: TValue[]; /** * This property is `true` if the form array does not have any errors * itself and none of its children have any errors. */ readonly isValid: boolean; /** * This property is `true` if the form array or any of its children * have at least one error. */ readonly isInvalid: boolean; /** * The errors of the form array. This property is computed by merging * the errors of the array with the errors of all its children where * the child errors are a property of the `errors` object prefixed with * an underscore, e.g. * * ``` * { * arrayError: true, * _0: { * childError: true, * }, * } * ``` * * If neither the array nor any children have errors the property is * set to `{}`. */ readonly errors: ValidationErrors; /** * The names of all asynchronous validations currently running for the * form array. */ readonly pendingValidations: ReadonlyArray; /** * This property indicates whether the array or any of its children * are currently being asynchronously validated. */ readonly isValidationPending: boolean; /** * This property indicates whether the form array is enabled. It is * `true` if and only if at least one of its child states is * enabled. When it is `false` the `errors` are always `{}` (i.e. * the form array is always valid if disabled) and `pendingValidations` * is always `[]` (i.e. all pending validations are cancelled). */ readonly isEnabled: boolean; /** * This property indicates whether the form array is disabled. It is * `true` if and only if all of its child states are disabled. When * it is `true` the `errors` are always `{}` (i.e. the form array * is always valid if disabled) and `pendingValidations` is always * `[]` (i.e. all pending validations are cancelled). */ readonly isDisabled: boolean; /** * This property is `true` if and only if at least one of the form * array's child states is marked as dirty. */ readonly isDirty: boolean; /** * This property is `true` if and only if all of the form array's * child states are pristine. */ readonly isPristine: boolean; /** * This property is `true` if and only if at least one of the form * array's child states is marked as touched. */ readonly isTouched: boolean; /** * This property is `true` if and only if all of the form array's * child states are untouched. */ readonly isUntouched: boolean; /** * This property is set to `true` as soon as the form array is * submitted. */ readonly isSubmitted: boolean; /** * This property is `true` as long as the state is not submitted. */ readonly isUnsubmitted: boolean; /** * This property contains all child states of the form array. As * you may have noticed the type of each child state is * `AbstractControlState` which sometimes forces you to cast the * state explicitly. It is not possible to improve this typing * until [conditional mapped types](https://github.com/Microsoft/TypeScript/issues/12424) * are added to TypeScript. */ readonly controls: ReadonlyArray>; } /** * This is a helper type that allows working around the distributiveness * of conditional types. */ export interface InferenceWrapper { t: T; } /** * This is a helper type that infers the correct form state type based * on the type contained in the inference wrapper. */ export declare type InferredFormState> = T extends InferenceWrapper ? AbstractControlState : T extends InferenceWrapper ? AbstractControlState : T extends InferenceWrapper ? AbstractControlState : T extends InferenceWrapper ? AbstractControlState : T extends InferenceWrapper> ? FormControlState> : T extends InferenceWrapper | undefined> ? FormControlState | undefined> : T extends InferenceWrapper | null> ? FormControlState | null> : T extends InferenceWrapper | undefined | null> ? FormControlState | undefined | null> : T extends InferenceWrapper ? FormControlState : T extends InferenceWrapper ? FormControlState : T extends InferenceWrapper ? FormControlState : T extends InferenceWrapper ? FormControlState : T extends InferenceWrapper ? FormControlState : T extends InferenceWrapper ? FormControlState : T extends InferenceWrapper ? FormControlState : T extends InferenceWrapper ? FormControlState : T extends InferenceWrapper ? FormControlState : T extends InferenceWrapper ? FormControlState : T extends InferenceWrapper ? FormControlState : T extends InferenceWrapper ? FormControlState : T extends InferenceWrapper<(infer U)[]> ? FormArrayState : T extends InferenceWrapper<(infer U)[] | undefined> ? FormArrayState : T extends InferenceWrapper<(infer U)[] | null> ? FormArrayState : T extends InferenceWrapper<(infer U)[] | undefined | null> ? FormArrayState : T extends InferenceWrapper ? FormGroupState : never; /** * This is a type that can infer the concrete type of a form state based * on the given type parameter. */ export declare type FormState = InferredFormState>; /** * This function determines if a value is a form state. */ export declare function isFormState(state: any): state is FormState; /** * This function determines if a value is an array state. */ export declare function isArrayState(state: any): state is FormArrayState; /** * This function determines if a value is a group state. */ export declare function isGroupState(state: any): state is FormGroupState; export declare function createChildState(id: string, childValue: TValue): FormState; export declare function verifyFormControlValueIsValid(value: TValue): TValue; /** * This function creates a form control state with an ID and a value. */ export declare function createFormControlState(id: FormControlId, value: TValue): FormControlState; export declare function getFormGroupValue(controls: FormGroupControls, originalValue: TValue): TValue; export declare function getFormGroupErrors(controls: FormGroupControls, originalErrors: ValidationErrors): ValidationErrors; export declare function computeGroupState(id: string, controls: FormGroupControls, value: TValue, errors: ValidationErrors, pendingValidations: ReadonlyArray, userDefinedProperties: KeyValue, flags: { wasOrShouldBeDirty?: boolean; wasOrShouldBeEnabled?: boolean; wasOrShouldBeTouched?: boolean; wasOrShouldBeSubmitted?: boolean; }): FormGroupState; /** * This function creates a form group state with an ID and a value. * From the value the shape of the group state is inferred, i.e. * object properties are inferred as form groups, array properties * are inferred as form arrays, and primitive properties are inferred * as form controls. */ export declare function createFormGroupState(id: FormControlId, initialValue: TValue): FormGroupState; export declare function computeArrayState(id: string, inferredControls: ReadonlyArray>, value: TValue[], errors: ValidationErrors, pendingValidations: ReadonlyArray, userDefinedProperties: KeyValue, flags: { wasOrShouldBeDirty?: boolean; wasOrShouldBeEnabled?: boolean; wasOrShouldBeTouched?: boolean; wasOrShouldBeSubmitted?: boolean; }): FormArrayState; /** * This function creates a form array state with an ID and a value. * From the value the shape of the array state is inferred, i.e. * object values are inferred as form groups, array values * are inferred as form arrays, and primitive values are inferred * as form controls. */ export declare function createFormArrayState(id: FormControlId, initialValue: TValue[]): FormArrayState;