ionic-framework: bug: IonButton type="submit" does not submit form when enter pressed in inputs
Bug Report
Ionic version: [x] @ionic/react 4.9.0-rc.2
Current behavior:
IonButtons created with type="submit" do not submit the forms when a user presses the “enter” key in an input field.
You can see that the generated Shadow DOM input does NOT contain the type="submit" attribute.
Expected behavior:
Standard HTML buttons created with type="submit" will automatically attempt to submit the form when the user presses “enter” in an input field. For accessibility, IonButtons inside a form should do the same.
Steps to reproduce: In the form below, press the “enter” key and observe that the form does not attempt to submit. Related code:
<form onSubmit={handleSubmit1}>
<IonItem>
<IonLabel>Ionic Input</IonLabel>
<IonInput
name="ionicInput1"
id="ionicInput1"
placeholder="Press enter in this field"
/>
</IonItem>
<IonItem>
<label for="genericInput1">Generic Input</label>
<input
type="text"
name="genericInput1"
id="genericInput1"
placeholder="Press enter in this field"
/>
</IonItem>
<IonButton type="submit">Submit</IonButton>
</form>
Working sample that demonstrate a regular <button type="submit">Submit</button> does properly submit the form and that an IonButton type="submit"/> does not:
Other information: None
Ionic info:
Ionic:
Ionic CLI : 5.2.5 (/Users/jn/.nvm/versions/node/v10.16.3/lib/node_modules/ionic)
Ionic Framework : @ionic/react 4.9.0-rc.2
Utility:
cordova-res : not installed
native-run : not installed
System:
NodeJS : v10.16.3 (/Users/jn/.nvm/versions/node/v10.16.3/bin/node)
npm : 6.9.0
OS : macOS Mojave
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 10
- Comments: 26 (8 by maintainers)
Adding
type="submit"does affect the shadon-dom correctly for me - but it doesn’t allow the enter key to submit the form. A hidden input does work on the other hand. This feels wrong though.Issue already closed one year ago #15136
Using an
ion-buttonwithtype="submit"inside a form with only one input works. As soon as there is more than one input inside the form, it doesn’t work anymore. 🤔any updates?
Having the same problem with Ionic 5
Pressing enter doesnt fire the submit, clicking the button works
Should setting the type of the ionic button to submit not cause the same functionality as having a hidden submit input though?
This keeps happening for us, but strangely only for tactile events from touch screens or simulated touch screens through browser dev tools. It works fine for mouse clicks. Quite worrysome that the submit button in a mobile UI framework does not do its only job in a mobile device.
Maybe the easiest and least problematic fix is to not use shadow DOM for this component, the same as other form-related components like input?
I am able to reproduce this, and the bug does not seem to be limited to just Ionic React apps.
The reason why this is happening is buttons in the Shadow DOM are not picked up by the parent form. In this case, clicking the button and pressing “Enter” would normally not submit the form.
We added some code to submit the form when you click the button: https://github.com/ionic-team/ionic/blob/master/core/src/components/button/button.tsx#L155
The issue still persists for pressing “Enter”. I will look into adding a fix for this in Ionic Framework, but for now you can add a keyup listener on the form to work around this limitation.
this still an issue as of sep 2021
I’m having to fake a submit button inside the form as workaround
This seems to be an issue with the way WebComponents/shadow-dom work in general (they are working on a solution). Adding the hidden input field seems to work, albeit its pretty hacky. From a consistency perspective i would want Ionic button to work exactly the same as native button, so what if ionic adds the hidden input submit button outside the shadow DOM until the spec comes up with a permanent fix. This inconsistency should be abstracted away from the library consumer.
@liamdebeasi sir can you help us, we have same problem into ionic angular latest 5.2.2 also,
<ion-button color="danger" size="large" expand="block" type="submit" id="login_button" [disabled]="is_disable">SIGNIN <i class="fas fa-sign-in-alt"></i></ion-button>When click on button it submit form but if press enter button then form not submitted.
Thanks in advance
Yeah i am also confused. When i change
<ion-button type="submit">to<button type="submit">then it works well. I saw that this issue was already present in Ionic 4… Isnt it time to fix this? 😉I’m also experiencing this issue. Reading through #15136 is seems to suggest that this functionality only works when 1 input is on a form (which it does) and that is due to browsers implementation and not ionic. However, this does not explain why it works when you add a button with an input type of submit? If it was due to the browser it still shouldn’t work?