-
Notifications
You must be signed in to change notification settings - Fork 0
/
log-form-element.js
43 lines (35 loc) · 954 Bytes
/
log-form-element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
export class LogFormElement extends HTMLElement {
static styleSheet = createStyleSheet(`
:host {
display: contents;
}
`);
constructor() {
super();
this.attachShadow({ mode: "open" });
this.shadowRoot.adoptedStyleSheets = [this.constructor.styleSheet];
this.shadowRoot.innerHTML = "<slot></slot>";
this.addEventListener("submit", this, true);
}
handleEvent(event) {
if (event.type === "submit" && event.target.matches("form")) {
event.preventDefault();
const formData = new FormData(event.target);
console.log(Array.from(formData));
}
}
static define(tagName = "log-form") {
if (!window.customElements.get(tagName)) {
window[this.name] = this;
window.customElements.define(tagName, this);
}
}
}
function createStyleSheet(styles) {
const sheet = new CSSStyleSheet();
sheet.replaceSync(styles);
return sheet;
}
if (new URL(import.meta.url).searchParams.has("define")) {
LogFormElement.define();
}