2021-08-21 08:25:55 +00:00
|
|
|
import type { Ref } from 'vue';
|
2021-06-26 01:35:40 +00:00
|
|
|
import { computed } from 'vue';
|
|
|
|
import type { RawValueType, FlattenOptionsType, Key } from '../interface/generator';
|
2020-10-07 14:49:01 +00:00
|
|
|
|
|
|
|
export default function useCacheOptions<
|
2021-08-21 08:25:55 +00:00
|
|
|
OptionType extends {
|
2020-10-07 14:49:01 +00:00
|
|
|
value?: RawValueType;
|
2021-08-21 08:25:55 +00:00
|
|
|
label?: any;
|
2020-10-07 14:49:01 +00:00
|
|
|
key?: Key;
|
|
|
|
disabled?: boolean;
|
2021-08-21 08:25:55 +00:00
|
|
|
},
|
2021-06-22 02:47:33 +00:00
|
|
|
>(options: Ref) {
|
2020-10-07 14:49:01 +00:00
|
|
|
const optionMap = computed(() => {
|
2021-08-21 08:25:55 +00:00
|
|
|
const map: Map<RawValueType, FlattenOptionsType<OptionType>[number]> = new Map();
|
2020-10-10 09:01:39 +00:00
|
|
|
options.value.forEach((item: any) => {
|
2020-10-07 14:49:01 +00:00
|
|
|
const {
|
|
|
|
data: { value },
|
|
|
|
} = item;
|
|
|
|
map.set(value, item);
|
|
|
|
});
|
|
|
|
return map;
|
|
|
|
});
|
|
|
|
|
2021-08-21 08:25:55 +00:00
|
|
|
const getValueOption = (vals: RawValueType[]) =>
|
2020-10-07 14:49:01 +00:00
|
|
|
vals.map(value => optionMap.value.get(value)).filter(Boolean);
|
|
|
|
|
|
|
|
return getValueOption;
|
|
|
|
}
|