Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form Submission on Enter #17

Closed
RaghulXander opened this issue Sep 3, 2018 · 4 comments
Closed

Form Submission on Enter #17

RaghulXander opened this issue Sep 3, 2018 · 4 comments

Comments

@RaghulXander
Copy link

Use Keyboard to navigate options and press enter immediately Form is get Submitted

  1. Possible solutions are avoiding Enter keypress but i need enter key press too.
@sChupin
Copy link

sChupin commented Nov 13, 2018

I have the same needs. This stackoverflow post helped me: https://stackoverflow.com/questions/11388251/google-autocomplete-enter-to-select.

@chubbard
Copy link

chubbard commented Nov 30, 2018

This should be an open issue because it's easy to fix if you are inside the Google directive. I'd argue really this should be fixed inside the underlying Google component because it'll happen for them as well outside of angular. But this is what I did to work around it. Inside the component holding the form element I looked for the enter key bubbling up. I'm checking where the key event came from so that hitting enter would still allow the form to be submitted if used in other fields, but if used on the places component it won't submit the form automatically.

    @ViewChild( 'street', { read: ElementRef} ) streetElem : ElementRef;

    @HostListener('keydown', ['$event'])
    handleKeyboardEvent( event : KeyboardEvent ) {
        // this prevents places component from submitting the form when using enter
        let key = event.key.toLowerCase();
        if( key == 'enter' && event.target === this.streetElem.nativeElement ) {
            event.preventDefault();
            event.stopPropagation();
        }
    }

skynet2 added a commit that referenced this issue Dec 11, 2018
@skynet2
Copy link
Owner

skynet2 commented Dec 11, 2018

Fixed in 2.0.3

@skynet2 skynet2 closed this as completed Dec 11, 2018
@hksix
Copy link

hksix commented Apr 5, 2019

@skynet2 With Chrome's built in autocomplete the code submitted in 2.0.3 will produce an error when the user clicks on the address suggested by the browser.
image

This can be fixed by wrapping the code in a if( "key" in event ){}
I believe the code snippet below should resolve this issue.

@HostListener('keydown', ['$event'])
	handleKeyboardEvent( event : KeyboardEvent ) {
		// this prevents places component from submitting the form when using enter
		if( "key" in event ){
			let key = event.key.toLowerCase();
			if( key == 'enter' && event.target === this.streetElem.nativeElement ) {
				event.preventDefault();
				event.stopPropagation();
			}
		}
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants