yanzhen-design-vue
Version:
61 lines (60 loc) • 1.87 kB
JavaScript
import { useSlots, computed, unref, mergeProps, h, withCtx, createVNode } from "vue";
import { FormItem } from "ant-design-vue";
import { useProxyProps } from "@yanzhen-libs/utils-vue";
import { pick, fromPairs, get } from "lodash-es";
import { isEmptyArray } from "@yanzhen-libs/utils";
import { AntFormItem } from "./form-item.mjs";
import { useInjectFormItemGroupContext } from "./hooks/form-item-group-context.mjs";
import "./form-item-wrapper.vue.mjs";
import _sfc_main from "./form-item-wrapper.vue2.mjs";
const useFormItem = (props, emit, expose) => {
const slots = { ...useSlots() };
const fieldName = computed(() => props.name || props.prop);
const config = computed(() => {
const _props2 = pick(props, ...Object.keys(AntFormItem.props));
if (typeof _props2.label === "function") {
slots["label"] = _props2.label;
delete _props2.label;
}
return _props2;
});
const _props = useProxyProps(config, AntFormItem.emits, {
emit,
exclude: ["name", "prop", "help"],
filter: true
});
const { id: groupNames } = useInjectFormItemGroupContext();
const help = computed(() => {
const names = unref(groupNames);
if (isEmptyArray(names)) {
return props.help;
}
const data = fromPairs(names.map((name2) => [name2, true]));
const name = unref(fieldName);
return name && get(data, name) === true ? [] : props.help;
});
return [
{
_props
},
(props2) => {
const propsData = mergeProps(unref(_props), props2 ?? {}, {
name: unref(fieldName),
help: unref(help)
});
return h(FormItem, propsData, {
...slots,
default: withCtx(() => {
return [
createVNode(_sfc_main, propsData, {
default: slots.default
})
];
})
});
}
];
};
export {
useFormItem
};