Skip to content

Commit 34b210c

Browse files
robertmesserlejelbourn
authored andcommitted
fix(list): adds focus state for nav-list items (#502)
closes #323 Note: More discussion needed on list items with multiple actions.
1 parent dc45b79 commit 34b210c

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

src/components/list/list-item.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<div class="md-list-item">
1+
<div class="md-list-item" [class.md-list-item-focus]="hasFocus">
22
<ng-content select="[md-list-avatar],[md-list-icon]"></ng-content>
33
<div class="md-list-text"><ng-content select="[md-line]"></ng-content></div>
44
<ng-content></ng-content>
5-
</div>
5+
</div>

src/components/list/list.scss

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ $md-dense-avatar-height: 48px;
2525
$md-dense-two-line-height: 60px;
2626
$md-dense-three-line-height: 76px;
2727

28-
/*
28+
/*
2929
This mixin provides all list-item styles, changing font size and height
3030
based on whether the list is in dense mode.
3131
*/
32-
@mixin md-list-item-base($font-size, $base-height, $avatar-height,
32+
@mixin md-list-item-base($font-size, $base-height, $avatar-height,
3333
$two-line-height, $three-line-height) {
3434

3535
.md-list-item {
@@ -94,7 +94,7 @@ based on whether the list is in dense mode.
9494
}
9595

9696
/*
97-
This mixin provides all md-line styles, changing secondary font size
97+
This mixin provides all md-line styles, changing secondary font size
9898
based on whether the list is in dense mode.
9999
*/
100100
@mixin md-line-base($secondary-font-size) {
@@ -201,8 +201,8 @@ md-nav-list {
201201
.md-list-item {
202202
cursor: pointer;
203203

204-
&:hover {
204+
&:hover, &.md-list-item-focus {
205205
background: md-color($md-background, 'hover');
206206
}
207207
}
208-
}
208+
}

src/components/list/list.spec.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {TestComponentBuilder} from '@angular/compiler/testing';
99
import {Component} from '@angular/core';
1010
import {By} from '@angular/platform-browser';
1111

12-
import {MD_LIST_DIRECTIVES} from './list';
12+
import {MD_LIST_DIRECTIVES, MdListItem} from './list';
1313

1414
describe('MdList', () => {
1515
let builder: TestComponentBuilder;
@@ -18,6 +18,31 @@ describe('MdList', () => {
1818
builder = tcb;
1919
}));
2020

21+
it('should add and remove focus class on focus/blur', () => {
22+
var template = `
23+
<md-list>
24+
<a md-list-item>
25+
Paprika
26+
</a>
27+
</md-list>
28+
`;
29+
return builder.overrideTemplate(TestList, template)
30+
.createAsync(TestList).then((fixture) => {
31+
let listItem = fixture.debugElement.query(By.directive(MdListItem));
32+
let listItemDiv = fixture.debugElement.query(By.css('.md-list-item'));
33+
fixture.detectChanges();
34+
expect(listItemDiv.nativeElement.classList).not.toContain('md-list-item-focus');
35+
36+
listItem.componentInstance.handleFocus();
37+
fixture.detectChanges();
38+
expect(listItemDiv.nativeElement.classList).toContain('md-list-item-focus');
39+
40+
listItem.componentInstance.handleBlur();
41+
fixture.detectChanges();
42+
expect(listItemDiv.nativeElement.classList).not.toContain('md-list-item-focus');
43+
});
44+
});
45+
2146
it('should not apply any class to a list without lines', (done: () => void) => {
2247
var template = `
2348
<md-list>

src/components/list/list.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,20 @@ export class MdListAvatar {}
3333
@Component({
3434
moduleId: module.id,
3535
selector: 'md-list-item, a[md-list-item]',
36-
host: {'role': 'listitem'},
36+
host: {
37+
'role': 'listitem',
38+
'(focus)': 'handleFocus()',
39+
'(blur)': 'handleBlur()',
40+
},
3741
templateUrl: 'list-item.html',
3842
encapsulation: ViewEncapsulation.None
3943
})
4044
export class MdListItem implements AfterContentInit {
4145
@ContentChildren(MdLine) _lines: QueryList<MdLine>;
4246

47+
/** @internal */
48+
hasFocus: boolean = false;
49+
4350
/** @internal */
4451
ngAfterContentInit() {
4552
this._setLineClass(this._lines.length);
@@ -56,6 +63,16 @@ export class MdListItem implements AfterContentInit {
5663

5764
constructor(private _renderer: Renderer, private _element: ElementRef) {}
5865

66+
/** @internal */
67+
handleFocus() {
68+
this.hasFocus = true;
69+
}
70+
71+
/** @internal */
72+
handleBlur() {
73+
this.hasFocus = false;
74+
}
75+
5976
private _setLineClass(count: number): void {
6077
this._resetClasses();
6178
if (count === 2 || count === 3) {

0 commit comments

Comments
 (0)