-
-
Notifications
You must be signed in to change notification settings - Fork 787
/
form.ts
159 lines (139 loc) · 6.3 KB
/
form.ts
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
import './formdata-event-polyfill';
import type SlButton from '../components/button/button';
import type { ReactiveController, ReactiveControllerHost } from 'lit';
const reportValidityOverloads: WeakMap<HTMLFormElement, () => boolean> = new WeakMap();
export interface FormSubmitControllerOptions {
/** A function that returns the form containing the form control. */
form: (input: unknown) => HTMLFormElement | null;
/** A function that returns the form control's name, which will be submitted with the form data. */
name: (input: unknown) => string;
/** A function that returns the form control's current value. */
value: (input: unknown) => unknown | unknown[];
/** A function that returns the form control's current disabled state. If disabled, the value won't be submitted. */
disabled: (input: unknown) => boolean;
/**
* A function that maps to the form control's reportValidity() function. When the control is invalid, this will
* prevent submission and trigger the browser's constraint violation warning.
*/
reportValidity: (input: unknown) => boolean;
}
export class FormSubmitController implements ReactiveController {
host?: ReactiveControllerHost & Element;
form?: HTMLFormElement | null;
options: FormSubmitControllerOptions;
constructor(host: ReactiveControllerHost & Element, options?: Partial<FormSubmitControllerOptions>) {
(this.host = host).addController(this);
this.options = {
form: (input: HTMLInputElement) => input.closest('form'),
name: (input: HTMLInputElement) => input.name,
value: (input: HTMLInputElement) => input.value,
disabled: (input: HTMLInputElement) => input.disabled,
reportValidity: (input: HTMLInputElement) => {
return typeof input.reportValidity === 'function' ? input.reportValidity() : true;
},
...options
};
this.handleFormData = this.handleFormData.bind(this);
this.handleFormSubmit = this.handleFormSubmit.bind(this);
this.reportFormValidity = this.reportFormValidity.bind(this);
}
hostConnected() {
this.form = this.options.form(this.host);
if (this.form) {
this.form.addEventListener('formdata', this.handleFormData);
this.form.addEventListener('submit', this.handleFormSubmit);
// Overload the form's reportValidity() method so it looks at Shoelace form controls
if (!reportValidityOverloads.has(this.form)) {
reportValidityOverloads.set(this.form, this.form.reportValidity);
this.form.reportValidity = () => this.reportFormValidity();
}
}
}
hostDisconnected() {
if (this.form) {
this.form.removeEventListener('formdata', this.handleFormData);
this.form.removeEventListener('submit', this.handleFormSubmit);
// Remove the overload and restore the original method
if (reportValidityOverloads.has(this.form)) {
this.form.reportValidity = reportValidityOverloads.get(this.form)!;
reportValidityOverloads.delete(this.form);
}
this.form = undefined;
}
}
handleFormData(event: FormDataEvent) {
const disabled = this.options.disabled(this.host);
const name = this.options.name(this.host);
const value = this.options.value(this.host);
if (!disabled && typeof name === 'string' && typeof value !== 'undefined') {
if (Array.isArray(value)) {
(value as unknown[]).forEach(val => {
event.formData.append(name, (val as string | number | boolean).toString());
});
} else {
event.formData.append(name, (value as string | number | boolean).toString());
}
}
}
handleFormSubmit(event: Event) {
const disabled = this.options.disabled(this.host);
const reportValidity = this.options.reportValidity;
if (this.form && !this.form.noValidate && !disabled && !reportValidity(this.host)) {
event.preventDefault();
event.stopImmediatePropagation();
}
}
reportFormValidity() {
//
// Shoelace form controls work hard to act like regular form controls. They support the Constraint Validation API
// and its associated methods such as setCustomValidity() and reportValidity(). However, the HTMLFormElement also
// has a reportValidity() method that will trigger validation on all child controls. Since we're not yet using
// ElementInternals, we need to overload this method so it looks for any element with the reportValidity() method.
//
// We preserve the original method in a WeakMap, but we don't call it from the overload because that would trigger
// validations in an unexpected order. When the element disconnects, we revert to the original behavior. This won't
// be necessary once we can use ElementInternals.
//
// Note that we're also honoring the form's novalidate attribute.
//
if (this.form && !this.form.noValidate) {
// This seems sloppy, but checking all elements will cover native inputs, Shoelace inputs, and other custom
// elements that support the constraint validation API.
const elements = this.form.querySelectorAll<HTMLInputElement>('*');
for (const element of elements) {
if (typeof element.reportValidity === 'function') {
if (!element.reportValidity()) {
return false;
}
}
}
}
return true;
}
/** Submits the form, triggering validation and form data injection. */
submit(submitter?: HTMLInputElement | SlButton) {
// Calling form.submit() bypasses the submit event and constraint validation. To prevent this, we can inject a
// native submit button into the form, "click" it, then remove it to simulate a standard form submission.
if (this.form) {
const button = document.createElement('button');
button.type = 'submit';
button.style.position = 'absolute';
button.style.width = '0';
button.style.height = '0';
button.style.clipPath = 'inset(50%)';
button.style.overflow = 'hidden';
button.style.whiteSpace = 'nowrap';
// Pass form attributes through to the temporary button
if (submitter) {
['formaction', 'formmethod', 'formnovalidate', 'formtarget'].forEach(attr => {
if (submitter.hasAttribute(attr)) {
button.setAttribute(attr, submitter.getAttribute(attr)!);
}
});
}
this.form.append(button);
button.click();
button.remove();
}
}
}