Skip to content

Commit 205a4a9

Browse files
fix(material/list): Mat Selection List example is limited to html temp
Added a reactive forms example to the single selection list Fixes #25894
1 parent da00f48 commit 205a4a9

File tree

5 files changed

+57
-32
lines changed

5 files changed

+57
-32
lines changed

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

Lines changed: 1 addition & 0 deletions
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,33 @@
1+
import {Component} from '@angular/core';
2+
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms';
3+
import {MatListModule} from '@angular/material/list';
4+
interface Shoes {
5+
value: string;
6+
name: string;
7+
}
8+
/**
9+
* @title List with single selection using a Reactive Forms approach
10+
*/
11+
@Component({
12+
selector: 'list-single-selection-reactive-form-example',
13+
templateUrl: 'list-single-selection-form-example.html',
14+
standalone: true,
15+
imports: [MatListModule, FormsModule, ReactiveFormsModule],
16+
})
17+
export class ListSingleSelectionReactiveFormExample {
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+
}
33+
}
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
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>
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>
Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
11
import {Component} from '@angular/core';
2-
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms';
32
import {MatListModule} from '@angular/material/list';
4-
interface Shoes {
5-
value: string;
6-
name: string;
7-
}
3+
84
/**
9-
* @title List with single selection using Reactive Forms
5+
* @title List with single selection
106
*/
117
@Component({
128
selector: 'list-single-selection-example',
139
templateUrl: 'list-single-selection-example.html',
1410
standalone: true,
15-
imports: [MatListModule, FormsModule, ReactiveFormsModule],
11+
imports: [MatListModule],
1612
})
1713
export class ListSingleSelectionExample {
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-
}
14+
typesOfShoes: string[] = ['Boots', 'Clogs', 'Loafers', 'Moccasins', 'Sneakers'];
3315
}

0 commit comments

Comments
 (0)