-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(material/list): Mat Selection List example is limited to html te…
…mp (#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 (cherry picked from commit 2f49eaa)
- Loading branch information
1 parent
1644662
commit cbc345a
Showing
7 changed files
with
117 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...material/list/list-single-selection-reactive-form/list-single-selection-form-example.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<form [formGroup]="form"> | ||
<mat-selection-list #shoesList [formControl]="shoesControl" name="shoes" [multiple]="false"> | ||
@for (shoe of shoes; track shoe) { | ||
<mat-list-option [value]="shoe.value">{{shoe.name}}</mat-list-option> | ||
} | ||
</mat-selection-list> | ||
<p> | ||
Option selected: {{shoesControl.value ? shoesControl.value[0] : 'None'}} | ||
</p> | ||
</form> |
34 changes: 34 additions & 0 deletions
34
...l/list/list-single-selection-reactive-form/list-single-selection-reactive-form-example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {Component} from '@angular/core'; | ||
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms'; | ||
import {MatListModule} from '@angular/material/list'; | ||
|
||
interface Shoes { | ||
value: string; | ||
name: string; | ||
} | ||
/** | ||
* @title List with single selection using Reactive forms | ||
*/ | ||
@Component({ | ||
selector: 'list-single-selection-reactive-form-example', | ||
templateUrl: 'list-single-selection-form-example.html', | ||
standalone: true, | ||
imports: [MatListModule, FormsModule, ReactiveFormsModule], | ||
}) | ||
export class ListSingleSelectionReactiveFormExample { | ||
form: FormGroup; | ||
shoes: Shoes[] = [ | ||
{value: 'boots', name: 'Boots'}, | ||
{value: 'clogs', name: 'Clogs'}, | ||
{value: 'loafers', name: 'Loafers'}, | ||
{value: 'moccasins', name: 'Moccasins'}, | ||
{value: 'sneakers', name: 'Sneakers'}, | ||
]; | ||
shoesControl = new FormControl(); | ||
|
||
constructor() { | ||
this.form = new FormGroup({ | ||
clothes: this.shoesControl, | ||
}); | ||
} | ||
} |
19 changes: 10 additions & 9 deletions
19
...omponents-examples/material/list/list-single-selection/list-single-selection-example.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
<mat-selection-list #shoes [multiple]="false"> | ||
@for (shoe of typesOfShoes; track shoe) { | ||
<mat-list-option [value]="shoe">{{shoe}}</mat-list-option> | ||
} | ||
</mat-selection-list> | ||
|
||
<p> | ||
Option selected: {{shoes.selectedOptions.hasValue() ? shoes.selectedOptions.selected[0].value : 'None'}} | ||
</p> | ||
<form [formGroup]="form"> | ||
<mat-selection-list #shoesList [formControl]="shoesControl" name="shoes" [multiple]="false"> | ||
@for (shoe of shoes; track shoe) { | ||
<mat-list-option [value]="shoe.value">{{shoe.name}}</mat-list-option> | ||
} | ||
</mat-selection-list> | ||
<p> | ||
Option selected: {{shoesControl.value ? shoesControl.value[0] : 'None'}} | ||
</p> | ||
</form> |
26 changes: 22 additions & 4 deletions
26
src/components-examples/material/list/list-single-selection/list-single-selection-example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,33 @@ | ||
import {Component} from '@angular/core'; | ||
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms'; | ||
import {MatListModule} from '@angular/material/list'; | ||
|
||
interface Shoes { | ||
value: string; | ||
name: string; | ||
} | ||
/** | ||
* @title List with single selection | ||
* @title List with single selection using Reactive Forms | ||
*/ | ||
@Component({ | ||
selector: 'list-single-selection-example', | ||
templateUrl: 'list-single-selection-example.html', | ||
standalone: true, | ||
imports: [MatListModule], | ||
imports: [MatListModule, FormsModule, ReactiveFormsModule], | ||
}) | ||
export class ListSingleSelectionExample { | ||
typesOfShoes: string[] = ['Boots', 'Clogs', 'Loafers', 'Moccasins', 'Sneakers']; | ||
form: FormGroup; | ||
shoes: Shoes[] = [ | ||
{value: 'boots', name: 'Boots'}, | ||
{value: 'clogs', name: 'Clogs'}, | ||
{value: 'loafers', name: 'Loafers'}, | ||
{value: 'moccasins', name: 'Moccasins'}, | ||
{value: 'sneakers', name: 'Sneakers'}, | ||
]; | ||
shoesControl = new FormControl(); | ||
|
||
constructor() { | ||
this.form = new FormGroup({ | ||
clothes: this.shoesControl, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters