src/lib/form-control.ts
UntypedFormControl
Properties |
|
Methods |
|
Accessors |
constructor(formState: OrBoxedValue<T>, options)
|
|||||||||
|
Defined in src/lib/form-control.ts:124
|
|||||||||
|
Parameters :
|
| Readonly controlId |
Type : string
|
|
Defined in src/lib/form-control.ts:95
|
| Readonly dirty$ |
Default value : this.dirtyChanges
.asObservable()
.pipe(distinctUntilChanged())
|
|
Defined in src/lib/form-control.ts:75
|
| Readonly disabled$ |
Default value : controlDisabled$<T>(this)
|
|
Defined in src/lib/form-control.ts:80
|
| Readonly enabled$ |
Default value : controlEnabled$<T>(this)
|
|
Defined in src/lib/form-control.ts:81
|
| Readonly errors |
Type : E | null
|
|
Defined in src/lib/form-control.ts:59
|
| Readonly errors$ |
Default value : controlErrorChanges$<E>(this)
|
|
Defined in src/lib/form-control.ts:93
|
| Readonly initialState |
Type : OrBoxedValue<T>
|
|
Defined in src/lib/form-control.ts:66
|
| Readonly readonly$ |
Default value : controlReadonly$<T>(this)
|
|
Defined in src/lib/form-control.ts:100
|
| Readonly stateChanges |
Default value : new Subject<void>()
|
|
Defined in src/lib/form-control.ts:99
|
| Readonly status |
Type : ControlState
|
|
Defined in src/lib/form-control.ts:64
|
| Readonly status$ |
Default value : controlStatusChanges$<T>(this)
|
|
Defined in src/lib/form-control.ts:92
|
| Readonly statusChanges |
Type : Observable<ControlState>
|
|
Defined in src/lib/form-control.ts:65
|
| Readonly touch$ |
Default value : this.touchChanges
.asObservable()
.pipe(distinctUntilChanged())
|
|
Defined in src/lib/form-control.ts:72
|
| Readonly value |
Type : T
|
|
Defined in src/lib/form-control.ts:58
|
| Readonly value$ |
Type : Observable<T>
|
Default value : controlValueChanges$<T>(this)
|
|
Defined in src/lib/form-control.ts:79
|
| Readonly valueChanges |
Type : Observable<T>
|
|
Defined in src/lib/form-control.ts:62
|
| Public disabledWhile | |||||||||
disabledWhile(observable: Observable
|
|||||||||
|
Defined in src/lib/form-control.ts:181
|
|||||||||
|
Parameters :
Returns :
any
|
| Public enabledWhile | |||||||||
enabledWhile(observable: Observable
|
|||||||||
|
Defined in src/lib/form-control.ts:188
|
|||||||||
|
Parameters :
Returns :
any
|
| Public getError | ||||||
getError(errorCode: K)
|
||||||
|
Defined in src/lib/form-control.ts:269
|
||||||
Type parameters :
|
||||||
|
Parameters :
Returns :
| null
|
| Public hasError | ||||||
hasError(errorCode: K)
|
||||||
|
Defined in src/lib/form-control.ts:273
|
||||||
Type parameters :
|
||||||
|
Parameters :
Returns :
any
|
| Public hasErrorAndDirty | ||||||
hasErrorAndDirty(error: ExtractStrings<E>)
|
||||||
|
Defined in src/lib/form-control.ts:289
|
||||||
|
Parameters :
Returns :
boolean
|
| Public hasErrorAndTouched | ||||||
hasErrorAndTouched(error: ExtractStrings<E>)
|
||||||
|
Defined in src/lib/form-control.ts:285
|
||||||
|
Parameters :
Returns :
boolean
|
| Public markAllAsDirty |
markAllAsDirty()
|
|
Defined in src/lib/form-control.ts:228
|
|
Returns :
void
|
| Public markAsDirty | ||||||
markAsDirty(opts?: OnlySelf)
|
||||||
|
Defined in src/lib/form-control.ts:223
|
||||||
|
Parameters :
Returns :
void
|
| Public markAsPristine | ||||||
markAsPristine(opts?: OnlySelf)
|
||||||
|
Defined in src/lib/form-control.ts:218
|
||||||
|
Parameters :
Returns :
void
|
| Public markAsTouched | ||||||
markAsTouched(opts?: OnlySelf)
|
||||||
|
Defined in src/lib/form-control.ts:208
|
||||||
|
Parameters :
Returns :
void
|
| Public markAsUntouched | ||||||
markAsUntouched(opts?: OnlySelf)
|
||||||
|
Defined in src/lib/form-control.ts:213
|
||||||
|
Parameters :
Returns :
void
|
| Public mergeAsyncValidators | ||||||
mergeAsyncValidators(validators: AsyncValidator)
|
||||||
|
Defined in src/lib/form-control.ts:199
|
||||||
|
Parameters :
Returns :
void
|
| Public mergeValidators | ||||||
mergeValidators(validators: Validator)
|
||||||
|
Defined in src/lib/form-control.ts:195
|
||||||
|
Parameters :
Returns :
void
|
| Public patchValue | |||||||||
patchValue(valueOrObservable: Observable
|
|||||||||
|
Defined in src/lib/form-control.ts:163
|
|||||||||
|
Parameters :
Returns :
Subscription
|
| Public patchValue | |||||||||
patchValue(valueOrObservable: T, options?: ControlOptions)
|
|||||||||
|
Defined in src/lib/form-control.ts:167
|
|||||||||
|
Parameters :
Returns :
void
|
| Public patchValue | |||||||||
patchValue(valueOrObservable: any, options?: ControlOptions)
|
|||||||||
|
Defined in src/lib/form-control.ts:168
|
|||||||||
|
Parameters :
Returns :
Subscription | void
|
| Public registerOnSetValue | ||||||
registerOnSetValue(setValueFn: SetValueFn<T>)
|
||||||
|
Defined in src/lib/form-control.ts:301
|
||||||
|
Parameters :
Returns :
void
|
| Public reset | |||||||||
reset(formState?: OrBoxedValue<T>, options?: ControlEventOptions)
|
|||||||||
|
Defined in src/lib/form-control.ts:232
|
|||||||||
|
Parameters :
Returns :
void
|
| Public setAsyncValidators | ||||||||||||
setAsyncValidators(newValidator: AsyncValidator, updateValueAndValidity)
|
||||||||||||
|
Defined in src/lib/form-control.ts:255
|
||||||||||||
|
Parameters :
Returns :
void
|
| Public setDisable | ||||||||||||
setDisable(disable, opts?: ControlEventOptions)
|
||||||||||||
|
Defined in src/lib/form-control.ts:297
|
||||||||||||
|
Parameters :
Returns :
void
|
| Public setEnable | ||||||||||||
setEnable(enable, opts?: ControlEventOptions)
|
||||||||||||
|
Defined in src/lib/form-control.ts:293
|
||||||||||||
|
Parameters :
Returns :
void
|
| Public setError |
setError(key: string, value: any, opts: EmitEvent)
|
|
Defined in src/lib/form-control.ts:281
|
|
Returns :
void
|
| Public setErrors | ||||||||||||
setErrors(errors: Partial
|
||||||||||||
|
Defined in src/lib/form-control.ts:277
|
||||||||||||
|
Parameters :
Returns :
any
|
| Public setValidators | ||||||||||||
setValidators(newValidator: Validator, updateValueAndValidity)
|
||||||||||||
|
Defined in src/lib/form-control.ts:245
|
||||||||||||
|
Parameters :
Returns :
void
|
| Public setValue | |||||||||
setValue(valueOrObservable: Observable
|
|||||||||
|
Defined in src/lib/form-control.ts:141
|
|||||||||
|
Parameters :
Returns :
Subscription
|
| Public setValue | |||||||||
setValue(valueOrObservable: T, options?: ControlOptions)
|
|||||||||
|
Defined in src/lib/form-control.ts:145
|
|||||||||
|
Parameters :
Returns :
void
|
| Public setValue | |||||||||
setValue(valueOrObservable: any, options?: ControlOptions)
|
|||||||||
|
Defined in src/lib/form-control.ts:146
|
|||||||||
|
Parameters :
Returns :
Subscription | void
|
| Public validateOn | ||||||
validateOn(observableValidation: Observable
|
||||||
|
Defined in src/lib/form-control.ts:265
|
||||||
|
Parameters :
Returns :
any
|
| readonly | ||||||
getreadonly()
|
||||||
|
Defined in src/lib/form-control.ts:83
|
||||||
setreadonly(value: boolean)
|
||||||
|
Defined in src/lib/form-control.ts:87
|
||||||
|
Parameters :
Returns :
void
|
| controlPath |
getcontrolPath()
|
|
Defined in src/lib/form-control.ts:102
|
| fullControlPath |
getfullControlPath()
|
|
Defined in src/lib/form-control.ts:116
|
import { UntypedFormControl } from '@angular/forms';
import { coerceArray } from '@rxap/utilities';
import {
isObservable,
Observable,
Subject,
Subscription,
} from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
import {
controlDisabled$,
controlDisabledWhile,
controlEnabled$,
controlEnabledWhile,
controlErrorChanges$,
controlReadonly$,
controlStatusChanges$,
controlValueChanges$,
disableControl,
enableControl,
hasErrorAndDirty,
hasErrorAndTouched,
mergeControlValidators,
validateControlOn,
} from './control-actions';
import {
FormDefinition,
FormType,
RxapAbstractControlOptions,
SetValueFn,
} from './model';
import {
AsyncValidator,
ControlEventOptions,
ControlOptions,
ControlState,
EmitEvent,
ExtractStrings,
OnlySelf,
OrBoxedValue,
Validator,
} from './types';
export class RxapFormControl<
T = any,
E extends object = any,
Parent extends object = any
> extends UntypedFormControl {
/**
* @internal
*/
public get rxapFormDefinition():
| (FormType<Parent> & FormDefinition<Parent>)
| undefined {
return (this.parent as any).rxapFormDefinition;
}
override readonly value!: T;
override readonly errors!: E | null;
// TODO : find solution to only overwrite the type with out impl the getter or setter logic
// readonly asyncValidator!: AsyncValidatorFn<T>;
override readonly valueChanges!: Observable<T>;
// @ts-expect-error overwrite the public type
override readonly status!: ControlState;
override readonly statusChanges!: Observable<ControlState>;
readonly initialState!: OrBoxedValue<T>;
private _readonly = false;
private touchChanges = new Subject<boolean>();
private dirtyChanges = new Subject<boolean>();
readonly touch$ = this.touchChanges
.asObservable()
.pipe(distinctUntilChanged());
readonly dirty$ = this.dirtyChanges
.asObservable()
.pipe(distinctUntilChanged());
readonly value$: Observable<T> = controlValueChanges$<T>(this);
readonly disabled$ = controlDisabled$<T>(this);
readonly enabled$ = controlEnabled$<T>(this);
public get readonly(): boolean {
return (this.parent as any)?.readonly ?? this._readonly;
}
public set readonly(value: boolean) {
this._readonly = value;
this.stateChanges.next();
}
readonly status$ = controlStatusChanges$<T>(this);
readonly errors$ = controlErrorChanges$<E>(this);
readonly controlId: string;
private readonly _onSetValue: SetValueFn<T>[] = [];
readonly stateChanges = new Subject<void>();
readonly readonly$ = controlReadonly$<T>(this);
public get controlPath(): string {
const parent: any = this.parent;
if (parent) {
if (parent.controlPath) {
if (parent === this.root) {
return this.controlId;
} else {
return [ parent.controlPath, this.controlId ].join('.');
}
}
}
return this.controlId;
}
public get fullControlPath(): string {
const parent: any = this.parent;
if (parent) {
if (parent.fullControlPath) {
return [ parent.fullControlPath, this.controlId ].join('.');
}
}
return this.controlId;
}
constructor(
formState: OrBoxedValue<T>,
options: RxapAbstractControlOptions & { controlId: string },
) {
super(formState, options);
this.controlId = options.controlId;
if (options.disabled) {
this.disable({ emitEvent: false });
}
if (options.readonly) {
this.readonly = true;
}
this.initialState = formState;
}
public override setValue(
valueOrObservable: Observable<T>,
options?: ControlOptions,
): Subscription;
public override setValue(valueOrObservable: T, options?: ControlOptions): void;
public override setValue(
valueOrObservable: any,
options?: ControlOptions,
): Subscription | void {
if (isObservable(valueOrObservable)) {
return (valueOrObservable as Observable<T>).subscribe((value) => {
super.setValue(value, options);
this._onSetValue.forEach((setValueFn) => setValueFn(value, options));
});
}
super.setValue(valueOrObservable, options);
this._onSetValue.forEach((setValueFn) =>
setValueFn(valueOrObservable, options),
);
}
public override patchValue(
valueOrObservable: Observable<T>,
options?: ControlOptions,
): Subscription;
public override patchValue(valueOrObservable: T, options?: ControlOptions): void;
public override patchValue(
valueOrObservable: any,
options?: ControlOptions,
): Subscription | void {
if (isObservable(valueOrObservable)) {
return valueOrObservable.subscribe((value) =>
super.patchValue(value, options),
);
}
super.patchValue(valueOrObservable, options);
}
public disabledWhile(
observable: Observable<boolean>,
options?: ControlOptions,
) {
return controlDisabledWhile(this, observable, options);
}
public enabledWhile(
observable: Observable<boolean>,
options?: ControlOptions,
) {
return controlEnabledWhile(this, observable, options);
}
public mergeValidators(validators: Validator) {
mergeControlValidators(this, validators);
}
public mergeAsyncValidators(validators: AsyncValidator) {
this.setAsyncValidators([
// TODO : remove 'as any' if solution for the type overwrite issue is found (above)
this.asyncValidator as any,
...coerceArray(validators),
]);
this.updateValueAndValidity();
}
public override markAsTouched(opts?: OnlySelf): void {
super.markAsTouched(opts);
this.touchChanges.next(true);
}
public override markAsUntouched(opts?: OnlySelf): void {
super.markAsUntouched(opts);
this.touchChanges.next(false);
}
public override markAsPristine(opts?: OnlySelf): void {
super.markAsPristine(opts);
this.dirtyChanges.next(false);
}
public override markAsDirty(opts?: OnlySelf): void {
super.markAsDirty(opts);
this.dirtyChanges.next(true);
}
public markAllAsDirty(): void {
this.markAsDirty({ onlySelf: true });
}
public override reset(
formState?: OrBoxedValue<T>,
options?: ControlEventOptions,
): void {
const newState = formState ?? this.initialState;
if (typeof newState === 'function') {
super.reset((newState as any)(), options);
} else {
super.reset(newState, options);
}
}
public override setValidators(
newValidator: Validator,
updateValueAndValidity = true,
): void {
super.setValidators(newValidator);
if (updateValueAndValidity) {
super.updateValueAndValidity();
}
}
public override setAsyncValidators(
newValidator: AsyncValidator,
updateValueAndValidity = true,
): void {
super.setAsyncValidators(newValidator);
if (updateValueAndValidity) {
super.updateValueAndValidity();
}
}
public validateOn(observableValidation: Observable<null | object>) {
return validateControlOn(this, observableValidation);
}
public override getError<K extends ExtractStrings<E>>(errorCode: K): E[K] | null {
return super.getError(errorCode) as E[K] | null;
}
public override hasError<K extends ExtractStrings<E>>(errorCode: K) {
return super.hasError(errorCode);
}
public override setErrors(errors: Partial<E> | null, opts: EmitEvent = {}) {
return super.setErrors(errors, opts);
}
public setError(key: string, value: any, opts: EmitEvent = {}): void {
super.setErrors({ [key]: value }, opts);
}
public hasErrorAndTouched(error: ExtractStrings<E>): boolean {
return hasErrorAndTouched(this, error);
}
public hasErrorAndDirty(error: ExtractStrings<E>): boolean {
return hasErrorAndDirty(this, error);
}
public setEnable(enable = true, opts?: ControlEventOptions) {
enableControl(this, enable, opts);
}
public setDisable(disable = true, opts?: ControlEventOptions) {
disableControl(this, disable, opts);
}
public registerOnSetValue(setValueFn: SetValueFn<T>) {
this._onSetValue.push(setValueFn);
}
}