-
-
Notifications
You must be signed in to change notification settings - Fork 13
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: add ignoreInvalidForm config #11
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import { FormGroup } from '@angular/forms'; | |
import { merge, Subject } from 'rxjs'; | ||
import { Router } from '@angular/router'; | ||
import { coerceArray, resolveParams } from './utils'; | ||
import { auditTime, map, takeUntil } from 'rxjs/operators'; | ||
import { auditTime, debounceTime, filter, map, takeUntil } from 'rxjs/operators'; | ||
import { BindQueryParamsOptions, QueryParamParams, ResolveParamsOption } from './types'; | ||
import { QueryParamDef } from './QueryParamDef'; | ||
import set from 'lodash.set'; | ||
|
@@ -31,7 +31,20 @@ export class BindQueryParamsManager<T = any> { | |
this.updateControl(this.defs, { emitEvent: true }, (def) => def.strategy === 'twoWay'); | ||
|
||
const controls = this.defs.map((def) => { | ||
return this.group.get(def.path)!.valueChanges.pipe( | ||
const control = this.group.get(def.path)!; | ||
return control.valueChanges.pipe( | ||
debounceTime(0), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to use this to ensure |
||
filter(() => { | ||
if (this.options.ignoreInvalidForm) { | ||
return this.group.valid; | ||
} | ||
|
||
if (def.ignoreInvalidForm) { | ||
return control.valid; | ||
} | ||
|
||
return true; | ||
}), | ||
map((value) => ({ | ||
def, | ||
value, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { BIND_QUERY_PARAMS_OPTIONS, BindQueryParamsFactory } from '@ngneat/bind-query-params'; | ||
import { FormControl, FormGroup } from '@angular/forms'; | ||
import { FormControl, FormGroup, Validators } from '@angular/forms'; | ||
import { createComponentFactory, Spectator } from '@ngneat/spectator'; | ||
import { Component } from '@angular/core'; | ||
import { Router } from '@angular/router'; | ||
|
@@ -29,6 +29,7 @@ function assertRouterCall(spectator: Spectator<HomeComponent>, queryParams: Reco | |
} | ||
|
||
interface Params { | ||
withMinlengthValidator: string; | ||
searchTerm: string; | ||
'withBrackets[gte]': string; | ||
showErrors: boolean; | ||
|
@@ -46,6 +47,7 @@ interface Params { | |
}) | ||
class HomeComponent { | ||
group = new FormGroup({ | ||
withMinlengthValidator: new FormControl('', [Validators.minLength(5)]), | ||
searchTerm: new FormControl(), | ||
'withBrackets[gte]': new FormControl(), | ||
showErrors: new FormControl(false), | ||
|
@@ -64,6 +66,7 @@ class HomeComponent { | |
|
||
bindQueryParams = this.factory | ||
.create<Params>([ | ||
{ queryKey: 'withMinlengthValidator', ignoreInvalidForm: true }, | ||
{ queryKey: 'searchTerm' }, | ||
{ queryKey: 'withBrackets[gte]' }, | ||
{ queryKey: 'showErrors', type: 'boolean' }, | ||
|
@@ -121,6 +124,61 @@ describe('BindQueryParams', () => { | |
})); | ||
}); | ||
|
||
describe('ignoreInvalidForm', () => { | ||
it('should navigate only when control is valid', fakeAsync(() => { | ||
spectator = createComponent(); | ||
const router = spectator.inject(Router); | ||
|
||
spectator.component.group.patchValue({ | ||
withMinlengthValidator: 'with', | ||
}); | ||
|
||
tick(); | ||
|
||
expect(router.navigate).not.toHaveBeenCalled(); | ||
|
||
spectator.component.group.patchValue({ | ||
withMinlengthValidator: 'with1', | ||
}); | ||
|
||
tick(); | ||
|
||
assertRouterCall(spectator, { withMinlengthValidator: 'with1' }); | ||
})); | ||
|
||
it('should navigate only when group is valid', fakeAsync(() => { | ||
spectator = createComponent({ | ||
providers: [ | ||
{ | ||
provide: BIND_QUERY_PARAMS_OPTIONS, | ||
useValue: { | ||
ignoreInvalidForm: true, | ||
windowRef: window, | ||
}, | ||
}, | ||
], | ||
}); | ||
const router = spectator.inject(Router); | ||
|
||
spectator.component.group.patchValue({ | ||
searchTerm: 'term', | ||
withMinlengthValidator: 'with', | ||
}); | ||
|
||
tick(); | ||
|
||
expect(router.navigate).not.toHaveBeenCalled(); | ||
|
||
spectator.component.group.patchValue({ | ||
withMinlengthValidator: 'with1', | ||
}); | ||
|
||
tick(); | ||
|
||
assertRouterCall(spectator, { withMinlengthValidator: 'with1' }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @NetanelBasal I'm stucked in this test. I was hoping the router to be called with {
searchTerm: 'term',
withMinlengthValidator: 'with1',
} ... but, as the |
||
})); | ||
}); | ||
|
||
describe('string', () => { | ||
it('control => query', fakeAsync(() => { | ||
spectator = createComponent(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import { QueryParamDef } from './QueryParamDef'; | |
export type ParamDefType = 'boolean' | 'array' | 'number' | 'string' | 'object'; | ||
|
||
export type QueryParamParams<QueryParams = any> = { | ||
ignoreInvalidForm?: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, feel free to suggest another name if needed :) |
||
queryKey: keyof QueryParams & string; | ||
path?: string; | ||
type?: ParamDefType; | ||
|
@@ -12,6 +13,7 @@ export type QueryParamParams<QueryParams = any> = { | |
}; | ||
|
||
export interface BindQueryParamsOptions { | ||
ignoreInvalidForm?: boolean; | ||
windowRef: Window; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we're checking the group here? I think the check should only be if the control is valid. If that's the case, we can update the address.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that's interesting... if we didn't check the group validity, how can we prevent a router sync if any control is invalid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intention was not to sync the specific controls that are not valid. Why preventing other valid controls?