[MatChipList] Disable last chip focus when pressing BACKSPACE with empty input
See original GitHub issueFeature Description
By default, when you have chips
, and your input is empty and focused,
When your press BACKSPACE
, the last chip will be selected.
The feature I’m looking for is that this behaviour will be behind a flag, which would obviously be enabled
/disabled
by default (whatever keeps the current behaviour the default), and when the flag is toggled - pressing BACKSPACE
when the input is empty would not select the last chip.
Use Case
The use case for this behaviour is when you have chips, and the user deletes whatever input he put in so far, eventually when the input is cleared, the chips will get focused and will be deleted.
This something that may be problematic for some users in some cases, and as far as I can see, it’s not something that should be too hard to implement.
Side Note
I’ve been looking at docs and code to try to disable this behaviour myself, but I couldn’t find a way (without changing code).
I tried calling preventDefault
and stopPropagation
on the events from keyup
, keydown
and input
events on the input[matChipInputFor]
input, but I guess that the MatChipList
subscription to those events takes precedence over the user subscription (probably due to subscription order), and calling preventDefault
/stopPropagation
on the event is just too late.
If you can help find a way to achieve this behaviour without changing the component, this would help me a lot.
Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:10 (4 by maintainers)
Top GitHub Comments
The solution described at StackOverflow worked for me, currently using Angular 8.
HTML
<input type="text" [matAutocomplete]="auto" [matChipInputFor]="chipList" (keydown.backspace)="onBackspaceKeydown($event)">
TS
public onBackspaceKeydown(event) { event.stopImmediatePropagation(); }
Yeah, that’s what I had in mind