MatAutoComplete - 当在输入中输入未知文本并按下 "ENTER" 键时,如何停止在 mat-chip-list 中打开 MatAutoComplete
MatAutoComplete - How to stop opening of MatAutoComplete in mat-chip-list when an unknown text is entered in the input and "ENTER" key is pressed
我通过参考官方 angular material 示例 https://material.angular.io/components/chips/overview#chip-input and https://stackblitz.com/angular/arjnbxmepgn?file=src%2Fapp%2Fchips-autocomplete-example.html 将 mat-chip-list
与 mat-autocomplete
一起使用。上面示例中显示的相同模板在这里 -
<mat-form-field class="example-chip-list">
<mat-chip-list #chipList aria-label="Fruit selection">
<mat-chip
*ngFor="let fruit of fruits"
[selectable]="selectable"
[removable]="removable"
(removed)="remove(fruit)">
{{fruit}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input
placeholder="New fruit..."
#fruitInput
[formControl]="fruitCtrl"
[matAutocomplete]="auto"
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
(matChipInputTokenEnd)="add($event)">
</mat-chip-list>
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
<mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
{{fruit}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
它工作正常,除了以下问题 -
只要用户聚焦输入,MatAutoComplete
面板就会打开并显示建议。如果用户输入建议中不存在的文本并按下 "ENTER" 键,则输入的文本在 Chip 中可见,并打开 MatAutoComplate
面板。在这种情况下,我想停止打开 MatAutoComplete
面板。如果用户输入未知文本,我不想打开建议面板 [即建议以外的文本]。 https://stackblitz.com/angular/arjnbxmepgn?file=src%2Fapp%2Fchips-autocomplete-example.html.
中可以看到相同的情况
如果用户从建议中选择一个选项并按下 "ENTER" 键,MatAutoComplete
面板不会打开。当用户在输入中输入未知文本并按下 "ENTER" 键时,这就是我想要的场景。
任何帮助将不胜感激。
我已将示例更新为:
import {COMMA, ENTER} from '@angular/cdk/keycodes';
import {Component, ElementRef, ViewChild} from '@angular/core';
import {FormControl} from '@angular/forms';
import {MatAutocompleteSelectedEvent, MatAutocomplete, MatAutocompleteTrigger} from '@angular/material/autocomplete';
import {MatChipInputEvent} from '@angular/material/chips';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';
/**
* @title Chips Autocomplete
*/
@Component({
selector: 'chips-autocomplete-example',
templateUrl: 'chips-autocomplete-example.html',
styleUrls: ['chips-autocomplete-example.css'],
})
export class ChipsAutocompleteExample {
visible = true;
selectable = true;
removable = true;
separatorKeysCodes: number[] = [ENTER, COMMA];
fruitCtrl = new FormControl();
filteredFruits: Observable<string[]>;
fruits: string[] = ['Lemon'];
allFruits: string[] = ['Apple', 'Lemon', 'Lime', 'Orange', 'Strawberry'];
@ViewChild('fruitInput') fruitInput: ElementRef<HTMLInputElement>;
@ViewChild('auto') matAutocomplete: MatAutocomplete;
@ViewChild(MatAutocompleteTrigger) autocomplete: MatAutocompleteTrigger;
constructor() {
this.filteredFruits = this.fruitCtrl.valueChanges.pipe(
startWith(null),
map((fruit: string | null) => fruit ? this._filter(fruit) : this.allFruits.slice()));
}
add(event: MatChipInputEvent): void {
const input = event.input;
const value = event.value;
// Add our fruit
if ((value || '').trim()) {
if(this.allFruits.indexOf(value) > -1){
this.fruits.push(value.trim());
}
}
// Reset the input value
if (input) {
input.value = '';
}
this.fruitCtrl.setValue(null);
this.autocomplete.closePanel();
}
remove(fruit: string): void {
const index = this.fruits.indexOf(fruit);
if (index >= 0) {
this.fruits.splice(index, 1);
}
}
selected(event: MatAutocompleteSelectedEvent): void {
this.fruits.push(event.option.viewValue);
this.fruitInput.nativeElement.value = '';
this.fruitCtrl.setValue(null);
}
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();
return this.allFruits.filter(fruit => fruit.toLowerCase().indexOf(filterValue) === 0);
}
}
我通过参考官方 angular material 示例 https://material.angular.io/components/chips/overview#chip-input and https://stackblitz.com/angular/arjnbxmepgn?file=src%2Fapp%2Fchips-autocomplete-example.html 将 mat-chip-list
与 mat-autocomplete
一起使用。上面示例中显示的相同模板在这里 -
<mat-form-field class="example-chip-list">
<mat-chip-list #chipList aria-label="Fruit selection">
<mat-chip
*ngFor="let fruit of fruits"
[selectable]="selectable"
[removable]="removable"
(removed)="remove(fruit)">
{{fruit}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input
placeholder="New fruit..."
#fruitInput
[formControl]="fruitCtrl"
[matAutocomplete]="auto"
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
(matChipInputTokenEnd)="add($event)">
</mat-chip-list>
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
<mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
{{fruit}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
它工作正常,除了以下问题 -
只要用户聚焦输入,MatAutoComplete
面板就会打开并显示建议。如果用户输入建议中不存在的文本并按下 "ENTER" 键,则输入的文本在 Chip 中可见,并打开 MatAutoComplate
面板。在这种情况下,我想停止打开 MatAutoComplete
面板。如果用户输入未知文本,我不想打开建议面板 [即建议以外的文本]。 https://stackblitz.com/angular/arjnbxmepgn?file=src%2Fapp%2Fchips-autocomplete-example.html.
如果用户从建议中选择一个选项并按下 "ENTER" 键,MatAutoComplete
面板不会打开。当用户在输入中输入未知文本并按下 "ENTER" 键时,这就是我想要的场景。
任何帮助将不胜感激。
我已将示例更新为:
import {COMMA, ENTER} from '@angular/cdk/keycodes';
import {Component, ElementRef, ViewChild} from '@angular/core';
import {FormControl} from '@angular/forms';
import {MatAutocompleteSelectedEvent, MatAutocomplete, MatAutocompleteTrigger} from '@angular/material/autocomplete';
import {MatChipInputEvent} from '@angular/material/chips';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';
/**
* @title Chips Autocomplete
*/
@Component({
selector: 'chips-autocomplete-example',
templateUrl: 'chips-autocomplete-example.html',
styleUrls: ['chips-autocomplete-example.css'],
})
export class ChipsAutocompleteExample {
visible = true;
selectable = true;
removable = true;
separatorKeysCodes: number[] = [ENTER, COMMA];
fruitCtrl = new FormControl();
filteredFruits: Observable<string[]>;
fruits: string[] = ['Lemon'];
allFruits: string[] = ['Apple', 'Lemon', 'Lime', 'Orange', 'Strawberry'];
@ViewChild('fruitInput') fruitInput: ElementRef<HTMLInputElement>;
@ViewChild('auto') matAutocomplete: MatAutocomplete;
@ViewChild(MatAutocompleteTrigger) autocomplete: MatAutocompleteTrigger;
constructor() {
this.filteredFruits = this.fruitCtrl.valueChanges.pipe(
startWith(null),
map((fruit: string | null) => fruit ? this._filter(fruit) : this.allFruits.slice()));
}
add(event: MatChipInputEvent): void {
const input = event.input;
const value = event.value;
// Add our fruit
if ((value || '').trim()) {
if(this.allFruits.indexOf(value) > -1){
this.fruits.push(value.trim());
}
}
// Reset the input value
if (input) {
input.value = '';
}
this.fruitCtrl.setValue(null);
this.autocomplete.closePanel();
}
remove(fruit: string): void {
const index = this.fruits.indexOf(fruit);
if (index >= 0) {
this.fruits.splice(index, 1);
}
}
selected(event: MatAutocompleteSelectedEvent): void {
this.fruits.push(event.option.viewValue);
this.fruitInput.nativeElement.value = '';
this.fruitCtrl.setValue(null);
}
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();
return this.allFruits.filter(fruit => fruit.toLowerCase().indexOf(filterValue) === 0);
}
}