Skip to content

Commit

Permalink
fix(button): submit type works (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
muratcorlu authored Feb 9, 2023
1 parent d3b58ed commit 2aacce9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/components/button/bl-button.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,18 @@ describe('bl-button', () => {
expect(ev.detail).to.be.equal('Click event fired!');
});
});
describe('Form Participation', () => {
it('submits wrapping form if type is submit', async () => {
const form = await fixture<HTMLFormElement>(html`<form>
<bl-button type="submit">button</bl-button>
</form>`);
form.addEventListener('submit', (e) => e.preventDefault());

const button = form.querySelector('bl-button')?.shadowRoot?.querySelector('button');

setTimeout(() => button?.click());
const ev = await oneEvent(form, 'submit');
expect(ev).to.exist;
});
});
});
12 changes: 12 additions & 0 deletions src/components/button/bl-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CSSResultGroup, html, LitElement, TemplateResult } from 'lit';
import { customElement, property, state, query } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { submit } from '@open-wc/form-helpers';
import { event, EventDispatcher } from '../../utilities/event';
import style from './bl-button.css';
import '../icon/bl-icon';
Expand Down Expand Up @@ -103,12 +104,23 @@ export default class BlButton extends LitElement {
return this.active;
}

private form: HTMLFormElement | null;

connectedCallback() {
super.connectedCallback();
this.form = this.closest('form');
}

private caretTemplate(): TemplateResult {
return html` <bl-icon class="open" name="arrow_up"></bl-icon>
<bl-icon class="close" name="arrow_down"></bl-icon>`;
}

private _handleClick() {
if (this.type === 'submit' && this.form) {
submit(this.form);
}

this.onClick('Click event fired!');
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/input/bl-input.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ Input component uses [ElementInternals](https://developer.mozilla.org/en-US/docs
<bl-input name="name" label="Your Name"></bl-input>
<bl-input name="age" type="number" required min="18" label="Age"></bl-input>

<button type="submit">Submit</button>
<bl-button type="submit">Submit</bl-button>
</form>

<script>
Expand Down
2 changes: 1 addition & 1 deletion src/components/textarea/bl-textarea.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ When you use `bl-textarea` within a form with a `name` attribute, textarea's val
<form novalidate>
<bl-textarea name="name" required></bl-textarea>

<button type="submit">Submit</button>
<bl-button type="submit">Submit</bl-button>
</form>

<script>
Expand Down

0 comments on commit 2aacce9

Please sign in to comment.