This repository was archived by the owner on Oct 7, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +17
-1
lines changed
demos/components/list-demo Expand file tree Collapse file tree 3 files changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -125,6 +125,10 @@ <h2>Design & API Documentation</h2>
125125 < td > getSelectedIndex(): number</ td >
126126 < td > Returns the currently selected item index.</ td >
127127 </ tr >
128+ < tr >
129+ < td > focusItemAtIndex(index: number)</ td >
130+ < td > Sets focus to the element at the given index in a list.</ td >
131+ </ tr >
128132 < tr >
129133 < td > focusFirstElement()</ td >
130134 < td > Sets focus to the first element in a list.</ td >
Original file line number Diff line number Diff line change @@ -199,7 +199,7 @@ export class MdcList implements AfterViewInit, OnDestroy {
199199 this . _listItems . toArray ( ) [ index ] . getListItemElement ( ) . classList . add ( className ) ,
200200 removeClassForElementIndex : ( index : number , className : string ) =>
201201 this . _listItems . toArray ( ) [ index ] . getListItemElement ( ) . classList . remove ( className ) ,
202- focusItemAtIndex : ( index : number ) => this . _listItems . toArray ( ) [ index ] . getListItemElement ( ) . focus ( ) ,
202+ focusItemAtIndex : ( index : number ) => this . focusItemAtIndex ( index ) ,
203203 setTabIndexForListItemChildren : ( listItemIndex : number , tabIndexValue : number ) => {
204204 const listItemChildren = [ ] . slice . call ( this . _listItems . toArray ( ) [ listItemIndex ] . getListItemElement ( )
205205 . querySelectorAll ( strings . CHILD_ELEMENTS_TO_TOGGLE_TABINDEX ) ) ;
@@ -347,6 +347,10 @@ export class MdcList implements AfterViewInit, OnDestroy {
347347 return this . _listItems . toArray ( ) . findIndex ( _ => _ . selected || _ . activated ) ;
348348 }
349349
350+ focusItemAtIndex ( index : number ) : void {
351+ this . _listItems . toArray ( ) [ index ] . getListItemElement ( ) . focus ( ) ;
352+ }
353+
350354 focusFirstElement ( ) : void {
351355 this . _foundation . focusFirstElement ( ) ;
352356 }
Original file line number Diff line number Diff line change @@ -148,6 +148,14 @@ describe('MdcListModule', () => {
148148 fixture . detectChanges ( ) ;
149149 } ) ;
150150
151+ it ( '#should set focus to list item index (0)' , ( ) => {
152+ const listItemDebugElement = fixture . debugElement . query ( By . directive ( MdcListItem ) ) ;
153+
154+ testInstance . focusItemAtIndex ( 0 ) ;
155+ fixture . detectChanges ( ) ;
156+ expect ( document . activeElement ) . toBe ( listItemDebugElement . nativeElement , 'Expected focus to be on list item' ) ;
157+ } ) ;
158+
151159 it ( '#should set focus to list item' , ( ) => {
152160 const listItemDebugElement = fixture . debugElement . query ( By . directive ( MdcListItem ) ) ;
153161 const listItemInstance = listItemDebugElement . injector . get < MdcListItem > ( MdcListItem ) ;
You can’t perform that action at this time.
0 commit comments