omi-vueify
Version:
Bridge between omi and vue
59 lines (58 loc) • 1.79 kB
JavaScript
import { defineComponent as d, ref as j, onMounted as v, onBeforeUnmount as y, h as A } from "vue";
function z(t, i) {
return d({
name: t,
setup(h, { emit: l, attrs: m, slots: f, expose: E }) {
const u = j(null), { methodNames: b } = i, s = {};
b.forEach((e) => {
s[e] = (...r) => {
var o, n;
if (u.value)
return (n = (o = u.value)[e]) == null ? void 0 : n.call(o, ...r);
};
}), E(s);
const p = Object.keys(m).filter((e) => e.match(/^on[A-Za-z]/)).map((e) => L(e));
return v(() => {
p.forEach((e) => {
var o;
const r = C(e);
O(r) && ((o = u.value) == null || o.addEventListener(e, (n) => {
l(r, n);
}));
});
}), y(() => {
p.forEach((e) => {
var r;
(r = u.value) == null || r.removeEventListener(e, () => {
});
});
}), () => {
const e = [];
return f.default && e.push(...f.default()), Object.keys(f).forEach((r) => {
var n;
if (r === "default") return;
const o = (n = f[r]) == null ? void 0 : n.call(f);
o && o.forEach((c) => {
Array.isArray(c) ? c.forEach((a) => {
a && typeof a == "object" && (a.props = { ...a.props || {}, slot: r });
}) : c && typeof c == "object" && (c.props = { ...c.props || {}, slot: r }), e.push(c);
});
}), A(
t,
{
ref: u,
...h
},
e
);
};
}
});
}
const L = (t) => {
const i = t.slice(2);
return i[0].toLowerCase() + i.slice(1);
}, C = (t) => t.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), O = (t) => t.includes("-");
export {
z as omiVueify
};