-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
Implicit form submit doesn't work in Ionic 4 #15682
Comments
As a temporary workaround this kind of works (with form: HTMLFormElement;
@Listen('keydown.enter')
handleEnterKey(e: KeyboardEvent) {
if ((e.target as HTMLIonInputElement).name === 'password') {
(this.form
.querySelector('ion-button[type="submit"]')
.shadowRoot.querySelector('button[type="submit"]') as HTMLButtonElement).click();
}
} Calling As a side note, the browser's built-in form validation (e. g. |
I was about to open this same issue. I traced the problem down to the It seems that the fake button technique isn't working as expected in Chrome and Safari. It works on Firefox though. The implicit submit was working on previous betas before turning on the Shadow DOM, so it may be related. Replacing the |
@FdezRomero thanks for the additional info, so it might be that the inputs lose the form context because each input lives in a separate Shadow DOM? Just did a search for it and found this: WICG/webcomponents#649. I think I had tried using a regular button but I only got it to work when also replacing the Is there a way of turning off Shadow DOM for a component including all it's child elements? I tried the |
@simonhaenisch I mentioned the change to Shadow DOM not as the cause, but as a point in time where it started happening in case it helps. If you look at the code I linked before, the Maybe @manucorporat can give us more info to submit a PR 😉 Edit: Now that I think of it, if the hidden button is only created and removed when the |
So I was talking with Mike from Ionic and learnt that the implicit form submission only works with both native inputs and a native submit input/button, as they have the necessary internal JS in the browser for it to work. Since these native elements are now placed inside the Shadow DOM of the custom elements, this platform feature won't work. The behavior can be replicated in Angular by using the <form [formGroup]="myForm" (ngSubmit)="submit()" (keyup.enter)="submit()"> |
So there's a few things going on here.
So vanilla use-case where things work <form onsubmit="checkData(event)">
<input type="text" required>
<input type="password" required>
<input type="submit">
</form> In this case, the form and inputs will submit on enter because of the built in HTML FormValidation/handling. This is something built into the browser, just as long as the form is valid and there is a valid handler for
Use case where things do not work
Why this does not work is a mixture of Custom Elements and Shadow DOM encapsulation. Since the native inputs in ion-input are encapsulated with shadow DOM, the built in browser form validation will not be able to work here. Also, because the ion-input essentially extend a base
This worked in earlier release and in V3 because we did not use shadow DOM, so the native inputs were reachable by the browser own built-in functionality. Ways to deal with this: In a framework landscape, like Angular, you'd have a keyup handler to list for enter. In vanilla JS, you'd have to wire that logic up yourself. |
Fixes ionic-team#15682 Pressing enter key on input & textarea now submits the form Also fixed one build issue regarding a type error related to `SelectInputChangeEvent`
One more thing that I just remember I was actually trying to get to work is to enable the proper buttons on the mobile keyboard (i. e. the "next" button to jump to the next input on iOS). So in order to get that to work I will probably need to write my own form component, because hooking up some key event listeners won't fix that... Edit: same thing for |
@mhartington would it be possible to expose the <ion-input shadow={false}></ion-input> I know it's not super trivial, but I really don't see a point in enabling Shadow DOM for this if there is so many built-in benefits being lost. Or as an alternative, add a Or maybe a better way to go is to add a |
+1, As pointed out by @simonhaenisch , the native keyboard behavior is totally different, so a framework-based workaround is needed in order to save the UX with forms. |
@FdezRomero + @simonhaenisch solutions solve the problem |
Related to this problem, our designers have defined some screens where the submit
To make this
Then, on the click event of the header ion-button we trigger a click in the hidden submit button to run the ngSubmit function. It's not a really ellegant workaround.
HTML button spec provides a It would be nice
|
I close this because as mentioned it has to do with shadow dom. Just create a method for it and use (keyup.enter) in Angular |
I think that a more permanent solution may be in order -- this is standard / expected behavior. This would require implementing a |
@paulstelzer I would also want this to be reopened, and might be possible to solve with an |
@simonhaenisch Thanks for your response. I will not open this issue because we already have two which seperate this issues very well: #15136: Button does not trigger ngSubmit So it's not useful to have different issues opened for the same topic. Is that okay for you :) |
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out. |
Bug Report
@ionic/[email protected]
@stencil/[email protected]
Describe the Bug
I created a login form (started with the Ionic Stencil PWA Starter) using
form
,ion-input
andion-button
. When I try to implicitly submit the form by pressing enter in one of the inputs, the form does not get submitted, despite my button having thetype="submit"
attribute set.Steps to Reproduce
Related Code
Fiddle: http://jsfiddle.net/ok8zn5e6/2/
Expected Behavior
The implicit form submit should work because the form contains a button with
type="submit"
.Additional Context
http://stonefishy.github.io/blog/2015/06/30/implicit-submission-of-form-when-pressing-enter-key/
The text was updated successfully, but these errors were encountered: