-
Notifications
You must be signed in to change notification settings - Fork 909
/
Copy pathform-submitter.ts
130 lines (116 loc) · 3.58 KB
/
form-submitter.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
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {isServer, ReactiveElement} from 'lit';
import {
internals,
WithElementInternals,
} from '../../labs/behaviors/element-internals.js';
/**
* A string indicating the form submission behavior of the element.
*
* - submit: The element submits the form. This is the default value if the
* attribute is not specified, or if it is dynamically changed to an empty or
* invalid value.
* - reset: The element resets the form.
* - button: The element does nothing.
*/
export type FormSubmitterType = 'button' | 'submit' | 'reset';
/**
* An element that can submit or reset a `<form>`, similar to
* `<button type="submit">`.
*/
export interface FormSubmitter extends ReactiveElement, WithElementInternals {
/**
* A string indicating the form submission behavior of the element.
*
* - submit: The element submits the form. This is the default value if the
* attribute is not specified, or if it is dynamically changed to an empty or
* invalid value.
* - reset: The element resets the form.
* - button: The element does nothing.
*/
type: FormSubmitterType;
/**
* The HTML name to use in form submission. When combined with a `value`, the
* submitting button's name/value will be added to the form.
*
* Names must reflect to a `name` attribute for form integration.
*/
name: string;
/**
* The value of the button. When combined with a `name`, the submitting
* button's name/value will be added to the form.
*/
value: string;
}
type FormSubmitterConstructor =
| (new () => FormSubmitter)
| (abstract new () => FormSubmitter);
/**
* Sets up an element's constructor to enable form submission. The element
* instance should be form associated and have a `type` property.
*
* A click listener is added to each element instance. If the click is not
* default prevented, it will submit the element's form, if any.
*
* @example
* ```ts
* class MyElement extends mixinElementInternals(LitElement) {
* static {
* setupFormSubmitter(MyElement);
* }
*
* static formAssociated = true;
*
* type: FormSubmitterType = 'submit';
* }
* ```
*
* @param ctor The form submitter element's constructor.
*/
export function setupFormSubmitter(ctor: FormSubmitterConstructor) {
if (isServer) {
return;
}
(ctor as unknown as typeof ReactiveElement).addInitializer((instance) => {
const submitter = instance as FormSubmitter;
submitter.addEventListener('click', async (event) => {
const {type, [internals]: elementInternals} = submitter;
const {form} = elementInternals;
if (!form || type === 'button') {
return;
}
// Wait a full task for event bubbling to complete.
await new Promise<void>((resolve) => {
setTimeout(resolve);
});
if (event.defaultPrevented) {
return;
}
if (type === 'reset') {
form.reset();
return;
}
// form.requestSubmit(submitter) does not work with form associated custom
// elements. This patches the dispatched submit event to add the correct
// `submitter`.
// See https://github.com/WICG/webcomponents/issues/814
form.addEventListener(
'submit',
(submitEvent) => {
Object.defineProperty(submitEvent, 'submitter', {
configurable: true,
enumerable: true,
get: () => submitter,
});
},
{capture: true, once: true},
);
elementInternals.setFormValue(submitter.value);
form.requestSubmit();
});
});
}