Skip to content

refactor(list): remove redundant focus logic #12965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/list/_list-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

.mat-list-option,
.mat-nav-list .mat-list-item {
&:hover, &.mat-list-item-focus {
&:hover, &:focus {
background: mat-color($background, 'hover');
}
}
Expand Down
17 changes: 0 additions & 17 deletions src/lib/list/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,6 @@ describe('MatList', () => {
TestBed.compileComponents();
}));

it('should add and remove focus class on focus/blur', () => {
let fixture = TestBed.createComponent(ListWithOneAnchorItem);
fixture.detectChanges();
let listItem = fixture.debugElement.query(By.directive(MatListItem));
let listItemEl = fixture.debugElement.query(By.css('.mat-list-item'));

expect(listItemEl.nativeElement.classList).not.toContain('mat-list-item-focus');

listItem.componentInstance._handleFocus();
fixture.detectChanges();
expect(listItemEl.nativeElement.classList).toContain('mat-list-item-focus');

listItem.componentInstance._handleBlur();
fixture.detectChanges();
expect(listItemEl.nativeElement.classList).not.toContain('mat-list-item-focus');
});

it('should not apply any additional class to a list without lines', () => {
let fixture = TestBed.createComponent(ListWithOneItem);
let listItem = fixture.debugElement.query(By.css('mat-list-item'));
Expand Down
10 changes: 0 additions & 10 deletions src/lib/list/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ export class MatListSubheaderCssMatStyler {}
// @breaking-change 7.0.0 Remove `mat-list-item-avatar` in favor of `mat-list-item-with-avatar`.
'[class.mat-list-item-avatar]': '_avatar || _icon',
'[class.mat-list-item-with-avatar]': '_avatar || _icon',
'(focus)': '_handleFocus()',
'(blur)': '_handleBlur()',
},
inputs: ['disableRipple'],
templateUrl: 'list-item.html',
Expand Down Expand Up @@ -140,14 +138,6 @@ export class MatListItem extends _MatListItemMixinBase implements AfterContentIn
return !this._isNavList || this.disableRipple || this._navList.disableRipple;
}

_handleFocus() {
this._element.nativeElement.classList.add('mat-list-item-focus');
}

_handleBlur() {
this._element.nativeElement.classList.remove('mat-list-item-focus');
}

/** Retrieves the DOM element of the component host. */
_getHostElement(): HTMLElement {
return this._element.nativeElement;
Expand Down
54 changes: 0 additions & 54 deletions src/lib/list/selection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,6 @@ describe('MatSelectionList without forms', () => {
selectionList = fixture.debugElement.query(By.directive(MatSelectionList));
}));

it('should add and remove focus class on focus/blur', () => {
// Use the second list item, because the first one is always disabled.
const listItem = listOptions[1].nativeElement;

expect(listItem.classList).not.toContain('mat-list-item-focus');

dispatchFakeEvent(listItem, 'focus');
fixture.detectChanges();
expect(listItem.className).toContain('mat-list-item-focus');

dispatchFakeEvent(listItem, 'blur');
fixture.detectChanges();
expect(listItem.className).not.toContain('mat-list-item-focus');
});

it('should be able to set a value on a list option', () => {
const optionValues = ['inbox', 'starred', 'sent-mail', 'drafts'];

Expand Down Expand Up @@ -519,45 +504,6 @@ describe('MatSelectionList without forms', () => {
});
});

describe('with single option', () => {
let fixture: ComponentFixture<SelectionListWithOnlyOneOption>;
let listOption: DebugElement;
let listItemEl: DebugElement;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MatListModule],
declarations: [
SelectionListWithListOptions,
SelectionListWithCheckboxPositionAfter,
SelectionListWithListDisabled,
SelectionListWithOnlyOneOption
],
});

TestBed.compileComponents();
}));

beforeEach(async(() => {
fixture = TestBed.createComponent(SelectionListWithOnlyOneOption);
listOption = fixture.debugElement.query(By.directive(MatListOption));
listItemEl = fixture.debugElement.query(By.css('.mat-list-item'));
fixture.detectChanges();
}));

it('should be focused when focus on nativeElements', () => {
dispatchFakeEvent(listOption.nativeElement, 'focus');
fixture.detectChanges();

expect(listItemEl.nativeElement.className).toContain('mat-list-item-focus');

dispatchFakeEvent(listOption.nativeElement, 'blur');
fixture.detectChanges();

expect(listItemEl.nativeElement.className).not.toContain('mat-list-item-focus');
});
});

describe('with option disabled', () => {
let fixture: ComponentFixture<SelectionListWithDisabledOption>;
let listOptionEl: HTMLElement;
Expand Down
10 changes: 2 additions & 8 deletions src/lib/list/selection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export class MatSelectionListChange {
'(click)': '_handleClick()',
'tabindex': '-1',
'[class.mat-list-item-disabled]': 'disabled',
'[class.mat-list-item-focus]': '_hasFocus',
'[class.mat-list-item-with-avatar]': '_avatar || _icon',
'[attr.aria-selected]': 'selected.toString()',
'[attr.aria-disabled]': 'disabled.toString()',
Expand All @@ -100,9 +99,6 @@ export class MatListOption extends _MatListOptionMixinBase
private _selected = false;
private _disabled = false;

/** Whether the option has focus. */
_hasFocus: boolean = false;

@ContentChild(MatListAvatarCssMatStyler) _avatar: MatListAvatarCssMatStyler;
@ContentChild(MatListIconCssMatStyler) _icon: MatListIconCssMatStyler;
@ContentChildren(MatLine) _lines: QueryList<MatLine>;
Expand Down Expand Up @@ -212,12 +208,10 @@ export class MatListOption extends _MatListOptionMixinBase
}

_handleFocus() {
this._hasFocus = true;
this.selectionList._setFocusedOption(this);
}

_handleBlur() {
this._hasFocus = false;
this.selectionList._onTouched();
}

Expand Down Expand Up @@ -392,9 +386,9 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu

/** Removes an option from the selection list and updates the active item. */
_removeOptionFromList(option: MatListOption) {
if (option._hasFocus) {
const optionIndex = this._getOptionIndex(option);
const optionIndex = this._getOptionIndex(option);

if (optionIndex > -1 && this._keyManager.activeItemIndex === optionIndex) {
// Check whether the option is the last item
if (optionIndex > 0) {
this._keyManager.setPreviousItemActive();
Expand Down