-
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.
feat(material/autocomplete): add input to require selection from the …
…panel (#27423) Adds the `requireSelection` input to the autocomplete, which when enabled will clear the input value if the user doesn't select an option from the list. Fixes #3334.
- Loading branch information
Showing
14 changed files
with
369 additions
and
26 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
...al/autocomplete/autocomplete-require-selection/autocomplete-require-selection-example.css
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,9 @@ | ||
.example-form { | ||
min-width: 150px; | ||
max-width: 500px; | ||
width: 100%; | ||
} | ||
|
||
.example-full-width { | ||
width: 100%; | ||
} |
18 changes: 18 additions & 0 deletions
18
...l/autocomplete/autocomplete-require-selection/autocomplete-require-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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<form class="example-form"> | ||
<mat-form-field class="example-full-width"> | ||
<mat-label>Number</mat-label> | ||
<input type="text" | ||
placeholder="Pick one" | ||
aria-label="Number" | ||
matInput | ||
[formControl]="myControl" | ||
[matAutocomplete]="auto"> | ||
<mat-autocomplete requireSelection #auto="matAutocomplete"> | ||
<mat-option *ngFor="let option of filteredOptions | async" [value]="option"> | ||
{{option}} | ||
</mat-option> | ||
</mat-autocomplete> | ||
</mat-form-field> | ||
</form> | ||
|
||
Control value: {{myControl.value}} |
45 changes: 45 additions & 0 deletions
45
...ial/autocomplete/autocomplete-require-selection/autocomplete-require-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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {Component, OnInit} from '@angular/core'; | ||
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms'; | ||
import {Observable} from 'rxjs'; | ||
import {map, startWith} from 'rxjs/operators'; | ||
import {NgFor, AsyncPipe} from '@angular/common'; | ||
import {MatAutocompleteModule} from '@angular/material/autocomplete'; | ||
import {MatInputModule} from '@angular/material/input'; | ||
import {MatFormFieldModule} from '@angular/material/form-field'; | ||
|
||
/** | ||
* @title Require an autocomplete option to be selected. | ||
*/ | ||
@Component({ | ||
selector: 'autocomplete-require-selection-example', | ||
templateUrl: 'autocomplete-require-selection-example.html', | ||
styleUrls: ['autocomplete-require-selection-example.css'], | ||
standalone: true, | ||
imports: [ | ||
FormsModule, | ||
MatFormFieldModule, | ||
MatInputModule, | ||
MatAutocompleteModule, | ||
ReactiveFormsModule, | ||
NgFor, | ||
AsyncPipe, | ||
], | ||
}) | ||
export class AutocompleteRequireSelectionExample implements OnInit { | ||
myControl = new FormControl(''); | ||
options: string[] = ['One', 'Two', 'Three', 'Three', 'Four']; | ||
filteredOptions: Observable<string[]>; | ||
|
||
ngOnInit() { | ||
this.filteredOptions = this.myControl.valueChanges.pipe( | ||
startWith(''), | ||
map(value => this._filter(value || '')), | ||
); | ||
} | ||
|
||
private _filter(value: string): string[] { | ||
const filterValue = value.toLowerCase(); | ||
|
||
return this.options.filter(option => option.toLowerCase().includes(filterValue)); | ||
} | ||
} |
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
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
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
Oops, something went wrong.