Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: only emit value when control is valid #183

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
name: '@ngneat/reactive-forms'
name: 'lothern/reactive-forms'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, those changes weren't supposed to make it into the master branch. I've corrected it.


on:
workflow_dispatch:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entire code uses lothern?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, those changes weren't supposed to make it into the master branch. I've corrected it.

push:
branches:
- master
pull_request:

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: true

steps:
- uses: actions/checkout@v2
- name: Cache node modules
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ const control = new FormControl('');
control.value$.subscribe(value => ...);
```

### `validValue$`

Similar to value$; Observes the control's value and **only** emits if the control is valid.

```ts
import { FormControl } from '@ngneat/reactive-forms';

const control = new FormControl(null, [Validators.required]);
control.validValue$.subscribe(value => ...);
```

### `disabled$`

Observes the control's `disable` status.
Expand Down
11 changes: 10 additions & 1 deletion libs/reactive-forms/src/lib/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AbstractControl, ValidationErrors } from '@angular/forms';
import { defer, merge, Observable, of, Subscription } from 'rxjs';
import { distinctUntilChanged, map } from 'rxjs/operators';
import { distinctUntilChanged, filter, map } from 'rxjs/operators';

export function selectControlValue$<T, R>(
control: any,
Expand All @@ -21,6 +21,15 @@ export function controlValueChanges$<T>(
) as Observable<T>;
}

export function controlValidValueChanges$<T>(
control: AbstractControl & { getRawValue: () => T }
): Observable<T> {
return merge(
defer(() => of(control.getRawValue())),
control.valueChanges.pipe(filter(() => control.valid), map(() => control.getRawValue()))
) as Observable<T>;
}

export type ControlState = 'VALID' | 'INVALID' | 'PENDING' | 'DISABLED';

export function controlStatus$<
Expand Down
28 changes: 27 additions & 1 deletion libs/reactive-forms/src/lib/form-array.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Validators } from '@angular/forms';
import { expectTypeOf } from 'expect-type';
import { Observable, of, Subject, Subscription } from 'rxjs';
import {ControlsOf, FormControl, FormGroup, ValuesOf} from '..';
import {ControlsOf, FormControl, FormGroup} from '..';
import { ControlState } from './core';
import { FormArray } from './form-array';

Expand Down Expand Up @@ -224,6 +224,13 @@ const createArray = (withError = false) => {
);
};

const createInvalidArray = (withError = false) => {
return new FormArray<string | null>(
[new FormControl(null, Validators.required), new FormControl(null, Validators.required)],
withError ? errorFn : []
);
};

describe('FormArray Functionality', () => {
it('should valueChanges$', () => {
const control = createArray();
Expand All @@ -240,6 +247,25 @@ describe('FormArray Functionality', () => {
expect(spy).toHaveBeenCalledWith(['1', '3', '']);
});

it('should validValueChanges$', () => {
const control = createInvalidArray();
const spy = jest.fn();
control.validValue$.subscribe(spy);
expect(spy).toHaveBeenCalledTimes(1);
control.patchValue(['1', '2']);
expect(spy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledWith(['1', '2']);
control.push(new FormControl('3', Validators.required));
expect(spy).toHaveBeenCalledTimes(3);
expect(spy).toHaveBeenCalledWith(['1', '2', '3']);
control.push(new FormControl(null, Validators.required));
expect(spy).toHaveBeenCalledTimes(3);
expect(spy).not.toHaveReturnedWith(['1', '2', '3', null]);
control.removeAt(3);
expect(spy).toHaveBeenCalledTimes(4);
expect(spy).toHaveBeenCalledWith(['1', '2', '3']);
});

it('should disabledChanges$', () => {
const control = createArray();
const spy = jest.fn();
Expand Down
2 changes: 2 additions & 0 deletions libs/reactive-forms/src/lib/form-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
disableControl,
enableControl,
markAllDirty,
controlValidValueChanges$,
} from './core';
import { DeepPartial } from './types';

Expand Down Expand Up @@ -46,6 +47,7 @@ export class FormArray<
.asObservable()
.pipe(distinctUntilChanged());
readonly value$ = controlValueChanges$<Array<ValueOfControl<Control>>>(this);
readonly validValue$ = controlValidValueChanges$<Array<ValueOfControl<Control>>>(this);
readonly disabled$ = controlStatus$(this, 'disabled');
readonly enabled$ = controlStatus$(this, 'enabled');
readonly invalid$ = controlStatus$(this, 'invalid');
Expand Down
13 changes: 13 additions & 0 deletions libs/reactive-forms/src/lib/form-control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ describe('FormControl Functionality', () => {
expect(spy).toHaveBeenCalledWith('patched');
});

it('should validValueChanges$', () => {
const control = new FormControl<string | null>(null, Validators.required);
const spy = jest.fn();
control.validValue$.subscribe(spy);
expect(spy).toHaveBeenCalledTimes(1);
control.patchValue('patched');
expect(spy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledWith('patched');
control.patchValue('');
expect(spy).toHaveBeenCalledTimes(2);
expect(spy).not.toHaveBeenCalledWith('');
});

it('should disabledChanges$', () => {
const control = new FormControl<string>();
const spy = jest.fn();
Expand Down
2 changes: 2 additions & 0 deletions libs/reactive-forms/src/lib/form-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
removeError,
hasErrorAnd,
controlErrorChanges$,
controlValidValueChanges$,
} from './core';
import { BoxedValue } from './types';

Expand All @@ -34,6 +35,7 @@ export class FormControl<T> extends UntypedFormControl {
.asObservable()
.pipe(distinctUntilChanged());
readonly value$ = controlValueChanges$<T>(this);
readonly validValue$ = controlValidValueChanges$<T>(this);
readonly disabled$ = controlStatus$(this, 'disabled');
readonly enabled$ = controlStatus$(this, 'enabled');
readonly invalid$ = controlStatus$(this, 'invalid');
Expand Down
55 changes: 55 additions & 0 deletions libs/reactive-forms/src/lib/form-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ const createGroup = () => {
);
};

const createInvalidGroup = () => {
return new FormGroup(
{
name: new FormControl<string | null>(null, Validators.required),
phone: new FormGroup({
num: new FormControl<number | null>(null, Validators.required),
prefix: new FormControl<number | null>(null, Validators.required),
}),
}
);
};

describe('FormGroup Functionality', () => {
it('should valueChanges$', () => {
const control = createGroup();
Expand All @@ -46,6 +58,49 @@ describe('FormGroup Functionality', () => {
});
});

it('should validValueChanges$', () => {
const control = createInvalidGroup();
const spy = jest.fn();
control.validValue$.subscribe(spy);

expect(spy).toHaveBeenCalledTimes(1);
control.patchValue({
name: 'jim',
phone: {
num: 0,
prefix: 1
}
});

expect(spy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledWith({
name: 'jim',
phone: { num: 0, prefix: 1 },
});

control.patchValue({
name: 'changed',
});

expect(spy).toHaveBeenCalledTimes(3);
expect(spy).toHaveBeenCalledWith({
name: 'changed',
phone: { num: 0, prefix: 1 },
});

control.patchValue({
phone: {
num: null
}
});

expect(spy).toHaveBeenCalledTimes(3);
expect(spy).not.toHaveBeenCalledWith({
name: 'changed',
phone: { num: null, prefix: 1 },
});
});

it('should disabledChanges$', () => {
const control = createGroup();
const spy = jest.fn();
Expand Down
2 changes: 2 additions & 0 deletions libs/reactive-forms/src/lib/form-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
controlEnabledWhile,
controlErrorChanges$,
controlStatus$,
controlValidValueChanges$,
controlValueChanges$,
disableControl,
enableControl,
Expand All @@ -36,6 +37,7 @@ export class FormGroup<T extends Record<string, any>> extends UntypedFormGroup {
.asObservable()
.pipe(distinctUntilChanged());
readonly value$ = controlValueChanges$<ValuesOf<T>>(this);
readonly validValue$ = controlValidValueChanges$<ValuesOf<T>>(this);
readonly disabled$ = controlStatus$(this, 'disabled');
readonly enabled$ = controlStatus$(this, 'enabled');
readonly invalid$ = controlStatus$(this, 'invalid');
Expand Down