Skip to content

Commit

Permalink
fix(button): submit forms using fake button
Browse files Browse the repository at this point in the history
fixes #14890
fixes #14786
  • Loading branch information
manucorporat committed Jul 30, 2018
1 parent b0ed426 commit c05d672
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
36 changes: 17 additions & 19 deletions core/src/components/button/button.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Component, Element, Event, EventEmitter, Prop, State } from '@stencil/core';

import { Color, CssClassMap, Mode, RouterDirection } from '../../interface';
import { getParentNode, openURL } from '../../utils/theme';
import { hasShadowDom } from '../../utils/helpers';
import { openURL } from '../../utils/theme';

@Component({
tag: 'ion-button',
Expand Down Expand Up @@ -121,28 +122,25 @@ export class Button {
}

onClick(ev: Event) {
if (this.type === 'submit') {
if (this.type === 'button') {
openURL(this.win, this.href, ev, this.routerDirection);

} else if (hasShadowDom(this.el)) {
// this button wants to specifically submit a form
// climb up the dom to see if we're in a <form>
// and if so, then use JS to submit it

let node = this.el;
while ((node = getParentNode(node))) {
if (node.nodeName.toLowerCase() === 'form') {
// cool, this submit button is within a <form>, let's submit it

// prevent the button's default and stop it's propagation
ev.preventDefault();
ev.stopPropagation();

// submit the <form> via JS
(node as HTMLFormElement).submit();
break;
}
const form = this.el.closest('form');
if (form) {
ev.preventDefault();
ev.stopPropagation();

const fakeButton = document.createElement('button');
fakeButton.type = this.type;
fakeButton.style.display = 'none';
form.appendChild(fakeButton);
fakeButton.click();
fakeButton.remove();
}

} else {
openURL(this.win, this.href, ev, this.routerDirection);
}
}

Expand Down
19 changes: 17 additions & 2 deletions core/src/components/button/test/form/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

<ion-content id="content" padding no-bounce text-center>

<form action="http://httpbin.org/get" method="GET">
<form onsubmit="return validate(event)" action="http://httpbin.org/get" method="GET">

<div>
<input name="name">
<input name="name" required>
</div>

<ion-button type="submit">
Expand All @@ -36,6 +36,21 @@

</ion-app>

<script>
document.querySelector('form').addEventListener('submit', (event) => {
console.log('SUBMIT from event', event);
});
function validate(event) {
console.log('SUBMIT from attribute', event);
if (event.target.elements[0].value === 'admin') {
return true;
} else {
console.log('INCORRECT USER, use "admin"')
return false;
}
}
</script>

</body>

</html>
6 changes: 5 additions & 1 deletion core/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ export function reorderArray(array: any[], indexes: {from: number, to: number}):
return array;
}

export function hasShadowDom(el: HTMLElement) {
return !!el.shadowRoot && !!el.attachShadow;
}

export function renderHiddenInput(container: HTMLElement, name: string, value: string, disabled: boolean) {
if (container.shadowRoot) {
if (hasShadowDom(container)) {
let input = container.querySelector('input.aux-input') as HTMLInputElement;
if (!input) {
input = container.ownerDocument.createElement('input');
Expand Down

0 comments on commit c05d672

Please sign in to comment.