@ng-stack/forms
Version:
> provides wrapped Angular's Reactive Forms to write its more strongly typed.
148 lines (147 loc) • 6.14 kB
TypeScript
import { UntypedFormControl as NativeFormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { Status, ValidationErrors, StringKeys, ValidatorFn, AsyncValidatorFn, AbstractControlOptions, ValidatorsModel, ExtractControlValue, FormControlState } from './types';
export declare class FormControl<T = any, V extends object = ValidatorsModel> extends NativeFormControl {
readonly value: ExtractControlValue<T>;
readonly valueChanges: Observable<ExtractControlValue<T>>;
readonly status: Status;
readonly statusChanges: Observable<Status>;
readonly errors: ValidationErrors<V> | null;
/**
* Creates a new `FormControl` instance.
*
* @param formState Initializes the control with an initial value,
* or an object that defines the initial value and disabled state.
*
* @param validatorOrOpts A synchronous validator function, or an array of
* such functions, or an `AbstractControlOptions` object that contains validation functions
* and a validation trigger.
*
* @param asyncValidator A single async validator or array of async validator functions
*
*/
constructor(formState?: FormControlState<T>, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null);
/**
* Sets a new value for the form control.
*
* @param value The new value for the control.
* @param options Configuration options that determine how the control proopagates changes
* and emits events when the value changes.
* The configuration options are passed to the
* [updateValueAndValidity](https://angular.io/api/forms/AbstractControl#updateValueAndValidity) method.
*
* * `onlySelf`: When true, each change only affects this control, and not its parent. Default is
* false.
* * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
* `valueChanges`
* observables emit events with the latest status and value when the control value is updated.
* When false, no events are emitted.
* * `emitModelToViewChange`: When true or not supplied (the default), each change triggers an
* `onChange` event to
* update the view.
* * `emitViewToModelChange`: When true or not supplied (the default), each change triggers an
* `ngModelChange`
* event to update the model.
*
*/
setValue(value: ExtractControlValue<T>, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
emitModelToViewChange?: boolean;
emitViewToModelChange?: boolean;
}): void;
/**
* Patches the value of a control.
*
* This function is functionally the same as [setValue](https://angular.io/api/forms/FormControl#setValue) at this level.
* It exists for symmetry with [patchValue](https://angular.io/api/forms/FormGroup#patchValue) on `FormGroups` and
* `FormArrays`, where it does behave differently.
*
* See also: `setValue` for options
*/
patchValue(value: ExtractControlValue<T>, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
emitModelToViewChange?: boolean;
emitViewToModelChange?: boolean;
}): void;
/**
* Resets the form control, marking it `pristine` and `untouched`, and setting
* the value to null.
*
* @param formState Resets the control with an initial value,
* or an object that defines the initial value and disabled state.
*
* @param options Configuration options that determine how the control propagates changes
* and emits events after the value changes.
*
* * `onlySelf`: When true, each change only affects this control, and not its parent. Default is
* false.
* * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
* `valueChanges`
* observables emit events with the latest status and value when the control is reset.
* When false, no events are emitted.
*
*/
reset(formState?: FormControlState<T>, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
}): void;
/**
* In `FormControl`, this method always returns `null`.
*/
get(): null;
/**
* Sets the synchronous validators that are active on this control. Calling
* this overwrites any existing sync validators.
*/
setValidators(newValidator: ValidatorFn | ValidatorFn[] | null): void;
/**
* Sets the async validators that are active on this control. Calling this
* overwrites any existing async validators.
*/
setAsyncValidators(newValidator: AsyncValidatorFn | AsyncValidatorFn[] | null): void;
/**
* Sets errors on a form control when running validations manually, rather than automatically.
*
* Calling `setErrors` also updates the validity of the parent control.
*
* ### Manually set the errors for a control
*
* ```ts
* const login = new FormControl('someLogin');
* login.setErrors({
* notUnique: true
* });
*
* expect(login.valid).toEqual(false);
* expect(login.errors).toEqual({ notUnique: true });
*
* login.setValue('someOtherLogin');
*
* expect(login.valid).toEqual(true);
* ```
*/
setErrors(errors: ValidationErrors | null, opts?: {
emitEvent?: boolean;
}): void;
/**
* Reports error data for the current control.
*
* @param errorCode The code of the error to check.
*
* @returns error data for that particular error. If an error is not present,
* null is returned.
*/
getError<K extends StringKeys<V> = any>(errorCode: K): V[K] | null;
/**
* Reports whether the current control has the error specified.
*
* @param errorCode The code of the error to check.
*
* @returns whether the given error is present in the current control.
*
* If an error is not present, false is returned.
*/
hasError<K extends StringKeys<V> = any>(errorCode: K): boolean;
}