sinple function + fix for radio

pull/9930/head
adiguba 3 years ago
parent be90d2e34d
commit bc7d36edad

@ -1,4 +1,3 @@
import { run_all } from '../common.js';
import { current_hydration_fragment, get_hydration_fragment } from './hydration.js';
import { get_descriptor } from './utils.js';
@ -86,19 +85,20 @@ export function init_operations() {
element_prototype.__className = '';
// @ts-expect-error
HTMLInputElement.prototype.__binds = undefined;
HTMLInputElement.prototype.__bind = undefined;
// @ts-expect-error
HTMLSelectElement.prototype.__binds = undefined;
HTMLSelectElement.prototype.__bind = undefined;
// @ts-expect-error
HTMLTextAreaElement.prototype.__binds = undefined;
HTMLTextAreaElement.prototype.__bind = undefined;
// On form's reset, invoke bindings on elements
document.body.addEventListener('reset', (evt)=> {
requestAnimationFrame(() => {
if (evt.defaultPrevented) return;
for (const e of (/**@type {HTMLFormElement} */(evt.target)).elements) {
// @ts-expect-error
if (e.__binds) run_all(e.__binds);
if (!evt.defaultPrevented) {
for (const e of (/**@type {HTMLFormElement} */(evt.target)).elements) {
// @ts-expect-error
e.__bind && e.__bind();
}
}
});
});

@ -66,15 +66,23 @@ const all_registerd_events = new Set();
const root_event_handles = new Set();
/**
* Add the function on the __binds attribute of the element
* Add the function on the __bind attribute of the element
* This allow to handle form's reset correctly
* @param {Element} dom
* @param {(this:Element)=>void} fn
* @param {()=>void} fn
*/
function binds(dom, fn) {
// @ts-ignore
(dom.__binds ||= [])
.push(fn);
if (dom.__bind) {
// special case for checkbox that can have multiple bind (group & checked for example)
// @ts-ignore
const prev = dom.__bind;
// @ts-ignore
dom.__bind = () => { prev(); fn(); }
} else {
// @ts-ignore
dom.__bind = fn;
}
return fn;
}
@ -1066,6 +1074,15 @@ export function bind_group(group, group_index, dom, get_value, update) {
let value = dom.__value;
if (is_checkbox) {
value = get_binding_group_value(binding_group, value, dom.checked);
} else if (!dom.checked) {
value = null;
if (dom.form && dom.name) {
const item = dom.form.elements.namedItem(dom.name);
if (item) {
// @ts-ignore
value = item.value;
}
}
}
update(value);
}));

Loading…
Cancel
Save