Skip to content

Commit e7e71ef

Browse files
committed
docs(material/chips): add only active autocomplete option on selection
close #32204, #13574
1 parent 372d20b commit e7e71ef

File tree

6 files changed

+17
-48
lines changed

6 files changed

+17
-48
lines changed

goldens/material/chips/index.api.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,6 @@ export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck
132132
static ɵfac: i0.ɵɵFactoryDeclaration<MatChip, never>;
133133
}
134134

135-
// @public (undocumented)
136-
export class MatChipAutocompleteInput extends MatChipInput {
137-
_emitChipEnd(event?: KeyboardEvent): void;
138-
// (undocumented)
139-
static ɵdir: i0.ɵɵDirectiveDeclaration<MatChipAutocompleteInput, "input[matChipInputFor][matAutocomplete]", ["matChipInput", "matChipInputFor"], {}, {}, never, never, true, never>;
140-
// (undocumented)
141-
static ɵfac: i0.ɵɵFactoryDeclaration<MatChipAutocompleteInput, never>;
142-
}
143-
144135
// @public
145136
export class MatChipAvatar {
146137
// (undocumented)
@@ -297,7 +288,6 @@ export class MatChipInput implements MatChipTextControl, OnChanges, OnDestroy {
297288
protected _getReadonlyAttribute(): string | null;
298289
id: string;
299290
readonly inputElement: HTMLInputElement;
300-
protected _isSeparatorKey(event: KeyboardEvent): boolean;
301291
_keydown(event: KeyboardEvent): void;
302292
// (undocumented)
303293
static ngAcceptInputType_addOnBlur: unknown;
@@ -319,7 +309,7 @@ export class MatChipInput implements MatChipTextControl, OnChanges, OnDestroy {
319309
// (undocumented)
320310
setDescribedByIds(ids: string[]): void;
321311
// (undocumented)
322-
static ɵdir: i0.ɵɵDirectiveDeclaration<MatChipInput, "input[matChipInputFor]:not([matAutocomplete])", ["matChipInput", "matChipInputFor"], { "chipGrid": { "alias": "matChipInputFor"; "required": false; }; "addOnBlur": { "alias": "matChipInputAddOnBlur"; "required": false; }; "separatorKeyCodes": { "alias": "matChipInputSeparatorKeyCodes"; "required": false; }; "placeholder": { "alias": "placeholder"; "required": false; }; "id": { "alias": "id"; "required": false; }; "disabled": { "alias": "disabled"; "required": false; }; "readonly": { "alias": "readonly"; "required": false; }; "disabledInteractive": { "alias": "matChipInputDisabledInteractive"; "required": false; }; }, { "chipEnd": "matChipInputTokenEnd"; }, never, never, true, never>;
312+
static ɵdir: i0.ɵɵDirectiveDeclaration<MatChipInput, "input[matChipInputFor]", ["matChipInput", "matChipInputFor"], { "chipGrid": { "alias": "matChipInputFor"; "required": false; }; "addOnBlur": { "alias": "matChipInputAddOnBlur"; "required": false; }; "separatorKeyCodes": { "alias": "matChipInputSeparatorKeyCodes"; "required": false; }; "placeholder": { "alias": "placeholder"; "required": false; }; "id": { "alias": "id"; "required": false; }; "disabled": { "alias": "disabled"; "required": false; }; "readonly": { "alias": "readonly"; "required": false; }; "disabledInteractive": { "alias": "matChipInputDisabledInteractive"; "required": false; }; }, { "chipEnd": "matChipInputTokenEnd"; }, never, never, true, never>;
323313
// (undocumented)
324314
static ɵfac: i0.ɵɵFactoryDeclaration<MatChipInput, never>;
325315
}
@@ -551,7 +541,7 @@ export class MatChipsModule {
551541
// (undocumented)
552542
static ɵinj: i0.ɵɵInjectorDeclaration<MatChipsModule>;
553543
// (undocumented)
554-
static ɵmod: i0.ɵɵNgModuleDeclaration<MatChipsModule, never, [typeof MatRippleModule, typeof MatChipAction, typeof MatChip, typeof MatChipAvatar, typeof MatChipEdit, typeof MatChipEditInput, typeof MatChipGrid, typeof MatChipAutocompleteInput, typeof MatChipInput, typeof MatChipListbox, typeof MatChipOption, typeof MatChipRemove, typeof MatChipRow, typeof MatChipSet, typeof MatChipTrailingIcon], [typeof i2.BidiModule, typeof MatChip, typeof MatChipAvatar, typeof MatChipEdit, typeof MatChipEditInput, typeof MatChipGrid, typeof MatChipAutocompleteInput, typeof MatChipInput, typeof MatChipListbox, typeof MatChipOption, typeof MatChipRemove, typeof MatChipRow, typeof MatChipSet, typeof MatChipTrailingIcon]>;
544+
static ɵmod: i0.ɵɵNgModuleDeclaration<MatChipsModule, never, [typeof MatRippleModule, typeof MatChipAction, typeof MatChip, typeof MatChipAvatar, typeof MatChipEdit, typeof MatChipEditInput, typeof MatChipGrid, typeof MatChipInput, typeof MatChipListbox, typeof MatChipOption, typeof MatChipRemove, typeof MatChipRow, typeof MatChipSet, typeof MatChipTrailingIcon], [typeof i2.BidiModule, typeof MatChip, typeof MatChipAvatar, typeof MatChipEdit, typeof MatChipEditInput, typeof MatChipGrid, typeof MatChipInput, typeof MatChipListbox, typeof MatChipOption, typeof MatChipRemove, typeof MatChipRow, typeof MatChipSet, typeof MatChipTrailingIcon]>;
555545
}
556546

557547
// @public

src/components-examples/material/chips/chips-autocomplete/chips-autocomplete-example.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import {LiveAnnouncer} from '@angular/cdk/a11y';
22
import {COMMA, ENTER} from '@angular/cdk/keycodes';
33
import {ChangeDetectionStrategy, Component, computed, inject, model, signal} from '@angular/core';
44
import {FormsModule} from '@angular/forms';
5-
import {MatAutocompleteModule, MatAutocompleteSelectedEvent} from '@angular/material/autocomplete';
6-
import {MatChipInputEvent, MatChipsModule} from '@angular/material/chips';
5+
import {
6+
MatAutocompleteModule,
7+
type MatAutocompleteSelectedEvent,
8+
} from '@angular/material/autocomplete';
9+
import {type MatChipInputEvent, MatChipsModule} from '@angular/material/chips';
710
import {MatFormFieldModule} from '@angular/material/form-field';
811
import {MatIconModule} from '@angular/material/icon';
912

@@ -14,7 +17,9 @@ import {MatIconModule} from '@angular/material/icon';
1417
selector: 'chips-autocomplete-example',
1518
templateUrl: 'chips-autocomplete-example.html',
1619
styleUrl: 'chips-autocomplete-example.css',
17-
imports: [MatFormFieldModule, MatChipsModule, MatIconModule, MatAutocompleteModule, FormsModule],
20+
// Make sure to import `MatAutocompleteModule` before `MatChipsModule` to prevent adding typed
21+
// text when autocomplete option is selected via keyboard).
22+
imports: [MatFormFieldModule, MatAutocompleteModule, MatChipsModule, MatIconModule, FormsModule],
1823
changeDetection: ChangeDetectionStrategy.OnPush,
1924
})
2025
export class ChipsAutocompleteExample {

src/material/chips/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ ng_project(
100100
"//:node_modules/@angular/forms",
101101
"//:node_modules/rxjs",
102102
"//src:dev_mode_types",
103-
"//src/material/autocomplete",
104103
"//src/material/core",
105104
"//src/material/form-field",
106105
],

src/material/chips/chip-input.ts

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
inject,
2020
} from '@angular/core';
2121
import {_IdGenerator} from '@angular/cdk/a11y';
22-
import {MatAutocompleteTrigger} from '../autocomplete';
2322
import {MatFormField, MAT_FORM_FIELD} from '../form-field';
2423
import {MatChipsDefaultOptions, MAT_CHIPS_DEFAULT_OPTIONS, SeparatorKey} from './tokens';
2524
import {MatChipGrid} from './chip-grid';
@@ -46,7 +45,7 @@ export interface MatChipInputEvent {
4645
* May be placed inside or outside of a `<mat-chip-grid>`.
4746
*/
4847
@Directive({
49-
selector: 'input[matChipInputFor]:not([matAutocomplete])',
48+
selector: 'input[matChipInputFor]',
5049
exportAs: 'matChipInput, matChipInputFor',
5150
host: {
5251
// TODO: eventually we should remove `mat-input-element` from here since it comes from the
@@ -243,7 +242,7 @@ export class MatChipInput implements MatChipTextControl, OnChanges, OnDestroy {
243242
}
244243

245244
/** Checks whether a keycode is one of the configured separators. */
246-
protected _isSeparatorKey(event: KeyboardEvent): boolean {
245+
private _isSeparatorKey(event: KeyboardEvent): boolean {
247246
if (!this.separatorKeyCodes) {
248247
return false;
249248
}
@@ -277,29 +276,3 @@ export class MatChipInput implements MatChipTextControl, OnChanges, OnDestroy {
277276
return this.readonly || (this.disabled && this.disabledInteractive) ? 'true' : null;
278277
}
279278
}
280-
281-
/**
282-
* Directive that adds chip-specific behaviors to an autocomplete input element inside
283-
* `<mat-form-field>`.
284-
* May be placed inside or outside of a `<mat-chip-grid>`.
285-
*/
286-
@Directive({
287-
selector: 'input[matChipInputFor][matAutocomplete]',
288-
exportAs: 'matChipInput, matChipInputFor',
289-
})
290-
export class MatChipAutocompleteInput extends MatChipInput {
291-
private readonly _autocompleteTrigger = inject(MatAutocompleteTrigger);
292-
293-
/** Checks to see if the (chipEnd) event needs to be emitted. */
294-
override _emitChipEnd(event?: KeyboardEvent) {
295-
if (
296-
(!event || (super._isSeparatorKey(event) && !event.repeat)) &&
297-
this._autocompleteTrigger.autocomplete.isOpen &&
298-
this._autocompleteTrigger.activeOption
299-
) {
300-
event?.preventDefault();
301-
} else {
302-
super._emitChipEnd(event);
303-
}
304-
}
305-
}

src/material/chips/chips-module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {MAT_CHIPS_DEFAULT_OPTIONS, MatChipsDefaultOptions} from './tokens';
1414
import {MatChipEditInput} from './chip-edit-input';
1515
import {MatChipGrid} from './chip-grid';
1616
import {MatChipAvatar, MatChipEdit, MatChipRemove, MatChipTrailingIcon} from './chip-icons';
17-
import {MatChipAutocompleteInput, MatChipInput} from './chip-input';
17+
import {MatChipInput} from './chip-input';
1818
import {MatChipListbox} from './chip-listbox';
1919
import {MatChipRow} from './chip-row';
2020
import {MatChipOption} from './chip-option';
@@ -28,7 +28,6 @@ const CHIP_DECLARATIONS = [
2828
MatChipEdit,
2929
MatChipEditInput,
3030
MatChipGrid,
31-
MatChipAutocompleteInput,
3231
MatChipInput,
3332
MatChipListbox,
3433
MatChipOption,

src/material/chips/chips.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ Users can press delete to remove a chip. Pressing delete triggers the `removed`
6565

6666
A `<mat-chip-grid>` can be combined with `<mat-autocomplete>` to enable free-form chip input with suggestions.
6767

68+
> _Please note: when using `MatChipsModule` together with `MatAutocompleteModule`, the order in which modules are imported matters._
69+
> _To ensure correct behavior (e.g., preventing adding typed text when autocomplete option is selected via keyboard), make sure to import `MatAutocompleteModule` before `MatChipsModule`._
70+
6871
<!-- example(chips-autocomplete) -->
6972

7073
### Icons
@@ -141,7 +144,7 @@ The chips components support 3 user interaction patterns, each with its own cont
141144

142145
`<mat-chip-grid>` and `<mat-chip-row>` : These elements implement a grid accessibility pattern. Use them as part of a free form input that allows users to enter text to add chips.
143146

144-
Note : be sure to have the input element be a sibling of mat-chip-grid to ensure accessibility of the input element by accessibility devices such as Voice Control. It is also recommended to apply an appropriate `aria-label` to the input to optimize accessibility of the input.
147+
> _Please note: be sure to have the input element be a sibling of `mat-chip-grid` to ensure accessibility of the input element by accessibility devices such as Voice Control. It is also recommended to apply an appropriate `aria-label` to the input to optimize accessibility of the input._
145148
146149
```html
147150
<mat-form-field>

0 commit comments

Comments
 (0)