Skip to content

Commit 12acf75

Browse files
Fix for reactive forms, custom input debounce
1 parent 146d93d commit 12acf75

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

source/connect/connect-base.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import 'rxjs/add/operator/debounceTime';
1717
import { FormStore } from '../form-store';
1818
import { State } from '../state';
1919

20+
import { Iterable } from 'immutable';
21+
2022
export interface ControlPair {
2123
path: Array<string>;
2224
control: AbstractControl;
@@ -25,11 +27,15 @@ export interface ControlPair {
2527
export class ConnectBase {
2628

2729
@Input('connect') connect: () => (string | number) | Array<string | number>;
30+
@Input('debounce') debounce: number;
2831
private stateSubscription: Unsubscribe;
2932

3033
private formSubscription: Subscription;
3134
protected store: FormStore;
3235
protected form: any;
36+
protected get changeDebounce(): number {
37+
return 'number' === typeof this.debounce || ('string' === typeof this.debounce && String(this.debounce).match(/^[0-9]+(\.[0-9]+)?$/)) ? Number(this.debounce) : 0;
38+
}
3339

3440
public get path(): Array<string> {
3541
const path = typeof this.connect === 'function'
@@ -63,13 +69,13 @@ export class ConnectBase {
6369

6470
ngAfterContentInit() {
6571
Promise.resolve().then(() => {
66-
this.resetState();
72+
this.resetState(false);
6773

68-
this.stateSubscription = this.store.subscribe(() => this.resetState());
74+
this.stateSubscription = this.store.subscribe(() => this.resetState(true));
6975

7076
Promise.resolve().then(() => {
7177
this.formSubscription = (<any>this.form.valueChanges)
72-
.debounceTime(0)
78+
.debounceTime(this.changeDebounce)
7379
.subscribe((values: any) => this.publish(values));
7480
});
7581
});
@@ -97,11 +103,12 @@ export class ConnectBase {
97103
throw new Error(`Unknown type of form element: ${formElement.constructor.name}`);
98104
}
99105

100-
return pairs.filter(p => (<any>p.control)._parent === this.form.control);
106+
return pairs.filter(p => (<any>p.control)._parent === this.form.control || (<any>p.control)._parent === this.form);
101107
}
102108

103-
private resetState() {
109+
private resetState(emitEvent: boolean) {
104110
var formElement;
111+
105112
if (this.form.control === undefined) {
106113
formElement = this.form;
107114
}
@@ -114,12 +121,12 @@ export class ConnectBase {
114121
children.forEach(c => {
115122
const { path, control } = c;
116123

117-
const value = State.get(this.getState(), this.path.concat(c.path));
124+
const value = State.get(this.getState(), this.path.concat(path));
118125

119126
if (control.value !== value) {
120127
const phonyControl = <any>{ path: path };
121128

122-
this.form.updateModel(phonyControl, value);
129+
control.setValue(value, {emitEvent});
123130
}
124131
});
125132
}

0 commit comments

Comments
 (0)