ucc-ui
Version:
ucc-ui components library
102 lines (101 loc) • 3.14 kB
JavaScript
import { defineComponent as u, reactive as p, provide as m, openBlock as h, createElementBlock as y, normalizeClass as v, withModifiers as A, renderSlot as F } from "vue";
const M = /* @__PURE__ */ u({
name: "UForm",
inheritAttrs: !1,
__name: "Form",
props: {
model: {},
rules: {},
labelPosition: { default: "right" },
labelWidth: { default: "" },
labelSuffix: { default: "" },
hideRequiredAsterisk: { type: Boolean, default: !1 },
requiredAsteriskPosition: { default: "left" },
showMessage: { type: Boolean, default: !0 },
inlineMessage: { type: Boolean, default: !1 },
statusIcon: { type: Boolean, default: !1 },
validateOnRuleChange: { type: Boolean, default: !0 },
size: { default: "" },
inline: { type: Boolean, default: !1 }
},
setup(a, { expose: n }) {
const i = a, o = [], d = p({
model: i.model,
rules: i.rules,
labelPosition: i.labelPosition,
labelWidth: i.labelWidth,
labelSuffix: i.labelSuffix,
hideRequiredAsterisk: i.hideRequiredAsterisk,
requiredAsteriskPosition: i.requiredAsteriskPosition,
showMessage: i.showMessage,
inlineMessage: i.inlineMessage,
statusIcon: i.statusIcon,
validateOnRuleChange: i.validateOnRuleChange,
size: i.size,
addField: (e) => {
o.push(e);
},
removeField: (e) => {
if (e.prop) {
const t = o.indexOf(e);
t !== -1 && o.splice(t, 1);
}
}
});
return m("form", d), n({
validate: async (e) => {
const t = {}, s = o.map((r) => r.validate(""));
try {
await Promise.all(s), e == null || e(!0);
} catch {
e == null || e(!1, t);
}
},
validateField: async (e, t) => {
if (!e) return !0;
const r = (Array.isArray(e) ? e : [e]).flat(), f = o.filter((l) => l.prop && r.includes(l.prop)).map((l) => l.validate(""));
try {
return await Promise.all(f), t == null || t(!0), !0;
} catch {
return t == null || t(!1), !1;
}
},
clearValidate: (e) => {
if (!e) {
o.forEach((r) => r.clearValidate());
return;
}
const s = (Array.isArray(e) ? e : [e]).flat();
o.forEach((r) => {
r.prop && s.includes(r.prop) && r.clearValidate();
});
},
scrollToField: (e) => {
const t = o.find(
(s) => s.prop === (Array.isArray(e) ? e[0] : e)
);
if (t) {
const s = document.querySelector(`.u-form-item[data-prop="${t.prop}"]`);
s == null || s.scrollIntoView({ behavior: "smooth", block: "nearest" });
}
},
resetFields: () => {
o.forEach((e) => {
e.resetField();
});
}
}), (e, t) => (h(), y("form", {
class: v(["u-form", [
e.labelPosition ? `u-form--label-${e.labelPosition}` : "",
{ "u-form--inline": e.inline }
]]),
onSubmit: t[0] || (t[0] = A(() => {
}, ["prevent"]))
}, [
F(e.$slots, "default")
], 34));
}
});
export {
M as _
};