Skip to content

Commit 9d20a34

Browse files
crisbetommalerba
authored andcommitted
refactor: switch over focus method calls to use the renderer (#2077)
Switches the calls to `focus` directly on the DOM node to use the Renderer instead. This should make it easier to start supporting Angular Universal, if we decide to at some point.
1 parent 8c5a5e1 commit 9d20a34

File tree

7 files changed

+16
-11
lines changed

7 files changed

+16
-11
lines changed

src/lib/button-toggle/button-toggle.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ContentChildren,
66
Directive,
77
ElementRef,
8+
Renderer,
89
EventEmitter,
910
HostBinding,
1011
Input,
@@ -306,7 +307,8 @@ export class MdButtonToggle implements OnInit {
306307

307308
constructor(@Optional() toggleGroup: MdButtonToggleGroup,
308309
@Optional() toggleGroupMultiple: MdButtonToggleGroupMultiple,
309-
public buttonToggleDispatcher: MdUniqueSelectionDispatcher) {
310+
public buttonToggleDispatcher: MdUniqueSelectionDispatcher,
311+
private _renderer: Renderer) {
310312
this.buttonToggleGroup = toggleGroup;
311313

312314
this.buttonToggleGroupMultiple = toggleGroupMultiple;
@@ -435,7 +437,7 @@ export class MdButtonToggle implements OnInit {
435437
}
436438

437439
focus() {
438-
this._inputElement.nativeElement.focus();
440+
this._renderer.invokeElementMethod(this._inputElement.nativeElement, 'focus');
439441
}
440442
}
441443

src/lib/button/button.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class MdButton {
9696

9797
/** TODO(hansl): e2e test this function. */
9898
focus() {
99-
this._elementRef.nativeElement.focus();
99+
this._renderer.invokeElementMethod(this._elementRef.nativeElement, 'focus');
100100
}
101101

102102
getHostElement() {

src/lib/checkbox/checkbox.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export class MdCheckbox implements ControlValueAccessor {
331331
}
332332

333333
focus() {
334-
this._inputElement.nativeElement.focus();
334+
this._renderer.invokeElementMethod(this._inputElement.nativeElement, 'focus');
335335
this._onInputFocus();
336336
}
337337

src/lib/input/input.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ContentChildren,
1111
ViewChild,
1212
ElementRef,
13+
Renderer,
1314
QueryList,
1415
OnChanges,
1516
EventEmitter,
@@ -237,7 +238,7 @@ export class MdInput implements ControlValueAccessor, AfterContentInit, OnChange
237238

238239
_elementType: 'input' | 'textarea';
239240

240-
constructor(elementRef: ElementRef) {
241+
constructor(elementRef: ElementRef, private _renderer: Renderer) {
241242
// Set the element type depending on normalized selector used(md-input / md-textarea)
242243
this._elementType = elementRef.nativeElement.nodeName.toLowerCase() === 'md-input' ?
243244
'input' :
@@ -246,7 +247,7 @@ export class MdInput implements ControlValueAccessor, AfterContentInit, OnChange
246247

247248
/** Set focus on input */
248249
focus() {
249-
this._inputElement.nativeElement.focus();
250+
this._renderer.invokeElementMethod(this._inputElement.nativeElement, 'focus');
250251
}
251252

252253
_handleFocus(event: FocusEvent) {

src/lib/radio/radio.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ContentChildren,
55
Directive,
66
ElementRef,
7+
Renderer,
78
EventEmitter,
89
HostBinding,
910
Input,
@@ -294,6 +295,7 @@ export class MdRadioButton implements OnInit {
294295

295296
constructor(@Optional() radioGroup: MdRadioGroup,
296297
private _elementRef: ElementRef,
298+
private _renderer: Renderer,
297299
public radioDispatcher: MdUniqueSelectionDispatcher) {
298300
// Assertions. Ideally these should be stripped out by the compiler.
299301
// TODO(jelbourn): Assert that there's no name binding AND a parent radio group.
@@ -412,7 +414,7 @@ export class MdRadioButton implements OnInit {
412414
}
413415

414416
focus() {
415-
this._inputElement.nativeElement.focus();
417+
this._renderer.invokeElementMethod(this._inputElement.nativeElement, 'focus');
416418
this._onInputFocus();
417419
}
418420

src/lib/slide-toggle/slide-toggle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor {
190190
}
191191

192192
focus() {
193-
this._inputElement.nativeElement.focus();
193+
this._renderer.invokeElementMethod(this._inputElement.nativeElement, 'focus');
194194
this._onInputFocus();
195195
}
196196

src/lib/tabs/tab-label-wrapper.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import {Directive, ElementRef} from '@angular/core';
1+
import {Directive, ElementRef, Renderer} from '@angular/core';
22

33

44
/** Used in the `md-tab-group` view to display tab labels */
55
@Directive({
66
selector: '[md-tab-label-wrapper], [mat-tab-label-wrapper]'
77
})
88
export class MdTabLabelWrapper {
9-
constructor(public elementRef: ElementRef) {}
9+
constructor(public elementRef: ElementRef, private _renderer: Renderer) {}
1010

1111
/**
1212
* Sets focus on the wrapper element
1313
*/
1414
focus(): void {
15-
this.elementRef.nativeElement.focus();
15+
this._renderer.invokeElementMethod(this.elementRef.nativeElement, 'focus');
1616
}
1717
}

0 commit comments

Comments
 (0)