Skip to content

Commit 7bc8670

Browse files
crisbetojelbourn
authored andcommitted
fix(selection-list): proper styling not being applied when using mat-list-icon (#12879)
Fixes the correct class not being applied to the `mat-list-option`, if it's using `mat-list-icon` which leads to the spacing not being correct. For reference: https://stackblitz.com/edit/angular-9tnpjv.
1 parent 548d2b7 commit 7bc8670

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,34 @@ describe('MatSelectionList without forms', () => {
689689
expect(listItemContent.nativeElement.classList).toContain('mat-list-item-content-reverse');
690690
});
691691
});
692+
693+
describe('with list item elements', () => {
694+
beforeEach(async(() => {
695+
TestBed.configureTestingModule({
696+
imports: [MatListModule],
697+
declarations: [
698+
SelectionListWithAvatar,
699+
SelectionListWithIcon,
700+
],
701+
}).compileComponents();
702+
}));
703+
704+
it('should add a class to reflect that it has an avatar', () => {
705+
const fixture = TestBed.createComponent(SelectionListWithIcon);
706+
fixture.detectChanges();
707+
708+
const listOption = fixture.nativeElement.querySelector('.mat-list-option');
709+
expect(listOption.classList).toContain('mat-list-item-with-avatar');
710+
});
711+
712+
it('should add a class to reflect that it has an icon', () => {
713+
const fixture = TestBed.createComponent(SelectionListWithIcon);
714+
fixture.detectChanges();
715+
716+
const listOption = fixture.nativeElement.querySelector('.mat-list-option');
717+
expect(listOption.classList).toContain('mat-list-item-with-avatar');
718+
});
719+
});
692720
});
693721

694722
describe('MatSelectionList with forms', () => {
@@ -1178,3 +1206,30 @@ class SelectionListWithCustomComparator {
11781206
{id: 3, label: 'Three'}
11791207
];
11801208
}
1209+
1210+
1211+
@Component({
1212+
template: `
1213+
<mat-selection-list>
1214+
<mat-list-option>
1215+
<div mat-list-avatar>I</div>
1216+
Inbox
1217+
</mat-list-option>
1218+
</mat-selection-list>
1219+
`
1220+
})
1221+
class SelectionListWithAvatar {
1222+
}
1223+
1224+
@Component({
1225+
template: `
1226+
<mat-selection-list>
1227+
<mat-list-option>
1228+
<div mat-list-icon>I</div>
1229+
Inbox
1230+
</mat-list-option>
1231+
</mat-selection-list>
1232+
`
1233+
})
1234+
class SelectionListWithIcon {
1235+
}

src/lib/list/selection-list.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
} from '@angular/material/core';
3939
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
4040
import {Subscription} from 'rxjs';
41-
import {MatListAvatarCssMatStyler} from './list';
41+
import {MatListAvatarCssMatStyler, MatListIconCssMatStyler} from './list';
4242

4343

4444
/** @docs-private */
@@ -86,7 +86,7 @@ export class MatSelectionListChange {
8686
'tabindex': '-1',
8787
'[class.mat-list-item-disabled]': 'disabled',
8888
'[class.mat-list-item-focus]': '_hasFocus',
89-
'[class.mat-list-item-with-avatar]': '_avatar',
89+
'[class.mat-list-item-with-avatar]': '_avatar || _icon',
9090
'[attr.aria-selected]': 'selected.toString()',
9191
'[attr.aria-disabled]': 'disabled.toString()',
9292
},
@@ -104,6 +104,7 @@ export class MatListOption extends _MatListOptionMixinBase
104104
_hasFocus: boolean = false;
105105

106106
@ContentChild(MatListAvatarCssMatStyler) _avatar: MatListAvatarCssMatStyler;
107+
@ContentChild(MatListIconCssMatStyler) _icon: MatListIconCssMatStyler;
107108
@ContentChildren(MatLine) _lines: QueryList<MatLine>;
108109

109110
/** DOM element containing the item's text. */

0 commit comments

Comments
 (0)