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>
125
125
< td > getSelectedIndex(): number</ td >
126
126
< td > Returns the currently selected item index.</ td >
127
127
</ 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 >
128
132
< tr >
129
133
< td > focusFirstElement()</ td >
130
134
< 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 {
199
199
this . _listItems . toArray ( ) [ index ] . getListItemElement ( ) . classList . add ( className ) ,
200
200
removeClassForElementIndex : ( index : number , className : string ) =>
201
201
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 ) ,
203
203
setTabIndexForListItemChildren : ( listItemIndex : number , tabIndexValue : number ) => {
204
204
const listItemChildren = [ ] . slice . call ( this . _listItems . toArray ( ) [ listItemIndex ] . getListItemElement ( )
205
205
. querySelectorAll ( strings . CHILD_ELEMENTS_TO_TOGGLE_TABINDEX ) ) ;
@@ -347,6 +347,10 @@ export class MdcList implements AfterViewInit, OnDestroy {
347
347
return this . _listItems . toArray ( ) . findIndex ( _ => _ . selected || _ . activated ) ;
348
348
}
349
349
350
+ focusItemAtIndex ( index : number ) : void {
351
+ this . _listItems . toArray ( ) [ index ] . getListItemElement ( ) . focus ( ) ;
352
+ }
353
+
350
354
focusFirstElement ( ) : void {
351
355
this . _foundation . focusFirstElement ( ) ;
352
356
}
Original file line number Diff line number Diff line change @@ -148,6 +148,14 @@ describe('MdcListModule', () => {
148
148
fixture . detectChanges ( ) ;
149
149
} ) ;
150
150
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
+
151
159
it ( '#should set focus to list item' , ( ) => {
152
160
const listItemDebugElement = fixture . debugElement . query ( By . directive ( MdcListItem ) ) ;
153
161
const listItemInstance = listItemDebugElement . injector . get < MdcListItem > ( MdcListItem ) ;
You can’t perform that action at this time.
0 commit comments