@lookwe/lui
Version:
Lookwe UI web components
57 lines (54 loc) • 3.07 kB
JavaScript
import { __classPrivateFieldSet, __classPrivateFieldGet } from 'tslib';
import { internals } from '@lookwe/lit-mixins/element-internals';
import { nextMacrotask } from '@lookwe/utils/event-loop';
var _FormSubmitterController_instances, _FormSubmitterController_host, _FormSubmitterController_clearController, _FormSubmitterController_handleClick;
/**
* A controller that handles click to submit form.
*/
class FormSubmitterController {
constructor(host) {
_FormSubmitterController_instances.add(this);
_FormSubmitterController_host.set(this, void 0);
_FormSubmitterController_clearController.set(this, void 0);
(__classPrivateFieldSet(this, _FormSubmitterController_host, host, "f")).addController(this);
}
hostConnected() {
__classPrivateFieldSet(this, _FormSubmitterController_clearController, __classPrivateFieldGet(this, _FormSubmitterController_clearController, "f") ?? new AbortController(), "f");
const signal = __classPrivateFieldGet(this, _FormSubmitterController_clearController, "f").signal;
__classPrivateFieldGet(this, _FormSubmitterController_host, "f").addEventListener('click', __classPrivateFieldGet(this, _FormSubmitterController_instances, "m", _FormSubmitterController_handleClick).bind(this), { signal });
}
hostDisconnected() {
__classPrivateFieldGet(this, _FormSubmitterController_clearController, "f")?.abort();
__classPrivateFieldSet(this, _FormSubmitterController_clearController, undefined, "f");
}
}
_FormSubmitterController_host = new WeakMap(), _FormSubmitterController_clearController = new WeakMap(), _FormSubmitterController_instances = new WeakSet(), _FormSubmitterController_handleClick = async function _FormSubmitterController_handleClick(event) {
const { [internals]: elementInternals, type } = __classPrivateFieldGet(this, _FormSubmitterController_host, "f");
const { form } = elementInternals;
if (!form || type === 'button')
return;
// Wait a full task for event bubbling to complete.
await nextMacrotask();
if (event.defaultPrevented)
return;
switch (type) {
case 'reset':
form.reset();
break;
case 'submit':
// 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: () => __classPrivateFieldGet(this, _FormSubmitterController_host, "f"),
});
}, { capture: true, once: true });
elementInternals.setFormValue(__classPrivateFieldGet(this, _FormSubmitterController_host, "f").value);
form.requestSubmit();
}
};
export { FormSubmitterController };