Skip to content

Commit

Permalink
fix: πŸ› fix type of status$ property
Browse files Browse the repository at this point in the history
βœ… Closes: #113
  • Loading branch information
NetanelBasal committed Oct 25, 2021
1 parent 21ce8f2 commit a106a3e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 16 deletions.
10 changes: 6 additions & 4 deletions libs/reactive-forms/src/lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ export function controlValueChanges$<T>(
) as Observable<T>;
}

export function controlStatus$(
export type ControlState = 'VALID' | 'INVALID' | 'PENDING' | 'DISABLED';

export function controlStatus$<K extends 'disabled' | 'enabled' | 'status'>(
control: AbstractControl,
type: 'disabled' | 'enabled' | 'status'
): Observable<boolean> {
type: K
): Observable<K extends 'status' ? ControlState : boolean> {
return merge(
defer(() => of(control[type])),
control.statusChanges.pipe(
map(() => control[type]),
distinctUntilChanged()
)
) as Observable<boolean>;
) as Observable<any>;
}

export function enableControl(
Expand Down
3 changes: 2 additions & 1 deletion libs/reactive-forms/src/lib/form-array.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expectTypeOf } from 'expect-type';
import { Observable, of, Subject, Subscription } from 'rxjs';
import { FormControl, FormGroup } from '..';
import { ControlState } from './core';
import { FormArray } from './form-array';

type Base = {
Expand Down Expand Up @@ -32,7 +33,7 @@ describe('FormArray Types', () => {

expectTypeOf(arr.disabled$).toEqualTypeOf<Observable<boolean>>();
expectTypeOf(arr.enabled$).toEqualTypeOf<Observable<boolean>>();
expectTypeOf(arr.status$).toEqualTypeOf<Observable<boolean>>();
expectTypeOf(arr.status$).toEqualTypeOf<Observable<ControlState>>();

const first$ = arr.select((state) => {
expectTypeOf(state).toEqualTypeOf<string[]>();
Expand Down
10 changes: 5 additions & 5 deletions libs/reactive-forms/src/lib/form-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import { DeepPartial } from './types';
export class FormArray<
T,
Control extends AbstractControl = T extends Record<any, any>
? FormGroup<ControlsOf<T>>
: FormControl<T>
> extends NgFormArray {
? FormGroup<ControlsOf<T>>
: FormControl<T>
> extends NgFormArray {
readonly value!: T[];
readonly valueChanges!: Observable<T[]>;

Expand All @@ -41,8 +41,8 @@ export class FormArray<
.asObservable()
.pipe(distinctUntilChanged());
readonly value$ = controlValueChanges$<T[]>(this);
readonly disabled$: Observable<boolean> = controlStatus$(this, 'disabled');
readonly enabled$: Observable<boolean> = controlStatus$(this, 'enabled');
readonly disabled$ = controlStatus$(this, 'disabled');
readonly enabled$ = controlStatus$(this, 'enabled');
readonly status$ = controlStatus$(this, 'status');
readonly errors$ = controlErrorChanges$(
this,
Expand Down
3 changes: 2 additions & 1 deletion libs/reactive-forms/src/lib/form-control.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Validators } from '@angular/forms';
import { expectTypeOf } from 'expect-type';
import { Observable, of, Subject, Subscription } from 'rxjs';
import { ControlState } from './core';
import { FormControl } from './form-control';

describe('FormControl Functionality', () => {
Expand Down Expand Up @@ -201,7 +202,7 @@ describe('FormControl Types', () => {

expectTypeOf(control.disabled$).toEqualTypeOf<Observable<boolean>>();
expectTypeOf(control.enabled$).toEqualTypeOf<Observable<boolean>>();
expectTypeOf(control.status$).toEqualTypeOf<Observable<boolean>>();
expectTypeOf(control.status$).toEqualTypeOf<Observable<ControlState>>();

// @ts-expect-error - should be typed
control.reset(1);
Expand Down
4 changes: 2 additions & 2 deletions libs/reactive-forms/src/lib/form-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export class FormControl<T> extends NgFormControl {
.asObservable()
.pipe(distinctUntilChanged());
readonly value$ = controlValueChanges$<T>(this);
readonly disabled$: Observable<boolean> = controlStatus$(this, 'disabled');
readonly enabled$: Observable<boolean> = controlStatus$(this, 'enabled');
readonly disabled$ = controlStatus$(this, 'disabled');
readonly enabled$ = controlStatus$(this, 'enabled');
readonly status$ = controlStatus$(this, 'status');
readonly errors$ = controlErrorChanges$(
this,
Expand Down
3 changes: 2 additions & 1 deletion libs/reactive-forms/src/lib/form-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AbstractControl } from '@angular/forms';
import { Observable, of, Subject, Subscription } from 'rxjs';
import { ControlsOf } from '..';
import { ValuesOf } from './types';
import { ControlState } from './core';

const createGroup = () => {
return new FormGroup(
Expand Down Expand Up @@ -316,7 +317,7 @@ describe('FormGroup Types', () => {

expectTypeOf(group.disabled$).toEqualTypeOf<Observable<boolean>>();
expectTypeOf(group.enabled$).toEqualTypeOf<Observable<boolean>>();
expectTypeOf(group.status$).toEqualTypeOf<Observable<boolean>>();
expectTypeOf(group.status$).toEqualTypeOf<Observable<ControlState>>();

const name$ = group.select((state) => {
expectTypeOf(state).toEqualTypeOf<Base>();
Expand Down
4 changes: 2 additions & 2 deletions libs/reactive-forms/src/lib/form-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export class FormGroup<T extends Record<string, any>> extends NgFormGroup {
.asObservable()
.pipe(distinctUntilChanged());
readonly value$ = controlValueChanges$<ValuesOf<T>>(this);
readonly disabled$: Observable<boolean> = controlStatus$(this, 'disabled');
readonly enabled$: Observable<boolean> = controlStatus$(this, 'enabled');
readonly disabled$ = controlStatus$(this, 'disabled');
readonly enabled$ = controlStatus$(this, 'enabled');
readonly status$ = controlStatus$(this, 'status');
readonly errors$ = controlErrorChanges$(
this,
Expand Down

0 comments on commit a106a3e

Please sign in to comment.