-
Notifications
You must be signed in to change notification settings - Fork 6.8k
docs: add signal form example for mat-select
#33886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
msmallest
wants to merge
4
commits into
angular:main
Choose a base branch
from
msmallest:docs-select-signal-form
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−6
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9422e01
docs: add signal form example for `mat-select`
msmallest d20baa6
docs: add signal form example for "Form field features"
msmallest 78397fc
docs: add signal form example for select disabling example
msmallest ecca6ff
docs: add multiselect signal form example
msmallest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
26 changes: 26 additions & 0 deletions
26
...ples/material/select/select-disabled-signal-form/select-disabled-signal-form-example.html
This file contains hidden or 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,26 @@ | ||
| <p> | ||
| <mat-checkbox [formField]="form.disableField">Disable select</mat-checkbox> | ||
| </p> | ||
|
|
||
| <h4>mat-select</h4> | ||
| <mat-form-field> | ||
| <mat-label>Choose an option</mat-label> | ||
| <mat-select [formField]="form.option"> | ||
| <mat-option value="option1">Option 1</mat-option> | ||
| <mat-option value="option2" disabled>Option 2 (disabled)</mat-option> | ||
| <mat-option value="option3">Option 3</mat-option> | ||
| </mat-select> | ||
| </mat-form-field> | ||
|
|
||
| <h4>native html select</h4> | ||
| <mat-form-field> | ||
| <mat-label>Choose an option</mat-label> | ||
| <select matNativeControl [formField]="form.vehicle"> | ||
| <option value="" selected></option> | ||
| <option value="volvo">Volvo</option> | ||
| <option value="saab" disabled>Saab</option> | ||
| <option value="mercedes">Mercedes</option> | ||
| <option value="audi">Audi</option> | ||
| </select> | ||
| </mat-form-field> | ||
|
|
20 changes: 20 additions & 0 deletions
20
...amples/material/select/select-disabled-signal-form/select-disabled-signal-form-example.ts
This file contains hidden or 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,20 @@ | ||
| import {Component, signal} from '@angular/core'; | ||
| import {disabled, form, FormField} from '@angular/forms/signals'; | ||
| import {MatInputModule} from '@angular/material/input'; | ||
| import {MatSelectModule} from '@angular/material/select'; | ||
| import {MatFormFieldModule} from '@angular/material/form-field'; | ||
| import {MatCheckboxModule} from '@angular/material/checkbox'; | ||
|
|
||
| /** @title Disabled select (signal forms) */ | ||
| @Component({ | ||
| selector: 'select-disabled-signal-form-example', | ||
| templateUrl: 'select-disabled-signal-form-example.html', | ||
| imports: [MatCheckboxModule, FormField, MatFormFieldModule, MatSelectModule, MatInputModule], | ||
| }) | ||
| export class SelectDisabledSignalFormExample { | ||
| // TODO - more distinct name? | ||
| protected form = form(signal({option: '', vehicle: '', disableField: false}), p => { | ||
| disabled(p.option, {when: ({valueOf}) => valueOf(p.disableField)}); | ||
| disabled(p.vehicle, {when: ({valueOf}) => valueOf(p.disableField)}); | ||
| }); | ||
| } |
23 changes: 23 additions & 0 deletions
23
...nts-examples/material/select/select-form-signal-form/select-form-signal-form-example.html
This file contains hidden or 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,23 @@ | ||
| <form> | ||
| <h4>mat-select</h4> | ||
| <mat-form-field> | ||
| <mat-label>Favorite food</mat-label> | ||
| <mat-select [formField]="selectedValue"> | ||
| @for (food of foods; track food) { | ||
| <mat-option [value]="food.value">{{food.viewValue}}</mat-option> | ||
| } | ||
| </mat-select> | ||
| </mat-form-field> | ||
| <p> Selected food: {{selectedValue().value()}} </p> | ||
| <h4>native html select</h4> | ||
| <mat-form-field> | ||
| <mat-label>Favorite car</mat-label> | ||
| <select matNativeControl [formField]="selectedCar"> | ||
| <option value="" selected></option> | ||
| @for (car of cars; track car) { | ||
| <option [value]="car.value">{{car.viewValue}}</option> | ||
| } | ||
| </select> | ||
| </mat-form-field> | ||
| <p> Selected car: {{selectedCar().value()}} </p> | ||
| </form> |
40 changes: 40 additions & 0 deletions
40
...nents-examples/material/select/select-form-signal-form/select-form-signal-form-example.ts
This file contains hidden or 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,40 @@ | ||
| import {Component, signal} from '@angular/core'; | ||
| import {MatInputModule} from '@angular/material/input'; | ||
| import {MatSelectModule} from '@angular/material/select'; | ||
| import {MatFormFieldModule} from '@angular/material/form-field'; | ||
| import {form, FormField} from '@angular/forms/signals'; | ||
|
|
||
| interface Food { | ||
| value: string; | ||
| viewValue: string; | ||
| } | ||
|
|
||
| interface Car { | ||
| value: string; | ||
| viewValue: string; | ||
| } | ||
|
|
||
| /** | ||
| * @title Select in a form (signal forms) | ||
| */ | ||
| @Component({ | ||
| selector: 'select-form-signal-form-example', | ||
| templateUrl: 'select-form-signal-form-example.html', | ||
| imports: [FormField, MatFormFieldModule, MatSelectModule, MatInputModule], | ||
| }) | ||
| export class SelectFormSignalFormExample { | ||
| protected selectedValue = form(signal('')); | ||
| protected selectedCar = form(signal('')); | ||
|
|
||
| protected foods: Food[] = [ | ||
| {value: 'steak-0', viewValue: 'Steak'}, | ||
| {value: 'pizza-1', viewValue: 'Pizza'}, | ||
| {value: 'tacos-2', viewValue: 'Tacos'}, | ||
| ]; | ||
|
|
||
| protected cars: Car[] = [ | ||
| {value: 'volvo', viewValue: 'Volvo'}, | ||
| {value: 'saab', viewValue: 'Saab'}, | ||
| {value: 'mercedes', viewValue: 'Mercedes'}, | ||
| ]; | ||
| } |
This file contains hidden or 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
32 changes: 32 additions & 0 deletions
32
.../material/select/select-hint-error-signal-form/select-hint-error-signal-form-example.html
This file contains hidden or 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,32 @@ | ||
| <h4>mat select</h4> | ||
| <mat-form-field> | ||
| <mat-label>Favorite animal</mat-label> | ||
| <mat-select [formField]="animalControl"> | ||
| <mat-option>--</mat-option> | ||
| @for (animal of animals; track animal) { | ||
| <mat-option [value]="animal.name">{{animal.name}}</mat-option> | ||
| } | ||
| </mat-select> | ||
| @if (animalControl().getError('required')) { | ||
| <mat-error>Please choose an animal</mat-error> | ||
| } | ||
| @if (animalSound(); as animalSound) { | ||
| <mat-hint>{{animalSound}}</mat-hint> | ||
| } | ||
| </mat-form-field> | ||
|
|
||
| <h4>native html select</h4> | ||
| <mat-form-field> | ||
| <mat-label>Select your car (required)</mat-label> | ||
| <select matNativeControl [formField]="selectFormControl"> | ||
| <option label="--select something --"></option> | ||
| <option value="saab">Saab</option> | ||
| <option value="mercedes">Mercedes</option> | ||
| <option value="audi">Audi</option> | ||
| </select> | ||
| @if (selectFormControl().getError('required')) { | ||
| <mat-error>This field is required</mat-error> | ||
| } | ||
| <mat-hint>You can pick up your favorite car here</mat-hint> | ||
| </mat-form-field> | ||
|
|
36 changes: 36 additions & 0 deletions
36
...es/material/select/select-hint-error-signal-form/select-hint-error-signal-form-example.ts
This file contains hidden or 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,36 @@ | ||
| import {Component, computed, signal} from '@angular/core'; | ||
| import {MatInputModule} from '@angular/material/input'; | ||
| import {MatSelectModule} from '@angular/material/select'; | ||
| import {MatFormFieldModule} from '@angular/material/form-field'; | ||
| import {form, FormField, required} from '@angular/forms/signals'; | ||
|
|
||
| interface Animal { | ||
| name: string; | ||
| sound: string; | ||
| } | ||
|
|
||
| /** @title Select with form field features (signal forms) */ | ||
| @Component({ | ||
| selector: 'select-hint-error-signal-form-example', | ||
| templateUrl: 'select-hint-error-signal-form-example.html', | ||
| imports: [MatFormFieldModule, MatSelectModule, FormField, MatInputModule], | ||
| }) | ||
| export class SelectHintErrorSignalFormExample { | ||
| protected animalControl = form(signal<string>(''), p => { | ||
| required(p); | ||
| }); | ||
| protected selectFormControl = form(signal(''), p => { | ||
| required(p); | ||
| }); | ||
|
|
||
| protected animalSound = computed<string | undefined>( | ||
| () => this.animals.find(a => a.name === this.animalControl().value())?.sound, | ||
| ); | ||
|
|
||
| protected animals: Animal[] = [ | ||
| {name: 'Dog', sound: 'Woof!'}, | ||
| {name: 'Cat', sound: 'Meow!'}, | ||
| {name: 'Cow', sound: 'Moo!'}, | ||
| {name: 'Fox', sound: 'Wa-pa-pa-pa-pa-pa-pow!'}, | ||
| ]; | ||
| } |
8 changes: 8 additions & 0 deletions
8
...ples/material/select/select-multiple-signal-form/select-multiple-signal-form-example.html
This file contains hidden or 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,8 @@ | ||
| <mat-form-field> | ||
| <mat-label>Toppings</mat-label> | ||
| <mat-select [formField]="toppings" multiple> | ||
| @for (topping of toppingList; track topping) { | ||
| <mat-option [value]="topping">{{topping}}</mat-option> | ||
| } | ||
| </mat-select> | ||
| </mat-form-field> |
15 changes: 15 additions & 0 deletions
15
...amples/material/select/select-multiple-signal-form/select-multiple-signal-form-example.ts
This file contains hidden or 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,15 @@ | ||
| import {Component, signal} from '@angular/core'; | ||
| import {MatSelectModule} from '@angular/material/select'; | ||
| import {MatFormFieldModule} from '@angular/material/form-field'; | ||
| import {form, FormField} from '@angular/forms/signals'; | ||
|
|
||
| /** @title Select with multiple selection (signal forms) */ | ||
| @Component({ | ||
| selector: 'select-multiple-signal-form-example', | ||
| templateUrl: 'select-multiple-signal-form-example.html', | ||
| imports: [MatFormFieldModule, MatSelectModule, FormField], | ||
| }) | ||
| export class SelectMultipleSignalFormExample { | ||
| toppings = form(signal<string[]>([])); | ||
| toppingList = ['Extra cheese', 'Mushroom', 'Onion', 'Pepperoni', 'Sausage', 'Tomato'] as const; | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@crisbeto When updating this example, I realized that merely adding one example would make things tricky, without a more comprehensive overhaul.
My reasoning:
(template-driven forms, as is consistent with my previous PRs.How should I go about this? In my opinion, I would just do the following two things
My reasoning being that signal forms are now the encouraged default for forms, the docs recently updated the page for custom Material controls to be signal forms first, and that there are plenty of older docs versions to look for older iterations of template/reactive form examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless there's some more awkwardness left in the support for signal forms, we should probably just promote those in all the examples.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TL;DR despite
select multipledefinitely being one of the remaining akwardnesses of signal forms left, I thinkmat-select multiplehandles it perfectly. Thoughts? If this all sounds right, I will proceed with the rest of the examples.Now that you mention it, select multiple is the one edge case with signal forms that has problems right now: angular/angular#68511 There is an outstanding PR pending more decisions in this issue.
However, it seems that it works great in Material. Example project I made for reference: https://stackblitz.com/edit/mhqdr1d1?file=src%2Fexample%2Fselect-multiple-example.ts. Boolean, number, string, string array (added this based on my follow notes on unit tests for select), and object arrays in a
mat-select multipleeach. I have not looked at the internals of howmat-select multipleis implemented, but in practicemat-selectseems like it lines up with the guidance from the Angular Skills which was just amended today: angular/angular@306b99c.I threw together a temp branch with a signal forms version of the mat-select harness test and the mat-select itself test, and modified the
multiplevariantsMultiSelecttest class on line 4802I had to modify some assertions due to value equality differences,
nullvs[]defaults for values, and a few other things that you may want to vet if I was in the right to do so, but with all that considered, those passed once adjusted to the new constraints.