UNPKG

yanzhen-design-vue

Version:

53 lines (52 loc) 1.81 kB
import { useSlots, computed, unref } from "vue"; import { useInjectForm } from "ant-design-vue/es/form/context"; import { isEmptyValue, isEmptyArray } from "@yanzhen-libs/utils"; import { getNamePath } from "ant-design-vue/es/form/utils/valueUtil"; import { useProvideFormItemContext } from "ant-design-vue/es/form/FormItemContext"; import { useInjectFormItemGroupContext } from "./hooks/form-item-group-context.mjs"; const defaultItemNamePrefixCls = "yz_form_item_wrapper"; function useFormItemWrapper(props, emit) { const slots = useSlots(); const { onFieldBlur, onFieldChange, clearValidate } = useInjectFormItemGroupContext(); const formContext = useInjectForm(); const fieldName = computed(() => props.name); const namePath = computed(() => { const name = unref(fieldName); if (isEmptyValue(name)) return null; return getNamePath(name); }); const fieldId = computed(() => { const namePaths = unref(namePath); if (isEmptyArray(namePaths)) { return void 0; } else { const formName = unref(formContext.name); const mergedId = namePaths.join("_"); return formName ? `${formName}_${mergedId}` : `${defaultItemNamePrefixCls}_${mergedId}`; } }); useProvideFormItemContext( { id: fieldId, onFieldBlur: () => { onFieldBlur(unref(fieldName)); }, onFieldChange: () => { onFieldChange(unref(fieldName)); }, clearValidate: () => { clearValidate(unref(fieldName)); } }, computed(() => { return !!(props.autoLink && unref(formContext.model) && unref(fieldName)); }) ); return [{ fieldId, fieldName }, () => { var _a; return (_a = slots == null ? void 0 : slots.default) == null ? void 0 : _a.call(slots); }]; } export { useFormItemWrapper };