Guest User

Untitled

a guest
May 27th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import { FormControl } from '@angular/forms';
  2. import { concat, of, Observable } from 'rxjs';
  3. import { map } from 'rxjs/operators';
  4.  
  5. type Nullable<T> = T | null;
  6.  
  7. /**
  8. * @see AbstractControl.status
  9. */
  10. export enum RxFormControlStatus {
  11. VALID = 'VALID',
  12. INVALID = 'INVALID',
  13. PENDING = 'PENDING',
  14. DISABLED = 'DISABLED',
  15. }
  16.  
  17. export class RxFormControl<T = Nullable<string>> {
  18. constructor(public _formControl: FormControl) {}
  19.  
  20. /**
  21. * Shortcut.
  22. * @see AbstractControl.valueChanges
  23. */
  24. public changes$: Observable<Nullable<T>> = this._formControl.valueChanges;
  25.  
  26. /**
  27. * The value of the control.
  28. */
  29. public value$: Observable<Nullable<T>> = concat(of(this._formControl.value), this.changes$);
  30.  
  31. /**
  32. * The validation status of the control.
  33. */
  34. public status$: Observable<RxFormControlStatus> = concat(of(this._formControl.status), this._formControl.statusChanges);
  35.  
  36. /**
  37. * Is the control status invalid.
  38. */
  39. public invalid$: Observable<boolean> = this.status$.pipe(map(status => status === RxFormControlStatus.INVALID));
  40.  
  41. /**
  42. * Shortcut.
  43. * @see FormControl.setValue
  44. */
  45. public setValue(value: Nullable<T>, options?: {
  46. onlySelf?: boolean;
  47. emitEvent?: boolean;
  48. emitModelToViewChange?: boolean;
  49. emitViewToModelChange?: boolean;
  50. }) {
  51. return this._formControl.setValue(value, options);
  52. }
  53. }
Add Comment
Please, Sign In to add comment