Skip to content

Commit e2330b8

Browse files
committed
feat(list): allow avatar in selection list options
1 parent 877eb85 commit e2330b8

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

src/demo-app/list/list-demo.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ <h3 mat-subheader>Groceries</h3>
119119
<mat-list-option value="strawberries">Strawberries</mat-list-option>
120120
</mat-selection-list>
121121

122+
<mat-selection-list>
123+
<h3 mat-subheader>Dogs</h3>
124+
125+
<mat-list-option checkboxPosition="before">
126+
<img matListAvatar src="https://material.angular.io/assets/img/examples/shiba1.jpg">
127+
<span matLine>Shiba Inu</span>
128+
</mat-list-option>
129+
130+
<mat-list-option checkboxPosition="after">
131+
<img matListAvatar src="https://material.angular.io/assets/img/examples/shiba2.jpg">
132+
<span matLine>Other Shiba Inu</span>
133+
</mat-list-option>
134+
</mat-selection-list>
135+
122136
<p>Selected: {{selectedOptions | json}}</p>
123137
<p>Change Event Count {{changeEventCount}}</p>
124138
<p>Model Change Event Count {{modelChangeEventCount}}</p>

src/lib/list/list-option.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212

1313
<div class="mat-list-text" #text><ng-content></ng-content></div>
1414

15+
<ng-content select="[mat-list-avatar], [mat-list-icon], [matListAvatar], [matListIcon]">
16+
</ng-content>
17+
1518
</div>

src/lib/list/list.scss

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,31 @@ $mat-list-item-inset-divider-offset: 72px;
127127
}
128128
}
129129

130+
// If avatar is the 4th item, it means the list-item has both checkbox and avatar-icon.
131+
// This means that there should be a margin for the mat-list-avatar on the start-side.
132+
.mat-list-item-content .mat-list-avatar:nth-child(4) {
133+
margin-right: 0;
134+
margin-left: $mat-list-side-padding;
135+
136+
[dir='rtl'] & {
137+
margin-right: $mat-list-side-padding;
138+
margin-left: 0;
139+
}
140+
}
141+
142+
// Reversed content is mainly used by the MatSelectionList for displaying the checkbox at the
143+
// end of the list option. If avatar is the 4th item, it means the list-item has both checkbox
144+
// and avatar-icon. There needs to be a padding for the mat-list-avatar on the end-side.
145+
.mat-list-item-content-reverse .mat-list-avatar:nth-child(4) {
146+
margin-left: 0;
147+
margin-right: $mat-list-side-padding;
148+
149+
[dir='rtl'] & {
150+
margin-right: 0;
151+
margin-left: $mat-list-side-padding;
152+
}
153+
}
154+
130155
.mat-list-avatar {
131156
flex-shrink: 0;
132157
width: $avatar-size;

src/lib/list/selection-list.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
ChangeDetectionStrategy,
1717
ChangeDetectorRef,
1818
Component,
19+
ContentChild,
1920
ContentChildren,
2021
ElementRef,
2122
EventEmitter,
@@ -39,6 +40,7 @@ import {
3940
} from '@angular/material/core';
4041
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
4142
import {Subscription} from 'rxjs/Subscription';
43+
import {MatListAvatarCssMatStyler} from './list';
4244

4345

4446
/** @docs-private */
@@ -97,6 +99,7 @@ export class MatSelectionListChange {
9799
'tabindex': '-1',
98100
'[class.mat-list-item-disabled]': 'disabled',
99101
'[class.mat-list-item-focus]': '_hasFocus',
102+
'[class.mat-list-item-with-avatar]': '_avatar',
100103
'[attr.aria-selected]': 'selected.toString()',
101104
'[attr.aria-disabled]': 'disabled.toString()',
102105
},
@@ -113,6 +116,7 @@ export class MatListOption extends _MatListOptionMixinBase
113116
/** Whether the option has focus. */
114117
_hasFocus: boolean = false;
115118

119+
@ContentChild(MatListAvatarCssMatStyler) _avatar: MatListAvatarCssMatStyler;
116120
@ContentChildren(MatLine) _lines: QueryList<MatLine>;
117121

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

0 commit comments

Comments
 (0)