import { ref, computed } from 'vue' import { selectedValueType, selectedValueItem } from './useSelectedValue' export type sourceListMapType = { [propName in selectedValueType]: selectedValueItem } export default keyConfig => { const sourceList = ref([]) const originalSourceList = ref([]) const sourceListMap = computed(() => { const { valueKey } = keyConfig return sourceList.value.reduce((result: sourceListMapType, item) => { result[item[valueKey]] = item return result }, {}) }) const formatSourceList = ( list: any[], { keyConfig }, ): selectedValueItem[] => { const { valueKey, labelKey, disabledKey } = keyConfig return list.map(item => { return { value: item[valueKey], label: item[labelKey], disabled: item[disabledKey] || false, ...item, } }) } const setSourceList = (list: any[], { keyConfig }) => { const flist = formatSourceList(list, { keyConfig }) sourceList.value = flist originalSourceList.value = flist } return { sourceList, originalSourceList, setSourceList, sourceListMap, } }