From 062296dc36f8986837e98c45cd6facdd3e864461 Mon Sep 17 00:00:00 2001 From: Yuan Gao Date: Fri, 4 Nov 2016 14:16:28 -0700 Subject: [PATCH] Add focus to checkbox, radio, button-toggle and slide-toggle --- .../slide-toggle/slide-toggle-demo.html | 2 +- src/lib/button-toggle/button-toggle.spec.ts | 10 ++++++++ src/lib/button-toggle/button-toggle.ts | 8 +++++++ src/lib/checkbox/checkbox.html | 3 ++- src/lib/checkbox/checkbox.spec.ts | 9 +++++++ src/lib/checkbox/checkbox.ts | 9 +++++++ src/lib/radio/radio.spec.ts | 24 +++++++++++++++++++ src/lib/radio/radio.ts | 9 +++++++ src/lib/slide-toggle/slide-toggle.spec.ts | 10 ++++++++ src/lib/slide-toggle/slide-toggle.ts | 8 +++++++ 10 files changed, 90 insertions(+), 2 deletions(-) diff --git a/src/demo-app/slide-toggle/slide-toggle-demo.html b/src/demo-app/slide-toggle/slide-toggle-demo.html index 0a1bcca960fc..03a1c71a03f0 100644 --- a/src/demo-app/slide-toggle/slide-toggle-demo.html +++ b/src/demo-app/slide-toggle/slide-toggle-demo.html @@ -26,4 +26,4 @@ - \ No newline at end of file + diff --git a/src/lib/button-toggle/button-toggle.spec.ts b/src/lib/button-toggle/button-toggle.spec.ts index 0841608e6a6a..5ea6b0faae32 100644 --- a/src/lib/button-toggle/button-toggle.spec.ts +++ b/src/lib/button-toggle/button-toggle.spec.ts @@ -500,6 +500,16 @@ describe('MdButtonToggle', () => { expect(changeSpy).toHaveBeenCalledTimes(2); })); + it('should focus on underlying input element when focus() is called', () => { + let nativeRadioInput = buttonToggleDebugElement.query(By.css('input')).nativeElement; + expect(document.activeElement).not.toBe(nativeRadioInput); + + buttonToggleInstance.focus(); + fixture.detectChanges(); + + expect(document.activeElement).toBe(nativeRadioInput); + }); + }); }); diff --git a/src/lib/button-toggle/button-toggle.ts b/src/lib/button-toggle/button-toggle.ts index 28d2f0b34d0d..f4db511ebe25 100644 --- a/src/lib/button-toggle/button-toggle.ts +++ b/src/lib/button-toggle/button-toggle.ts @@ -4,6 +4,7 @@ import { Component, ContentChildren, Directive, + ElementRef, EventEmitter, HostBinding, Input, @@ -11,6 +12,7 @@ import { Optional, Output, QueryList, + ViewChild, ViewEncapsulation, forwardRef, AfterViewInit @@ -266,6 +268,8 @@ export class MdButtonToggle implements OnInit { return this._change.asObservable(); } + @ViewChild('input') _inputElement: ElementRef; + constructor(@Optional() toggleGroup: MdButtonToggleGroup, @Optional() toggleGroupMultiple: MdButtonToggleGroupMultiple, public buttonToggleDispatcher: MdUniqueSelectionDispatcher) { @@ -395,6 +399,10 @@ export class MdButtonToggle implements OnInit { // Preventing bubbling for the second event will solve that issue. event.stopPropagation(); } + + focus() { + this._inputElement.nativeElement.focus(); + } } diff --git a/src/lib/checkbox/checkbox.html b/src/lib/checkbox/checkbox.html index c53bc700f0ae..fba084ce10a5 100644 --- a/src/lib/checkbox/checkbox.html +++ b/src/lib/checkbox/checkbox.html @@ -1,6 +1,7 @@