Skip to content

Commit 78b40a0

Browse files
fix(material/list): Mat Selection List example is limited to html templating
Updated the single selection list example from a template-driven form to a reactive form. Fixes #25894
1 parent cee9b04 commit 78b40a0

File tree

4 files changed

+72
-15
lines changed

4 files changed

+72
-15
lines changed
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<mat-selection-list #shoes [multiple]="false">
2-
@for (shoe of typesOfShoes; track shoe) {
3-
<mat-list-option [value]="shoe">{{shoe}}</mat-list-option>
4-
}
5-
</mat-selection-list>
6-
7-
<p>
8-
Option selected: {{shoes.selectedOptions.hasValue() ? shoes.selectedOptions.selected[0].value : 'None'}}
9-
</p>
1+
<form [formGroup]="form">
2+
<mat-selection-list #shoesList [formControl]="shoesControl" name="shoes" [multiple]="false">
3+
@for (shoe of shoes; track shoe) {
4+
<mat-list-option [value]="shoe.value">{{shoe.name}}</mat-list-option>
5+
}
6+
</mat-selection-list>
7+
<p>
8+
Option selected: {{shoesControl.value ? shoesControl.value[0] : 'None'}}
9+
</p>
10+
</form>
Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
import {Component} from '@angular/core';
2+
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms';
23
import {MatListModule} from '@angular/material/list';
3-
4+
interface Shoes {
5+
value: string;
6+
name: string;
7+
}
48
/**
5-
* @title List with single selection
9+
* @title List with single selection using Reactive Forms
610
*/
711
@Component({
812
selector: 'list-single-selection-example',
913
templateUrl: 'list-single-selection-example.html',
1014
standalone: true,
11-
imports: [MatListModule],
15+
imports: [MatListModule, FormsModule, ReactiveFormsModule],
1216
})
1317
export class ListSingleSelectionExample {
14-
typesOfShoes: string[] = ['Boots', 'Clogs', 'Loafers', 'Moccasins', 'Sneakers'];
18+
form: FormGroup;
19+
shoes: Shoes[] = [
20+
{value: 'boots', name: 'Boots'},
21+
{value: 'clogs', name: 'Clogs'},
22+
{value: 'loafers', name: 'Loafers'},
23+
{value: 'moccasins', name: 'Moccasins'},
24+
{value: 'sneakers', name: 'Sneakers'},
25+
];
26+
shoesControl = new FormControl();
27+
28+
constructor() {
29+
this.form = new FormGroup({
30+
clothes: this.shoesControl,
31+
});
32+
}
1533
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,19 @@ <h2>Single Selection list</h2>
213213
</mat-selection-list>
214214

215215
<p>Selected: {{favoriteOptions | json}}</p>
216+
217+
<h4>Single Selection List with Reactive Forms</h4>
218+
219+
<form [formGroup]="form">
220+
<mat-selection-list #shoesList [formControl]="shoesControl" name="shoes" [multiple]="false">
221+
@for (shoe of shoes; track shoe) {
222+
<mat-list-option [value]="shoe.value">{{shoe.name}}</mat-list-option>
223+
}
224+
</mat-selection-list>
225+
<p>
226+
Option selected: {{shoesControl.value ? shoesControl.value[0] : 'None'}}
227+
</p>
228+
</form>
216229
</div>
217230

218231
<div>

src/dev-app/list/list-demo.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {CommonModule} from '@angular/common';
1010
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, inject} from '@angular/core';
11-
import {FormsModule} from '@angular/forms';
11+
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms';
1212
import {MatButtonModule} from '@angular/material/button';
1313
import {MatIconModule} from '@angular/material/icon';
1414
import {MatListModule, MatListOptionTogglePosition} from '@angular/material/list';
@@ -18,18 +18,39 @@ interface Link {
1818
name: string;
1919
href: string;
2020
}
21+
interface Shoes {
22+
value: string;
23+
name: string;
24+
}
2125

2226
@Component({
2327
selector: 'list-demo',
2428
templateUrl: 'list-demo.html',
2529
styleUrl: 'list-demo.css',
2630
standalone: true,
27-
imports: [CommonModule, FormsModule, MatButtonModule, MatIconModule, MatListModule],
31+
imports: [
32+
CommonModule,
33+
FormsModule,
34+
MatButtonModule,
35+
MatIconModule,
36+
MatListModule,
37+
ReactiveFormsModule,
38+
],
2839
changeDetection: ChangeDetectionStrategy.OnPush,
2940
})
3041
export class ListDemo {
3142
items: string[] = ['Pepper', 'Salt', 'Paprika'];
3243

44+
form: FormGroup;
45+
shoes: Shoes[] = [
46+
{value: 'boots', name: 'Boots'},
47+
{value: 'clogs', name: 'Clogs'},
48+
{value: 'loafers', name: 'Loafers'},
49+
{value: 'moccasins', name: 'Moccasins'},
50+
{value: 'sneakers', name: 'Sneakers'},
51+
];
52+
shoesControl = new FormControl();
53+
3354
togglePosition: MatListOptionTogglePosition = 'before';
3455

3556
contacts: {name: string; headline: string}[] = [
@@ -84,6 +105,10 @@ export class ListDemo {
84105
this.activatedRoute.url.subscribe(() => {
85106
this.cdr.markForCheck();
86107
});
108+
109+
this.form = new FormGroup({
110+
shoes: this.shoesControl,
111+
});
87112
}
88113

89114
onSelectedOptionsChange(values: string[]) {

0 commit comments

Comments
 (0)