Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { FormControl } from '@angular/forms';
- import { concat, of, Observable } from 'rxjs';
- import { map } from 'rxjs/operators';
- type Nullable<T> = T | null;
- /**
- * @see AbstractControl.status
- */
- export enum RxFormControlStatus {
- VALID = 'VALID',
- INVALID = 'INVALID',
- PENDING = 'PENDING',
- DISABLED = 'DISABLED',
- }
- export class RxFormControl<T = Nullable<string>> {
- constructor(public _formControl: FormControl) {}
- /**
- * Shortcut.
- * @see AbstractControl.valueChanges
- */
- public changes$: Observable<Nullable<T>> = this._formControl.valueChanges;
- /**
- * The value of the control.
- */
- public value$: Observable<Nullable<T>> = concat(of(this._formControl.value), this.changes$);
- /**
- * The validation status of the control.
- */
- public status$: Observable<RxFormControlStatus> = concat(of(this._formControl.status), this._formControl.statusChanges);
- /**
- * Is the control status invalid.
- */
- public invalid$: Observable<boolean> = this.status$.pipe(map(status => status === RxFormControlStatus.INVALID));
- /**
- * Shortcut.
- * @see FormControl.setValue
- */
- public setValue(value: Nullable<T>, options?: {
- onlySelf?: boolean;
- emitEvent?: boolean;
- emitModelToViewChange?: boolean;
- emitViewToModelChange?: boolean;
- }) {
- return this._formControl.setValue(value, options);
- }
- }
Add Comment
Please, Sign In to add comment