Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components-examples/material/list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export {ListOverviewExample} from './list-overview/list-overview-example';
export {ListSectionsExample} from './list-sections/list-sections-example';
export {ListSelectionExample} from './list-selection/list-selection-example';
export {ListSingleSelectionExample} from './list-single-selection/list-single-selection-example';
export {ListSingleSelectionReactiveFormExample} from './list-single-selection-reactive-form/list-single-selection-reactive-form-example';
export {ListHarnessExample} from './list-harness/list-harness-example';
export {ListVariantsExample} from './list-variants/list-variants-example';
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>
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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add a new line after this import.


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,
});
}
}
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">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than replacing the example, I think we should add another one with reactive forms.

<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>
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,
});
}
}
13 changes: 13 additions & 0 deletions src/dev-app/list/list-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,19 @@ <h2>Single Selection list</h2>
</mat-selection-list>

<p>Selected: {{favoriteOptions | json}}</p>

<h4>Single Selection List with Reactive Forms</h4>

<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>
</div>

<div>
Expand Down
29 changes: 27 additions & 2 deletions src/dev-app/list/list-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {CommonModule} from '@angular/common';
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, inject} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms';
import {MatButtonModule} from '@angular/material/button';
import {MatIconModule} from '@angular/material/icon';
import {MatListModule, MatListOptionTogglePosition} from '@angular/material/list';
Expand All @@ -18,18 +18,39 @@ interface Link {
name: string;
href: string;
}
interface Shoes {
value: string;
name: string;
}

@Component({
selector: 'list-demo',
templateUrl: 'list-demo.html',
styleUrl: 'list-demo.css',
standalone: true,
imports: [CommonModule, FormsModule, MatButtonModule, MatIconModule, MatListModule],
imports: [
CommonModule,
FormsModule,
MatButtonModule,
MatIconModule,
MatListModule,
ReactiveFormsModule,
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListDemo {
items: string[] = ['Pepper', 'Salt', 'Paprika'];

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();

togglePosition: MatListOptionTogglePosition = 'before';

contacts: {name: string; headline: string}[] = [
Expand Down Expand Up @@ -84,6 +105,10 @@ export class ListDemo {
this.activatedRoute.url.subscribe(() => {
this.cdr.markForCheck();
});

this.form = new FormGroup({
shoes: this.shoesControl,
});
}

onSelectedOptionsChange(values: string[]) {
Expand Down