y-design-ssr
Version:
SSR component library of YUI with Vue3
216 lines (215 loc) • 5.15 kB
JavaScript
import { getCurrentInstance as C, reactive as x, provide as E, isVNode as O, ref as F, defineComponent as P, createVNode as S } from "vue";
const V = (n) => {
const e = [], r = (o) => {
Array.isArray(o) && o.forEach((l) => {
var c;
O(l) && (e.push(l), (c = l.component) != null && c.subTree && r(l.component.subTree.children), l.children && r(l.children));
});
};
return r(n), e;
}, T = (n, e) => {
const r = V(e.subTree.children);
n.sort((o, l) => r.indexOf(o.vnode) - r.indexOf(l.vnode));
}, _ = (n) => {
const e = C(), r = x([]);
return {
children: r,
initProvide: (l) => {
E(n, {
addChild: (f) => {
f.proxy && (r.push(f), e && T(r, e));
},
removeChild: (f) => {
const h = r.indexOf(f);
r.splice(h, 1);
},
children: r,
...l
});
}
};
}, d = (n, e) => e ? typeof e == "string" ? ` ${n}--${e}` : Array.isArray(e) ? e.reduce((r, o) => r + d(n, o), "") : Object.keys(e).reduce(
(r, o) => r + (e[o] ? d(n, o) : ""),
""
) : "", M = (n) => (e, r) => {
let o = e, l = r;
return o && typeof o != "string" && (l = o, o = ""), o = o ? `${n}__${o}` : n, `${o}${d(o, l)}`;
}, N = () => (n, e) => e ? `${d(`y-${n}`, e)}` : `y-${n}`, I = {
"pull-refresh": {
pulling: "下拉刷新...",
loosing: "释放刷新...",
loading: "数据加载中...",
success: "数据已更新",
failed: "数据跟新失败,请稍后再试"
},
"form-item": {
validateMessage: "请输入正确内容"
}
};
F("zh-CN");
x({
"zh-CN": I
});
const z = (n) => {
const e = `y-${n}`;
return [e, M(e), N()];
}, R = {
size: {
type: String,
default: null
},
border: {
type: Boolean,
default: !0
},
labelWidth: {
type: [Number, String],
default: null
},
colon: {
// 是否有 ":"
type: Boolean,
default: !1
},
labelAlign: {
type: String,
default: null
},
showError: {
type: Boolean,
default: !0
},
showErrorMessage: {
type: Boolean,
default: !0
},
errorMessageAlign: {
type: String,
default: null
},
rules: {
type: [Object, Array],
default: null
}
}, [$, D] = z("form"), j = $, k = /* @__PURE__ */ P({
name: $,
props: {
...R,
validateFirst: {
// 是否在某一项校验不通过时停止校验
type: Boolean,
default: !1
},
scrollToError: {
type: Boolean,
default: !0
},
validateTrigger: {
type: String,
default: "onBlur"
},
enterable: {
// 回车可以submit
type: Boolean,
default: !0
}
},
emits: ["change", "submit", "failed"],
setup(n, {
slots: e,
emit: r,
expose: o
}) {
const {
initProvide: l,
children: c
} = _(j), p = () => {
const t = {};
return c.forEach((s) => {
const a = s.exposed;
t[s.props.prop] = a.formValue;
}), t;
}, f = (t) => {
const s = c.filter((a) => a.props.prop === t);
return new Promise((a, i) => {
if (s.length === 0)
i(new Error('【YUI】form method "validateField": no field, please check your param: "propName"!'));
else {
const u = s[0].exposed;
u == null || u.validate().then((b) => {
b ? i(b) : a();
});
}
});
}, h = async () => {
const t = [];
if (await c.reduce((s, a) => s.then(() => {
if (!t.length) {
const i = a.exposed;
return i == null ? void 0 : i.validate().then((u) => {
u && t.push(u);
});
}
return null;
}), Promise.resolve()), t.length)
throw t;
}, w = async () => {
const s = (await Promise.all(c.map((a) => a.exposed.validate()))).filter((a) => a);
if (s.length)
throw s;
}, m = (t) => t ? f(t) : n.validateFirst ? h() : w(), y = (t, s) => {
const a = c.find((u) => u.props.prop === t);
(a == null ? void 0 : a.vnode.el).scrollIntoView(s);
}, g = () => {
const t = p();
m().then(() => {
r("submit", t);
}).catch((s) => {
r("failed", {
values: t,
errors: s
}), Array.isArray(s) && n.scrollToError && y(s == null ? void 0 : s[0].name);
});
}, A = (t) => {
t.preventDefault(), g();
}, v = () => {
r("change", p());
}, B = (t) => {
c.forEach((s) => {
var a;
if (!t || s.props.prop === t) {
const i = s.exposed;
(a = i.resetValidation) == null || a.call(i);
}
});
};
return l({
props: n,
change: v
}), o({
submit: g,
scrollToField: y,
validate: m,
change: v,
resetValidation: B
}), () => {
var t;
return S("form", {
onSubmit: A,
class: [D()]
}, [(t = e.default) == null ? void 0 : t.call(e)]);
};
}
});
function q(n) {
const e = n;
return e.install = (r) => {
const { name: o } = n;
o && r.component(o, n);
}, e;
}
const L = q(k);
export {
R as commonProps,
L as default
};