Skip to content

Commit 6a889f3

Browse files
crisbetojelbourn
authored andcommitted
refactor(text-field): allow ElementRef to be passed to autofill monitor (#12783)
Similarly to the other observers, allows for an `ElementRef` to be passed to the `AutofillMonitor` which is more convenient since most of the time we deal with `ElementRef`-s anyway.
1 parent a4a79ea commit 6a889f3

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

src/cdk/text-field/autofill.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@ describe('cdkAutofill', () => {
198198
}));
199199

200200
it('should monitor host element on init', () => {
201-
expect(autofillMonitor.monitor).toHaveBeenCalledWith(testComponent.input.nativeElement);
201+
expect(autofillMonitor.monitor).toHaveBeenCalledWith(testComponent.input);
202202
});
203203

204204
it('should stop monitoring host element on destroy', () => {
205205
expect(autofillMonitor.stopMonitoring).not.toHaveBeenCalled();
206206
fixture.destroy();
207-
expect(autofillMonitor.stopMonitoring).toHaveBeenCalledWith(testComponent.input.nativeElement);
207+
expect(autofillMonitor.stopMonitoring).toHaveBeenCalledWith(testComponent.input);
208208
});
209209
});
210210

src/cdk/text-field/autofill.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,21 @@ export class AutofillMonitor implements OnDestroy {
5656
* @param element The element to monitor.
5757
* @return A stream of autofill state changes.
5858
*/
59-
monitor(element: Element): Observable<AutofillEvent> {
59+
monitor(element: Element): Observable<AutofillEvent>;
60+
61+
/**
62+
* Monitor for changes in the autofill state of the given input element.
63+
* @param element The element to monitor.
64+
* @return A stream of autofill state changes.
65+
*/
66+
monitor(element: ElementRef<Element>): Observable<AutofillEvent>;
67+
68+
monitor(elementOrRef: Element | ElementRef<Element>): Observable<AutofillEvent> {
6069
if (!this._platform.isBrowser) {
6170
return EMPTY;
6271
}
6372

73+
const element = elementOrRef instanceof ElementRef ? elementOrRef.nativeElement : elementOrRef;
6474
const info = this._monitoredElements.get(element);
6575

6676
if (info) {
@@ -103,7 +113,16 @@ export class AutofillMonitor implements OnDestroy {
103113
* Stop monitoring the autofill state of the given input element.
104114
* @param element The element to stop monitoring.
105115
*/
106-
stopMonitoring(element: Element) {
116+
stopMonitoring(element: Element);
117+
118+
/**
119+
* Stop monitoring the autofill state of the given input element.
120+
* @param element The element to stop monitoring.
121+
*/
122+
stopMonitoring(element: ElementRef<Element>);
123+
124+
stopMonitoring(elementOrRef: Element | ElementRef<Element>) {
125+
const element = elementOrRef instanceof ElementRef ? elementOrRef.nativeElement : elementOrRef;
107126
const info = this._monitoredElements.get(element);
108127

109128
if (info) {
@@ -133,11 +152,11 @@ export class CdkAutofill implements OnDestroy, OnInit {
133152

134153
ngOnInit() {
135154
this._autofillMonitor
136-
.monitor(this._elementRef.nativeElement)
155+
.monitor(this._elementRef)
137156
.subscribe(event => this.cdkAutofill.emit(event));
138157
}
139158

140159
ngOnDestroy() {
141-
this._autofillMonitor.stopMonitoring(this._elementRef.nativeElement);
160+
this._autofillMonitor.stopMonitoring(this._elementRef);
142161
}
143162
}

src/lib/input/input.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
254254
}
255255

256256
ngOnInit() {
257-
this._autofillMonitor.monitor(this._elementRef.nativeElement).subscribe(event => {
257+
this._autofillMonitor.monitor(this._elementRef).subscribe(event => {
258258
this.autofilled = event.isAutofilled;
259259
this.stateChanges.next();
260260
});
@@ -266,7 +266,7 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
266266

267267
ngOnDestroy() {
268268
this.stateChanges.complete();
269-
this._autofillMonitor.stopMonitoring(this._elementRef.nativeElement);
269+
this._autofillMonitor.stopMonitoring(this._elementRef);
270270
}
271271

272272
ngDoCheck() {

src/material-examples/text-field-autofill-monitor/text-field-autofill-monitor-example.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ export class TextFieldAutofillMonitorExample implements OnDestroy, OnInit {
1616
constructor(private autofill: AutofillMonitor) {}
1717

1818
ngOnInit() {
19-
this.autofill.monitor(this.firstName.nativeElement)
19+
this.autofill.monitor(this.firstName)
2020
.subscribe(e => this.firstNameAutofilled = e.isAutofilled);
21-
this.autofill.monitor(this.lastName.nativeElement)
21+
this.autofill.monitor(this.lastName)
2222
.subscribe(e => this.lastNameAutofilled = e.isAutofilled);
2323
}
2424

2525
ngOnDestroy() {
26-
this.autofill.stopMonitoring(this.firstName.nativeElement);
27-
this.autofill.stopMonitoring(this.lastName.nativeElement);
26+
this.autofill.stopMonitoring(this.firstName);
27+
this.autofill.stopMonitoring(this.lastName);
2828
}
2929
}

0 commit comments

Comments
 (0)