Skip to content

Commit 128c233

Browse files
committed
feat(core): Added patchValue and setValue for QueryParamGroup
fixes #2
1 parent 164eee1 commit 128c233

4 files changed

Lines changed: 57 additions & 6 deletions

File tree

projects/ngqp-demo/src/app/playground/playground.component.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@
1111
<input type="number" class="form-control" queryParamName="counter" />
1212

1313
<input type="range" class="form-control" min="1" max="10" queryParamName="range" />
14-
</ng-container>
14+
</ng-container>
15+
16+
<button type="button" (click)="patchGroupValue()">Patch Group</button>
17+
<button type="button" (click)="setGroupValue()">Set Group</button>

projects/ngqp-demo/src/app/playground/playground.component.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,20 @@ export class PlaygroundComponent {
5555
});
5656
}
5757

58+
public patchGroupValue() {
59+
this.paramGroup.patchValue({
60+
checker: false,
61+
counter: 1337,
62+
range: 9,
63+
});
64+
}
65+
66+
public setGroupValue() {
67+
this.paramGroup.setValue({
68+
checker: false,
69+
counter: 1337,
70+
range: 9,
71+
});
72+
}
73+
5874
}

projects/ngqp/core/src/lib/model.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export type ParamDeserializer<T> = (value: string | null) => T | null;
1111
/** TODO Documentation */
1212
export type OnChangeFunction<T> = (value: T) => void;
1313

14+
/** TODO Documentation */
15+
export interface QueryParamGroupValue {
16+
[ controlName: string ]: any;
17+
}
18+
1419
/**
1520
* TODO Documentation
1621
*/
@@ -34,10 +39,10 @@ export interface QueryParamControlOpts<T> {
3439
*/
3540
export class QueryParamGroup {
3641

37-
private _valueChanges = new Subject<any>();
42+
private _valueChanges = new Subject<QueryParamGroupValue>();
3843

3944
/** TODO Documentation */
40-
public readonly valueChanges: Observable<any> = this._valueChanges.asObservable();
45+
public readonly valueChanges: Observable<QueryParamGroupValue> = this._valueChanges.asObservable();
4146

4247
/** TODO Documentation */
4348
public readonly controls: { [ controlName: string ]: QueryParamControl<any> };
@@ -53,13 +58,34 @@ export class QueryParamGroup {
5358
}
5459

5560
/** TODO Documentation */
56-
public get value(): any {
57-
const value: any = {};
61+
public get value(): QueryParamGroupValue {
62+
const value: QueryParamGroupValue = {};
5863
Object.keys(this.controls).forEach(controlName => value[ controlName ] = this.controls[ controlName ].value);
5964

6065
return value;
6166
}
6267

68+
/**
69+
* TODO Documentation
70+
*/
71+
public patchValue(value: QueryParamGroupValue): void {
72+
Object.keys(value).forEach(controlName => {
73+
const control = this.controls[ controlName ];
74+
if (isMissing(control)) {
75+
return;
76+
}
77+
78+
control.setValue(value[ controlName ]);
79+
});
80+
}
81+
82+
/**
83+
* TODO Documentation
84+
*/
85+
public setValue(value: QueryParamGroupValue): void {
86+
Object.keys(this.controls).forEach(controlName => this.controls[ controlName ].setValue(value[ controlName ]));
87+
}
88+
6389
/**
6490
* TODO Documentation
6591
*/

projects/ngqp/core/src/lib/query-param-group.directive.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Directive, Input, OnDestroy, OnInit } from '@angular/core';
22
import { ActivatedRoute, Params, Router } from '@angular/router';
33
import { Subject } from 'rxjs';
4-
import { concatMap, debounceTime, takeUntil, tap } from 'rxjs/operators';
4+
import { bufferTime, concatMap, debounceTime, map, takeUntil, tap } from 'rxjs/operators';
55
import { QueryParamNameDirective } from './query-param-name.directive';
66
import { QueryParamControl, QueryParamGroup } from './model';
77
import { isMissing } from './util';
@@ -101,6 +101,12 @@ export class QueryParamGroupDirective implements OnInit, OnDestroy {
101101
private setupNavigationQueue() {
102102
this.queue$.pipe(
103103
takeUntil(this.destroy$),
104+
// This buffers multiple synchronous events together to only execute a single navigation
105+
bufferTime(0),
106+
map(paramsList => paramsList.reduce((a: Params, b: Params): Params => {
107+
return { ...a, ...b };
108+
}, {})),
109+
104110
concatMap(params => this.router.navigate([], {
105111
relativeTo: this.route,
106112
queryParamsHandling: 'merge',

0 commit comments

Comments
 (0)