Skip to content

Commit

Permalink
feat: reset and submit support for <utrecht-button>
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbert committed Sep 4, 2021
1 parent 64dac3b commit e1319e5
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion components/button/stencil.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Prop, h } from "@stencil/core";
import { Component, Element, Event, EventEmitter, Prop, h } from "@stencil/core";
import clsx from "clsx";

@Component({
Expand All @@ -8,14 +8,40 @@ import clsx from "clsx";
})
export class Button {
@Prop() disabled: boolean;
@Event({ cancelable: true }) utrechtRequestReset: EventEmitter;
@Event({ cancelable: true }) utrechtRequestSubmit: EventEmitter;
@Element() host: HTMLElement;
@Prop() type: string;

render() {
const handleReset = () => {
const event = this.utrechtRequestReset.emit();
if (!event.defaultPrevented) {
this.host?.closest("form")?.reset();
}
};

const handleSubmit = () => {
const event = this.utrechtRequestSubmit.emit();
if (!event.defaultPrevented) {
this.host?.closest("form")?.requestSubmit();
}
};

const handleClick = () => {
if (this.type === "reset") {
handleReset();
} else if (this.type === "submit") {
handleSubmit();
}
};

return (
<button
class={clsx("utrecht-button", this.disabled && "utrecht-button--disabled")}
disabled={this.disabled}
type={this.type || "button"}
onClick={handleClick}
>
<slot></slot>
</button>
Expand Down

0 comments on commit e1319e5

Please sign in to comment.