Skip to content
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

fix(material/list): spike on supporting list item with switch #28550

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/dev-app/list/BUILD.bazel
Expand Up @@ -13,6 +13,7 @@ ng_module(
"//src/material/button",
"//src/material/icon",
"//src/material/list",
"//src/material/slide-toggle",
],
)

Expand Down
7 changes: 6 additions & 1 deletion src/dev-app/list/list-demo.html
Expand Up @@ -2,6 +2,7 @@
<h1>mat-list demo</h1>

<button mat-raised-button (click)="thirdLine = !thirdLine">Show third line</button>
<button mat-raised-button (click)="showSwitch = !showSwitch">Show switch</button>
<button mat-raised-button (click)="showBoxes = !showBoxes">Show item boxes</button>

<div class="demo-list" [class.demo-show-boxes]="showBoxes">
Expand All @@ -12,7 +13,10 @@ <h2>Normal lists</h2>
<mat-list>
<div mat-subheader>Items</div>
@for (item of items; track item) {
<mat-list-item>{{item}}</mat-list-item>
<mat-list-item>
{{item}}
<mat-slide-toggle matListItemSwitch *ngIf="showSwitch"></mat-slide-toggle>
</mat-list-item>
}
</mat-list>

Expand All @@ -24,6 +28,7 @@ <h2>Normal lists</h2>
<div matListItemLine>extra line</div>
}
<div matListItemLine>{{contact.headline}}</div>
<mat-slide-toggle matListItemSwitch *ngIf="showSwitch"></mat-slide-toggle>
</mat-list-item>
}
</mat-list>
Expand Down
11 changes: 10 additions & 1 deletion src/dev-app/list/list-demo.ts
Expand Up @@ -12,13 +12,21 @@ import {MatButtonModule} from '@angular/material/button';
import {MatListModule, MatListOptionTogglePosition} from '@angular/material/list';
import {MatIconModule} from '@angular/material/icon';
import {CommonModule} from '@angular/common';
import {MatSlideToggleModule} from '@angular/material/slide-toggle';

@Component({
selector: 'list-demo',
templateUrl: 'list-demo.html',
styleUrls: ['list-demo.css'],
standalone: true,
imports: [CommonModule, FormsModule, MatButtonModule, MatIconModule, MatListModule],
imports: [
CommonModule,
FormsModule,
MatButtonModule,
MatIconModule,
MatListModule,
MatSlideToggleModule,
],
})
export class ListDemo {
items: string[] = ['Pepper', 'Salt', 'Paprika'];
Expand Down Expand Up @@ -60,6 +68,7 @@ export class ListDemo {
];

thirdLine = false;
showSwitch = false;
showBoxes = false;
infoClicked = false;
selectionListDisabled = false;
Expand Down
15 changes: 15 additions & 0 deletions src/material/list/list-item-sections.ts
Expand Up @@ -52,6 +52,21 @@ export class MatListItemLine {
})
export class MatListItemMeta {}

/**
* Directive matching an optional switch for list items.
*
* List items can reserve space at the end of an item to display a control,
* button or additional text content.
*/
@Directive({
selector: 'mat-slide-toggle[matListItemMeta]',
host: {
'class': 'mat-mdc-list-item-switch mdc-list-item__end',
},
standalone: true,
})
export class MatListItemSwitch {}

/**
* @docs-private
*
Expand Down
1 change: 1 addition & 0 deletions src/material/list/list-item.html
Expand Up @@ -10,6 +10,7 @@
</span>

<ng-content select="[matListItemMeta]"></ng-content>
<ng-content select="[matListItemSwitch]"></ng-content>

<ng-content select="mat-divider"></ng-content>

Expand Down
2 changes: 2 additions & 0 deletions src/material/list/list-module.ts
Expand Up @@ -20,6 +20,7 @@ import {
MatListItemMeta,
MatListItemAvatar,
MatListItemIcon,
MatListItemSwitch,
} from './list-item-sections';
import {MatNavList} from './nav-list';
import {MatSelectionList} from './selection-list';
Expand All @@ -44,6 +45,7 @@ import {ObserversModule} from '@angular/cdk/observers';
MatListItemLine,
MatListItemTitle,
MatListItemMeta,
MatListItemSwitch,
],
exports: [
MatList,
Expand Down
9 changes: 8 additions & 1 deletion src/material/list/list.ts
Expand Up @@ -24,7 +24,12 @@ import {
} from '@angular/core';
import {MAT_RIPPLE_GLOBAL_OPTIONS, RippleGlobalOptions} from '@angular/material/core';
import {MatListBase, MatListItemBase} from './list-base';
import {MatListItemLine, MatListItemMeta, MatListItemTitle} from './list-item-sections';
import {
MatListItemLine,
MatListItemMeta,
MatListItemSwitch,
MatListItemTitle,
} from './list-item-sections';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {CdkObserveContent} from '@angular/cdk/observers';

Expand Down Expand Up @@ -59,6 +64,7 @@ export class MatList extends MatListBase {}
'[class.mdc-list-item--with-leading-avatar]': '_avatars.length !== 0',
'[class.mdc-list-item--with-leading-icon]': '_icons.length !== 0',
'[class.mdc-list-item--with-trailing-meta]': '_meta.length !== 0',
'[class.mdc-list-item--with-trailing-switch]': '_switch.length !== 0',
'[class._mat-animation-noopable]': '_noopAnimations',
'[attr.aria-current]': '_getAriaCurrent()',
},
Expand All @@ -72,6 +78,7 @@ export class MatListItem extends MatListItemBase {
@ContentChildren(MatListItemLine, {descendants: true}) _lines: QueryList<MatListItemLine>;
@ContentChildren(MatListItemTitle, {descendants: true}) _titles: QueryList<MatListItemTitle>;
@ContentChildren(MatListItemMeta, {descendants: true}) _meta: QueryList<MatListItemMeta>;
@ContentChildren(MatListItemSwitch, {descendants: true}) _switch: QueryList<MatListItemSwitch>;
@ViewChild('unscopedContent') _unscopedContent: ElementRef<HTMLSpanElement>;
@ViewChild('text') _itemText: ElementRef<HTMLElement>;

Expand Down