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

fix(vwc-button): multiple click events generated on button #1101

Merged
merged 14 commits into from
Oct 20, 2021
Merged
8 changes: 6 additions & 2 deletions components/button/src/vwc-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class VWCButton extends MWCButton {
// return nothing
}

protected _handleClick(): void {
protected _handleClick(event: MouseEvent): void {
if (this.form) {
switch (this.getAttribute('type')) {
case 'reset':
Expand All @@ -143,7 +143,11 @@ export class VWCButton extends MWCButton {
case 'button':
break;
default:
this.#_hiddenButton.click();
if (event.target === this) {
this.#_hiddenButton.click();
} else {
event.stopImmediatePropagation();
}
break;
}
}
Expand Down
30 changes: 30 additions & 0 deletions components/button/test/button.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@ describe('button', () => {
expect(submitted).to.equal(true);
});

it('should emit a click event only once when inside a form', async function () {
let clicked = 0;
const addedElements = addElement(
textToDomToParent(
`<form onsubmit="return false" name="testForm" id="testForm"><${COMPONENT_NAME} type="submit">Button Text</${COMPONENT_NAME}></form>`
)
);
await waitNextTask();
const formElement = addedElements[0];
const actualElement = formElement.firstChild;
actualElement.addEventListener('click', () => (clicked++));

actualElement.click();
expect(clicked).to.equal(1);
});

it('should emit a click event only once when outside a form', async function () {
let clicked = 0;
const [actualElement] = addElement(
textToDomToParent(
`<${COMPONENT_NAME} type="submit">Button Text</${COMPONENT_NAME}>`
)
);
await waitNextTask();
actualElement.addEventListener('click', () => (clicked++));

actualElement.click();
expect(clicked).to.equal(1);
});

it('should reset form when of type reset', async function () {
let submitted = false;
let reset = false;
Expand Down