Automatic Two Way Binding un value
This commit is contained in:
@@ -97,7 +97,7 @@
|
|||||||
const callback = isExplicit ? fn : target;
|
const callback = isExplicit ? fn : target;
|
||||||
const depsInput = isExplicit ? target : null;
|
const depsInput = isExplicit ? target : null;
|
||||||
|
|
||||||
if (typeof callback !== "function") return () => {};
|
if (typeof callback !== "function") return () => { };
|
||||||
|
|
||||||
const owner = currentOwner;
|
const owner = currentOwner;
|
||||||
const runner = () => {
|
const runner = () => {
|
||||||
@@ -190,22 +190,25 @@
|
|||||||
el._cleanups = new Set();
|
el._cleanups = new Set();
|
||||||
|
|
||||||
for (let [k, v] of Object.entries(props)) {
|
for (let [k, v] of Object.entries(props)) {
|
||||||
if (k.startsWith("on")) {
|
const isSignal = typeof v === "function";
|
||||||
|
const isInput = ["INPUT", "TEXTAREA", "SELECT"].includes(el.tagName);
|
||||||
|
const isBindAttr = (k === "value" || k === "checked");
|
||||||
|
|
||||||
|
if (isInput && isBindAttr && isSignal) {
|
||||||
|
el._cleanups.add($.watch(() => {
|
||||||
|
const val = v();
|
||||||
|
if (el[k] !== val) el[k] = val;
|
||||||
|
}));
|
||||||
|
const evt = k === "checked" ? "change" : "input";
|
||||||
|
const handler = (e) => v(e.target[k]);
|
||||||
|
el.addEventListener(evt, handler);
|
||||||
|
el._cleanups.add(() => el.removeEventListener(evt, handler));
|
||||||
|
} else if (k.startsWith("on")) {
|
||||||
const name = k.slice(2).toLowerCase().split(".")[0];
|
const name = k.slice(2).toLowerCase().split(".")[0];
|
||||||
const handler = (e) => v(e);
|
const handler = (e) => v(e);
|
||||||
el.addEventListener(name, handler);
|
el.addEventListener(name, handler);
|
||||||
el._cleanups.add(() => el.removeEventListener(name, handler));
|
el._cleanups.add(() => el.removeEventListener(name, handler));
|
||||||
} else if (k.startsWith("$")) {
|
} else if (isSignal) {
|
||||||
const attr = k.slice(1);
|
|
||||||
el._cleanups.add($.watch(() => {
|
|
||||||
const val = typeof v === "function" ? v() : v;
|
|
||||||
if (el[attr] !== val) el[attr] = val;
|
|
||||||
}));
|
|
||||||
if (typeof v === "function") {
|
|
||||||
const evt = attr === "checked" ? "change" : "input";
|
|
||||||
el.addEventListener(evt, (e) => v(e.target[attr]));
|
|
||||||
}
|
|
||||||
} else if (typeof v === "function") {
|
|
||||||
el._cleanups.add($.watch(() => {
|
el._cleanups.add($.watch(() => {
|
||||||
const val = v();
|
const val = v();
|
||||||
if (k === "class") el.className = val || "";
|
if (k === "class") el.className = val || "";
|
||||||
|
|||||||
Reference in New Issue
Block a user