mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(autocomplete): do not trigger submit on ENTER (#3727)
* fix(autocomplete): do not trigger submit on ENTER When an option is selected with the ENTER key, it should not submit the form and prevent the default input behavior Fixes #3159 * test(autocomplete): removing extra detectChanges
This commit is contained in:
committed by
tinayuangao
parent
6d6c12dc16
commit
bedf5a1e42
@@ -205,6 +205,7 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
|
||||
_handleKeydown(event: KeyboardEvent): void {
|
||||
if (this.activeOption && event.keyCode === ENTER) {
|
||||
this.activeOption._selectViaInteraction();
|
||||
event.preventDefault();
|
||||
} else {
|
||||
this.autocomplete._keyManager.onKeydown(event);
|
||||
if (event.keyCode === UP_ARROW || event.keyCode === DOWN_ARROW) {
|
||||
|
||||
@@ -631,6 +631,20 @@ describe('MdAutocomplete', () => {
|
||||
});
|
||||
}));
|
||||
|
||||
it('should prevent the default enter key action', async(() => {
|
||||
fixture.whenStable().then(() => {
|
||||
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
|
||||
|
||||
fixture.whenStable().then(() => {
|
||||
spyOn(ENTER_EVENT, 'preventDefault');
|
||||
|
||||
fixture.componentInstance.trigger._handleKeydown(ENTER_EVENT);
|
||||
|
||||
expect(ENTER_EVENT.preventDefault).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
it('should fill the text field, not select an option, when SPACE is entered', async(() => {
|
||||
fixture.whenStable().then(() => {
|
||||
typeInElement('New', input);
|
||||
|
||||
Reference in New Issue
Block a user