Skip to content

Commit 3bdaaa4

Browse files
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 78b40a0 commit 3bdaaa4

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
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,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+
}

0 commit comments

Comments
 (0)