Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 14d0d8e

Browse files
authored
feat(list): Add focusItemAtIndex (#1558)
Sets focus (not selection) to the element at the given index in a list. - Adds `focusItemAtIndex(index: number)` - Update documentation - Include unit test Closes #1557
1 parent f8f8453 commit 14d0d8e

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

demos/components/list-demo/list-demo.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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>

packages/list/list.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff 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
}

test/list/list.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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);

0 commit comments

Comments
 (0)