ant-design-vue/components/vc-select/hooks/useCacheOptions.ts

29 lines
801 B
TypeScript
Raw Normal View History

2021-06-26 01:35:40 +00:00
import type { Ref, VNodeChild } from 'vue';
import { computed } from 'vue';
import type { RawValueType, FlattenOptionsType, Key } from '../interface/generator';
2020-10-07 14:49:01 +00:00
export default function useCacheOptions<
OptionsType extends {
value?: RawValueType;
label?: VNodeChild;
key?: Key;
disabled?: boolean;
2021-06-23 15:08:16 +00:00
}[],
2021-06-22 02:47:33 +00:00
>(options: Ref) {
2020-10-07 14:49:01 +00:00
const optionMap = computed(() => {
const map: Map<RawValueType, FlattenOptionsType<OptionsType>[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;
});
const getValueOption = (vals: RawValueType[]): FlattenOptionsType<OptionsType> =>
vals.map(value => optionMap.value.get(value)).filter(Boolean);
return getValueOption;
}