Skip to content

Commit 2f49eaa

Browse files
docs(material/list): Mat Selection List example is limited to html temp (#29348)
* 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 * fix(material/list): Mat Selection List example is limited to html templating Added a reactive forms example to the single selection list Fixes #25894
1 parent 168b89e commit 2f49eaa

File tree

7 files changed

+117
-15
lines changed

7 files changed

+117
-15
lines changed

src/components-examples/material/list/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export {ListOverviewExample} from './list-overview/list-overview-example';
22
export {ListSectionsExample} from './list-sections/list-sections-example';
33
export {ListSelectionExample} from './list-selection/list-selection-example';
44
export {ListSingleSelectionExample} from './list-single-selection/list-single-selection-example';
5+
export {ListSingleSelectionReactiveFormExample} from './list-single-selection-reactive-form/list-single-selection-reactive-form-example';
56
export {ListHarnessExample} from './list-harness/list-harness-example';
67
export {ListVariantsExample} from './list-variants/list-variants-example';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {Component} from '@angular/core';
2+
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms';
3+
import {MatListModule} from '@angular/material/list';
4+
5+
interface Shoes {
6+
value: string;
7+
name: string;
8+
}
9+
/**
10+
* @title List with single selection using Reactive forms
11+
*/
12+
@Component({
13+
selector: 'list-single-selection-reactive-form-example',
14+
templateUrl: 'list-single-selection-form-example.html',
15+
standalone: true,
16+
imports: [MatListModule, FormsModule, ReactiveFormsModule],
17+
})
18+
export class ListSingleSelectionReactiveFormExample {
19+
form: FormGroup;
20+
shoes: Shoes[] = [
21+
{value: 'boots', name: 'Boots'},
22+
{value: 'clogs', name: 'Clogs'},
23+
{value: 'loafers', name: 'Loafers'},
24+
{value: 'moccasins', name: 'Moccasins'},
25+
{value: 'sneakers', name: 'Sneakers'},
26+
];
27+
shoesControl = new FormControl();
28+
29+
constructor() {
30+
this.form = new FormGroup({
31+
clothes: this.shoesControl,
32+
});
33+
}
34+
}
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>
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

+13
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

+27-2
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)