forked from MicrosoftEdge/Demos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
173 lines (138 loc) · 5.78 KB
/
Copy pathscript.js
File metadata and controls
173 lines (138 loc) · 5.78 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// ---- Feature detection ----
const supportsBehaviors = typeof HTMLSubmitButtonBehavior !== "undefined";
if (!supportsBehaviors) {
document.getElementById("feature-warning").hidden = false;
}
// ---- Custom element definitions ----
if (supportsBehaviors) {
class MySubmitButton extends HTMLElement {
static formAssociated = true;
constructor() {
super();
const behavior = new HTMLSubmitButtonBehavior();
this.attachInternals({ behaviors: [behavior] });
}
}
customElements.define("my-submit-button", MySubmitButton);
class OverrideSubmitButton extends HTMLElement {
static formAssociated = true;
#behavior;
constructor() {
super();
this.#behavior = new HTMLSubmitButtonBehavior();
this.attachInternals({ behaviors: [this.#behavior] });
}
get behavior() { return this.#behavior; }
}
customElements.define("override-submit-button", OverrideSubmitButton);
// Set form override properties on the behavior instances.
const saveBtn = document.querySelector("#save-btn");
saveBtn.behavior.name = "action";
saveBtn.behavior.value = "save";
saveBtn.behavior.formMethod = "post";
const draftBtn = document.querySelector("#draft-btn");
draftBtn.behavior.name = "action";
draftBtn.behavior.value = "draft";
draftBtn.behavior.formMethod = "get";
class ImplicitSubmitButton extends HTMLElement {
static formAssociated = true;
constructor() {
super();
const behavior = new HTMLSubmitButtonBehavior();
this.attachInternals({ behaviors: [behavior] });
}
}
customElements.define("implicit-submit-button", ImplicitSubmitButton);
class A11ySubmitButton extends HTMLElement {
static formAssociated = true;
constructor() {
super();
const behavior = new HTMLSubmitButtonBehavior();
this.attachInternals({ behaviors: [behavior] });
}
}
customElements.define("a11y-submit-button", A11ySubmitButton);
}
// ===========================================================================
// Use Case 1: Basic Form Submission
// ===========================================================================
const formBasic = document.getElementById("form-basic");
const basicOutput = document.getElementById("basic-output");
const basicData = document.getElementById("basic-data");
formBasic.addEventListener("submit", (event) => {
event.preventDefault();
const data = new FormData(formBasic);
const entries = [...data.entries()];
const submitter = event.submitter;
let result = "Form entries:\n";
entries.forEach(([key, val]) => {
result += ` ${key} = ${val}\n`;
});
result += `\nSubmitter: <${submitter?.localName || "unknown"}>`;
if (submitter?.tagName && !submitter.tagName.includes("-")) {
result += " (native)";
} else if (submitter?.tagName) {
result += " (custom element)";
}
basicData.textContent = result;
basicOutput.hidden = false;
});
// ===========================================================================
// Use Case 2: Form Override Properties
// ===========================================================================
const formOverrides = document.getElementById("form-overrides");
const overridesOutput = document.getElementById("overrides-output");
const overridesData = document.getElementById("overrides-data");
formOverrides.addEventListener("submit", (event) => {
event.preventDefault();
const data = new FormData(formOverrides);
const entries = [...data.entries()];
const submitter = event.submitter;
let result = "Form entries:\n";
entries.forEach(([key, val]) => {
result += ` ${key} = ${val}\n`;
});
result += `\nSubmitter: <${submitter?.localName || "unknown"}>`;
result += `\nBehavior name: ${submitter?.behavior?.name || "(none)"}`;
result += `\nBehavior value: ${submitter?.behavior?.value || "(none)"}`;
result += `\nBehavior formMethod: ${submitter?.behavior?.formMethod || "(none)"}`;
overridesData.textContent = result;
overridesOutput.hidden = false;
});
// ===========================================================================
// Use Case 3: Implicit Submission
// ===========================================================================
const formImplicit = document.getElementById("form-implicit");
const implicitOutput = document.getElementById("implicit-output");
const implicitData = document.getElementById("implicit-data");
formImplicit.addEventListener("submit", (event) => {
event.preventDefault();
const data = new FormData(formImplicit);
const entries = [...data.entries()];
let result = "Form entries:\n";
entries.forEach(([key, val]) => {
result += ` ${key} = ${val}\n`;
});
result += `\nSubmitter: <${event.submitter?.localName || "unknown"}> (custom element)`;
implicitData.textContent = result;
implicitOutput.hidden = false;
});
// ===========================================================================
// Use Case 4: Accessibility and Keyboard Activation
// ===========================================================================
const formA11y = document.getElementById("form-a11y");
const a11yOutput = document.getElementById("a11y-output");
const a11yDataOutput = document.getElementById("a11y-data-output");
formA11y.addEventListener("submit", (event) => {
event.preventDefault();
const data = new FormData(formA11y);
const entries = [...data.entries()];
let result = "Form entries:\n";
entries.forEach(([key, val]) => {
result += ` ${key} = ${val}\n`;
});
result += `\nSubmitter: <${event.submitter?.localName || "unknown"}>`;
result += `\nSubmitter tabIndex: ${event.submitter?.tabIndex ?? "N/A"}`;
a11yDataOutput.textContent = result;
a11yOutput.hidden = false;
});