UNPKG

tav-ui

Version:
143 lines (140 loc) 5.04 kB
import { unref, ref, computed } from 'vue'; import { debounce } from 'lodash-es'; import '../constants/index2.mjs'; import { RequestErrorTip, DebounceDely } from '../constants/hooks2.mjs'; const CASCADE_PRO_CACHE = /* @__PURE__ */ new Map(); const handleSelectRecordsAfterGetFields = (records, selectRecord, idPathSplitResult, setSelectRecords, type) => { if (type === "init") { setSelectRecords([], "recover"); } else { const allParentRecords = []; for (let i = 0; i < idPathSplitResult.length - 1; i++) { const options = records[i]; const parentRecord = options.find((option) => option.id === idPathSplitResult[i]); parentRecord && allParentRecords.push(parentRecord); } if (allParentRecords.length > 0) { setSelectRecords([...allParentRecords, unref(selectRecord)], "normal"); } else { setSelectRecords([unref(selectRecord)], "normal"); } } }; function getFirstLetterSortedOptions(options) { return options.sort((a, b) => { const nameA = a.firstLetter?.toUpperCase(); const nameB = b.firstLetter?.toUpperCase(); if (nameA && nameB) { if (nameA < nameB) { return -1; } if (nameA > nameB) { return 1; } } return 0; }); } function handleOptionsWithFirstLetterVisible(options) { const handledFirstLetterSort = getFirstLetterSortedOptions(options); const firstLetters = [ ...new Set(handledFirstLetterSort.map((option) => option.firstLetter)) ]; const firstLettersMap = firstLetters.reduce((result, cur) => { result[cur] = 0; return result; }, {}); const groupByFirstLetterOptions = handledFirstLetterSort.map((option) => { const { firstLetter } = option; if (firstLettersMap[firstLetter] === 0) { firstLettersMap[firstLetter]++; return { ...option, firstLetterGroup: firstLetter }; } else { return { ...option, firstLetterGroup: "" }; } }); return groupByFirstLetterOptions; } function searchData(param) { const firstLetterVisible = unref(param.firstLetterVisible); const { id, pid, fieldIndex, idPathSplitResult } = unref(param.selectRecordFibers); const tree = unref(param.options).tree; if (id === "" && pid === "" && fieldIndex === 0) { if (firstLetterVisible) { if (!CASCADE_PRO_CACHE.has(unref(param.id))) { CASCADE_PRO_CACHE.set(unref(param.id), { firstFieldFirstLetterResult: handleOptionsWithFirstLetterVisible(tree) }); } const cache = CASCADE_PRO_CACHE.get(unref(param.id)); handleSelectRecordsAfterGetFields([cache["firstFieldFirstLetterResult"]], unref(param.selectRecord), idPathSplitResult, param.setSelectRecords, "init"); return [cache["firstFieldFirstLetterResult"]]; } else { handleSelectRecordsAfterGetFields([tree], unref(param.selectRecord), idPathSplitResult, param.setSelectRecords, "init"); return [tree]; } } else { const currentFieldLength = idPathSplitResult.length; const nextFieldLength = currentFieldLength + 1; const records = []; for (let i = 0; i < nextFieldLength; i++) { if (i === 0) { if (firstLetterVisible) { if (!CASCADE_PRO_CACHE.has(unref(param.id))) { CASCADE_PRO_CACHE.set(unref(param.id), { firstFieldFirstLetterResult: handleOptionsWithFirstLetterVisible(tree) }); } const cache = CASCADE_PRO_CACHE.get(unref(param.id)); records.push(cache["firstFieldFirstLetterResult"]); } else { records.push(tree); } } else { const currentOptions = records[i - 1].find((option) => option.id === idPathSplitResult[i - 1])?.children; records.push(currentOptions); } } const response = records.filter(Boolean); handleSelectRecordsAfterGetFields(response, unref(param.selectRecord), idPathSplitResult, param.setSelectRecords, "normal"); return response; } } function useFieldRequest(param) { const resultRef = ref([]); const errorRef = ref(""); const onClick = (selected) => { const selectRecord = selected; param.setSelectRecord(selectRecord); param.setLoading(true); const response = searchData({ firstLetterVisible: param.firstLetterVisible, selectRecord: param.selectRecord, setSelectRecords: param.setSelectRecords, selectRecordFibers: param.selectRecordFibers, options: param.options, id: param.id }); if (Array.isArray(response)) { resultRef.value = response; errorRef.value = ""; } else if (response === null) { } else { resultRef.value = []; errorRef.value = RequestErrorTip; } param.setLoading(false); }; if (param.immediate) { onClick(unref(param.selectRecord)); } const result = computed(() => unref(resultRef)); const error = computed(() => unref(errorRef)); return { result, error, onClick: debounce(onClick, DebounceDely) }; } export { useFieldRequest }; //# sourceMappingURL=use-request2.mjs.map