yanzhen-design-vue
Version:
82 lines (81 loc) • 2.38 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const vue = require("vue");
const antDesignVue = require("ant-design-vue");
const lodashEs = require("lodash-es");
const utilsVue = require("@yanzhen-libs/utils-vue");
const utils = require("@yanzhen-libs/utils");
const context = require("ant-design-vue/es/form/context");
const form = require("./form.cjs");
const useForm$1 = require("./hooks/use-form.cjs");
function useForm(props, emit) {
const _ref = vue.ref(null);
const slots = vue.useSlots();
const attrs = vue.useAttrs();
const model = context.useInjectForm().model;
const modelRef = vue.computed(() => props.model ?? vue.unref(model) ?? {});
const rulesRef = vue.computed(() => props.rules ?? {});
const { validate, resetFields, clearValidate, scrollToField } = useForm$1.useProvideForm(
modelRef,
rulesRef
);
const config = vue.computed(() => {
return lodashEs.pick(props, ...Object.keys(form.AntForm.props));
});
const proxyProps = utilsVue.useProxyProps(config, form.AntForm.emits, {
emit,
exclude: ["onValidate"],
filter: true
});
const onValidate = (name, ...args) => {
validate(name).then((r) => r);
emit("validate", name, ...args);
};
const ctx = {
resetFields,
clearValidate,
scrollToField,
async validateFields(nameList) {
if (utils.isEmptyValue(nameList)) {
return Promise.resolve();
}
await Promise.allSettled(nameList.map((name) => validate(name)));
}
};
const exposeCtx = new Proxy(ctx, {
get(target, p, receiver) {
const formRef = vue.unref(_ref);
const fun1 = Reflect.get(target, p, receiver);
const fun2 = !utils.isEmptyValue(formRef) ? Reflect.get(formRef, p, receiver) : null;
if (Object.keys(ctx).includes(p)) {
return (...args) => {
for (const fun of [fun1, fun2]) {
try {
lodashEs.isFunction(fun) && fun(...args);
} catch {
}
}
};
}
return fun1 ?? fun2;
}
});
return [
{
_ref,
proxyProps,
onValidate,
exposeCtx
},
(props2) => {
return vue.h(
antDesignVue.Form,
vue.mergeProps(attrs, vue.unref(proxyProps), props2, {
ref: _ref
}),
slots
);
}
];
}
exports.useForm = useForm;