Skip to content

Commit

Permalink
fix(button)!: remove unnecessary property preventClickDefault
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 542958395
  • Loading branch information
AndrewJakubowicz authored and copybara-github committed Jun 23, 2023
1 parent cf5d893 commit 9244524
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
73 changes: 73 additions & 0 deletions button/filled-button_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// import 'jasmine'; (google3-only)

import {html} from 'lit';

import {Environment} from '../testing/environment.js';
import {createTokenTests} from '../testing/tokens.js';

import {MdFilledButton} from './filled-button.js';
import {ButtonHarness} from './harness.js';

describe('<md-filled-button>', () => {
const env = new Environment();

describe('.styles', () => {
createTokenTests(MdFilledButton.styles);
});

describe('form associated', () => {
async function setupTest() {
const root =
env.render(html`<form><md-filled-button></md-filled-button></form>`);
const element = root.querySelector('md-filled-button');
if (!element) {
throw new Error(`Could not query rendered <md-filled-button>`);
}
const form = root.querySelector('form');
if (!form) throw new Error(`Could not query form`);

await env.waitForStability();

return {harness: new ButtonHarness(element), form};
}

it('button with type submit can submit a form', async () => {
const {harness, form} = await setupTest();
harness.element.type = 'submit';

spyOn(form, 'requestSubmit');
spyOn(form, 'reset');
await harness.clickWithMouse();

expect(form.requestSubmit).toHaveBeenCalled();
expect(form.reset).not.toHaveBeenCalled();
});

it('button with type reset can reset a form', async () => {
const {harness, form} = await setupTest();
harness.element.type = 'reset';

spyOn(form, 'requestSubmit');
spyOn(form, 'reset');
await harness.clickWithMouse();

expect(form.requestSubmit).not.toHaveBeenCalled();
expect(form.reset).toHaveBeenCalled();
});

it('submit can be cancelled with preventDefault', async () => {
const {harness, form} = await setupTest();
harness.element.type = 'submit';

spyOn(form, 'requestSubmit');

harness.element.addEventListener('click', e => {
e.preventDefault();
}, {once: true});

await harness.clickWithMouse();

expect(form.requestSubmit).not.toHaveBeenCalled();
});
});
});
10 changes: 0 additions & 10 deletions button/lib/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ export abstract class Button extends LitElement {
*/
@property({type: Boolean, attribute: 'has-icon'}) hasIcon = false;

/**
* Whether `preventDefault()` should be called on the underlying button.
* Useful for preventing certain native functionalities like preventing form
* submissions.
*/
@property({type: Boolean}) preventClickDefault = false;

/**
* Specifies the type of button, used for controlling forms. When type
* is `submit`, the containing form is submitted; when it is `reset` the
Expand Down Expand Up @@ -190,9 +183,6 @@ export abstract class Button extends LitElement {
if (this.isRedispatchingEvent) {
return;
}
if (this.preventClickDefault) {
e.preventDefault();
}
// based on type, trigger form action.
const {type, internals: {form}} = this;
if (!form) {
Expand Down

0 comments on commit 9244524

Please sign in to comment.