Skip to content

Commit 5e5a8ce

Browse files
committed
Address feedback
1 parent b52a405 commit 5e5a8ce

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

src/lib/list/selection-list.spec.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {async, TestBed, ComponentFixture, inject} from '@angular/core/testing';
22
import {Component, DebugElement} from '@angular/core';
33
import {By} from '@angular/platform-browser';
44
import {MdSelectionList, MdListOption, MdListModule} from './index';
5-
import {createKeyboardEvent} from '@angular/cdk/testing';
5+
import {createKeyboardEvent, dispatchFakeEvent} from '@angular/cdk/testing';
66
import {UP_ARROW, DOWN_ARROW, SPACE} from '@angular/material/core';
77
import {Platform} from '@angular/material/core';
88

@@ -40,11 +40,11 @@ describe('MdSelectionList', () => {
4040
it('should add and remove focus class on focus/blur', () => {
4141
expect(listItemEl.nativeElement.classList).not.toContain('mat-list-item-focus');
4242

43-
listOption[0].componentInstance._handleFocus();
43+
dispatchFakeEvent(listOption[0].nativeElement, 'focus');
4444
fixture.detectChanges();
4545
expect(listItemEl.nativeElement.className).toContain('mat-list-item-focus');
4646

47-
listOption[0].componentInstance._handleBlur();
47+
dispatchFakeEvent(listOption[0].nativeElement, 'blur');
4848
fixture.detectChanges();
4949
expect(listItemEl.nativeElement.className).not.toContain('mat-list-item-focus');
5050
});
@@ -140,12 +140,9 @@ describe('MdSelectionList', () => {
140140
let SPACE_EVENT: KeyboardEvent =
141141
createKeyboardEvent('keydown', SPACE, testListItem);
142142
let selectList = selectionList.injector.get<MdSelectionList>(MdSelectionList).selectedOptions;
143-
let options = selectionList.componentInstance.options;
144-
let array = options.toArray();
145-
let focusItem = array[1];
146143
expect(selectList.selected.length).toBe(0);
147144

148-
focusItem.focus();
145+
dispatchFakeEvent(listOption[0].nativeElement, 'focus');
149146
selectionList.componentInstance._keydown(SPACE_EVENT);
150147

151148
fixture.detectChanges();
@@ -159,25 +156,20 @@ describe('MdSelectionList', () => {
159156
listOption[3].componentInstance._handleFocus();
160157

161158
expect(manager.activeItemIndex).toBe(3);
162-
expect(listOption[3].componentInstance._hasFocus).toBe(true);
163159

164160
fixture.componentInstance.showLastOption = false;
165161
fixture.detectChanges();
166162

167163
expect(manager.activeItemIndex).toBe(2);
168-
expect(listOption[2].componentInstance._hasFocus).toBe(true);
169164
});
170165

171166
it('should focus previous item when press UP ARROW', () => {
172167
let testListItem = listOption[2].nativeElement as HTMLElement;
173168
let UP_EVENT: KeyboardEvent =
174169
createKeyboardEvent('keydown', UP_ARROW, testListItem);
175-
let options = selectionList.componentInstance.options;
176-
let array = options.toArray();
177-
let focusItem = array[2];
178170
let manager = selectionList.componentInstance._keyManager;
179171

180-
focusItem.focus();
172+
dispatchFakeEvent(listOption[2].nativeElement, 'focus');
181173
expect(manager.activeItemIndex).toEqual(2);
182174

183175
selectionList.componentInstance._keydown(UP_EVENT);
@@ -191,12 +183,9 @@ describe('MdSelectionList', () => {
191183
let testListItem = listOption[2].nativeElement as HTMLElement;
192184
let DOWN_EVENT: KeyboardEvent =
193185
createKeyboardEvent('keydown', DOWN_ARROW, testListItem);
194-
let options = selectionList.componentInstance.options;
195-
let array = options.toArray();
196-
let focusItem = array[2];
197186
let manager = selectionList.componentInstance._keyManager;
198187

199-
focusItem.focus();
188+
dispatchFakeEvent(listOption[2].nativeElement, 'focus');
200189
expect(manager.activeItemIndex).toEqual(2);
201190

202191
selectionList.componentInstance._keydown(DOWN_EVENT);

src/lib/list/selection-list.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export interface MdSelectionListOptionEvent {
6565
'role': 'option',
6666
'class': 'mat-list-item mat-list-option',
6767
'(focus)': '_handleFocus()',
68-
'(blur)': '_handleBlur()',
68+
'(blur)': '_hasFocus = false',
6969
'(click)': '_handleClick()',
7070
'tabindex': '-1',
7171
'[class.mat-list-item-disabled]': 'disabled',
@@ -111,7 +111,7 @@ export class MdListOption extends _MdListOptionMixinBase
111111

112112
/** Emitted when the option is deselected. */
113113
@Output() deselected = new EventEmitter<MdSelectionListOptionEvent>();
114-
114+
115115
constructor(private _renderer: Renderer2,
116116
private _element: ElementRef,
117117
private _changeDetector: ChangeDetectorRef,
@@ -160,10 +160,6 @@ export class MdListOption extends _MdListOptionMixinBase
160160
this.selectionList._setFocusedOption(this);
161161
}
162162

163-
_handleBlur() {
164-
this._hasFocus = false;
165-
}
166-
167163
/** Retrieves the DOM element of the component host. */
168164
_getHostElement(): HTMLElement {
169165
return this._element.nativeElement;
@@ -242,18 +238,18 @@ export class MdSelectionList extends _MdSelectionListMixinBase
242238

243239
/** Sets the focused option of the selection-list. */
244240
_setFocusedOption(option: MdListOption) {
245-
this._keyManager.updateActiveItemIndex(this._getIndexFromOption(option));
241+
this._keyManager.updateActiveItemIndex(this._getOptionIndex(option));
246242
}
247243

248244
/** Removes an option from the selection list and updates the active item. */
249245
_removeOptionFromList(option: MdListOption) {
250246
if (option._hasFocus) {
251-
const optionIndex = this._getIndexFromOption(option);
247+
const optionIndex = this._getOptionIndex(option);
252248

253249
// Check whether the option is the last item
254-
if (optionIndex - 1 >= 0) {
250+
if (optionIndex > 0) {
255251
this._keyManager.setPreviousItemActive();
256-
} else if (optionIndex < this.options.length - 1) {
252+
} else if (optionIndex === 0 && this.options.length > 1) {
257253
this._keyManager.setNextItemActive();
258254
}
259255
}
@@ -296,7 +292,7 @@ export class MdSelectionList extends _MdSelectionListMixinBase
296292
}
297293

298294
/** Returns the index of the specified list option. */
299-
private _getIndexFromOption(option: MdListOption): number {
295+
private _getOptionIndex(option: MdListOption): number {
300296
return this.options.toArray().indexOf(option);
301297
}
302298
}

0 commit comments

Comments
 (0)