Skip to content

Commit 20cbdba

Browse files
tinayuangaommalerba
authored andcommitted
feat(list): allow avatar in selection list options (#10316)
1 parent c40fef4 commit 20cbdba

File tree

5 files changed

+50
-19
lines changed

5 files changed

+50
-19
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: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,36 @@ $mat-list-item-inset-divider-offset: 72px;
102102
padding: 0;
103103
}
104104

105-
// There can be a secondary item (e.g. avatar-icon, checkbox) at the start of the
106-
// list-item. This means that there should be a padding for the mat-list-text on the start-side.
107-
.mat-list-item-content .mat-list-text:not(:nth-child(2)) {
108-
padding-right: 0;
109-
padding-left: $mat-list-side-padding;
105+
&.mat-list-item-with-avatar,
106+
&.mat-list-option {
107+
.mat-list-item-content .mat-list-text {
108+
padding-right: 0;
109+
padding-left: $mat-list-side-padding;
110110

111-
[dir='rtl'] & {
112-
padding-right: $mat-list-side-padding;
113-
padding-left: 0;
111+
[dir='rtl'] & {
112+
padding-right: $mat-list-side-padding;
113+
padding-left: 0;
114+
}
114115
}
115-
}
116116

117-
// Reversed content is mainly used by the MatSelectionList for displaying the checkbox at the
118-
// end of the list option. Since there is a secondary item (checkbox) at the end of the
119-
// option, there needs to be a padding for the mat-list-text on the end-side.
120-
.mat-list-item-content-reverse .mat-list-text:not(:nth-child(2)) {
121-
padding-left: 0;
122-
padding-right: $mat-list-side-padding;
117+
// Reversed content is mainly used by the MatSelectionList for displaying the checkbox at the
118+
// end of the list option. Since there is a secondary item (checkbox) at the end of the
119+
// option, there needs to be a padding for the mat-list-text on the end-side.
120+
.mat-list-item-content-reverse .mat-list-text {
121+
padding-left: 0;
122+
padding-right: $mat-list-side-padding;
123123

124-
[dir='rtl'] & {
125-
padding-right: 0;
124+
[dir='rtl'] & {
125+
padding-right: 0;
126+
padding-left: $mat-list-side-padding;
127+
}
128+
}
129+
}
130+
131+
&.mat-list-item-with-avatar.mat-list-option {
132+
.mat-list-item-content-reverse .mat-list-text,
133+
.mat-list-item-content .mat-list-text {
134+
padding-right: $mat-list-side-padding;
126135
padding-left: $mat-list-side-padding;
127136
}
128137
}

src/lib/list/list.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ export class MatListSubheaderCssMatStyler {}
9797
host: {
9898
'class': 'mat-list-item',
9999
// @deletion-target 7.0.0 Remove `mat-list-item-avatar` in favor of `mat-list-item-with-avatar`.
100-
'[class.mat-list-item-avatar]': '_avatar',
101-
'[class.mat-list-item-with-avatar]': '_avatar',
100+
'[class.mat-list-item-avatar]': '_avatar || _icon',
101+
'[class.mat-list-item-with-avatar]': '_avatar || _icon',
102102
'(focus)': '_handleFocus()',
103103
'(blur)': '_handleBlur()',
104104
},
@@ -113,6 +113,7 @@ export class MatListItem extends _MatListItemMixinBase implements AfterContentIn
113113

114114
@ContentChildren(MatLine) _lines: QueryList<MatLine>;
115115
@ContentChild(MatListAvatarCssMatStyler) _avatar: MatListAvatarCssMatStyler;
116+
@ContentChild(MatListIconCssMatStyler) _icon: MatListIconCssMatStyler;
116117

117118
constructor(private _element: ElementRef,
118119
@Optional() private _navList: MatNavList) {

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,
@@ -37,6 +38,7 @@ import {
3738
} from '@angular/material/core';
3839
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
3940
import {Subscription} from 'rxjs';
41+
import {MatListAvatarCssMatStyler} from './list';
4042

4143

4244
/** @docs-private */
@@ -82,6 +84,7 @@ export class MatSelectionListChange {
8284
'tabindex': '-1',
8385
'[class.mat-list-item-disabled]': 'disabled',
8486
'[class.mat-list-item-focus]': '_hasFocus',
87+
'[class.mat-list-item-with-avatar]': '_avatar',
8588
'[attr.aria-selected]': 'selected.toString()',
8689
'[attr.aria-disabled]': 'disabled.toString()',
8790
},
@@ -98,6 +101,7 @@ export class MatListOption extends _MatListOptionMixinBase
98101
/** Whether the option has focus. */
99102
_hasFocus: boolean = false;
100103

104+
@ContentChild(MatListAvatarCssMatStyler) _avatar: MatListAvatarCssMatStyler;
101105
@ContentChildren(MatLine) _lines: QueryList<MatLine>;
102106

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

0 commit comments

Comments
 (0)