UNPKG

yanzhen-design-vue

Version:

184 lines (183 loc) 5.05 kB
import { toRefs, ref, computed, unref, onMounted, onUnmounted } from "vue"; import { flattenDeep, kebabCase, camelCase, merge, uniq, isFunction } from "lodash-es"; import { createWatcher } from "@yanzhen-libs/utils-vue"; import { Form } from "ant-design-vue"; import { useContactsSelectedData } from "./hook/use-contacts-selected-data.mjs"; import { contactsPickProps } from "./contacts.mjs"; import { contactsSelectListType, contactsSelectOmitModalProps, defineContactsSelectProps } from "./contacts-select.mjs"; import { useContactsConfirmModels } from "./hook/use-contacts-confirm-models.mjs"; function useContactsSelect(props, emit) { const watcher = createWatcher(); const { multiple, immediate } = toRefs(props); const contactsRef = ref(null); const { open: _open, disabled, ...vModels } = useContactsConfirmModels(props); const formItemContext = Form.useInjectFormItemContext(); const { selectedValue, getSelectedValues } = useContactsSelectedData( contactsRef, emit ); const contactsProps = computed(() => { const _props = { ...props }; for (const [prop, value] of Object.entries(_props)) { if (value === void 0) { delete _props[prop]; } } return { ...contactsPickProps(_props), ...unref(vModels.props), ...unref(vModels.emits) }; }); const listType = computed(() => { if (props.listType === false) { return false; } else if (props.listType === true) { return [contactsSelectListType[0]]; } return flattenDeep([props.listType]).filter(Boolean); }); const modalProps = computed(() => { const _modalProps = Object.entries(props).reduce((prev, [prop, value]) => { if (value === void 0) return prev; const key = kebabCase(prop); switch (true) { case key === "modal-props": break; case key.startsWith("modal-"): prev[camelCase(key.replace("modal-", ""))] = value; break; } return prev; }, {}); const _props = merge(_modalProps, (props == null ? void 0 : props.modalProps) ?? {}); return contactsSelectOmitModalProps(_props); }); const selectProps = computed(() => { return merge(defineContactsSelectProps(props.selectProps), { autoClear: props.autoClear, multiple: props.multiple }); }); const selectedData = computed(() => { const values = unref(selectedValue); const maxCount = unref(selectProps).maxCount; if (values.length === maxCount) { return { values, overage: [] }; } return { values: values.slice(0, maxCount), overage: values.slice(maxCount, values.length) }; }); function handleOverageText(values = []) { const labels = values.map(({ label }) => label); return labels.join(","); } watcher.set("open", { source: _open, handler(open) { var _a; if (open) { (_a = unref(contactsRef)) == null ? void 0 : _a.refresh(); } }, immediate: true, debounce: 500 }); async function handleContactsChange() { if (!unref(multiple) && unref(immediate)) { await handleConfirm(); } } async function handleConfirm() { const ids = uniq(flattenDeep(Object.values(unref(vModels.props)))); const selectedValue2 = getSelectedValues(ids); const args = [selectedValue2, unref(vModels.props)]; console.log(args, "items+++++"); if (isFunction(props.beforeConfirm)) { const result = await props.beforeConfirm(...args); if (result === false) return false; } if (props.onConfirm) { const result = await props.onConfirm(...args); if (result === false) return false; } else { emit == null ? void 0 : emit("confirm", ...args); } vModels.send(); handleChange(selectedValue2[selectedValue2.length - 1], ids); handleBlur(); handleClose(); } function handleDelete(item, index) { vModels.delete(item, index); } function handleCancel() { _open.value = false; emit("cancel"); handleBlur(); handleClose(); } function handleOpen() { vModels.update(); if (unref(disabled)) return; _open.value = true; } function handleClose() { vModels.update(); _open.value = false; } const handleChange = (...args) => { formItemContext.onFieldChange(); emit("change", ...args); }; const handleBlur = () => { formItemContext.onFieldBlur(); emit("blur"); }; onMounted(() => { }); onUnmounted(() => { _open.value = false; watcher.stop(); }); const refs = { contactsRef }; const ctx = { _open, selectedValue, modalProps, contactsProps, selectProps, listType, selectedData }; const methods = { open: handleOpen, close: handleCancel, handleOpen, handleCancel, handleConfirm, handleContactsChange, handleDelete, handleOverageText }; return { refs, ...refs, ctx, ...ctx, methods, ...methods }; } export { useContactsSelect };