Line data Source code
1 : import { useFuse } from '@vueuse/integrations/useFuse'
2 : import type { MaybeRefOrGetter, Ref } from 'vue'
3 : import { computed, toValue } from 'vue'
4 :
5 : import type { SelectOption } from './types'
6 :
7 179 : export function useSelectSearch<Value extends string | number>(
8 : query: Ref<string>,
9 : options: MaybeRefOrGetter<SelectOption<Value>[]>
10 : ) {
11 179 : const { results } = useFuse(query, options, {
12 : fuseOptions: { keys: ['name', 'value'], threshold: 0.3 },
13 : matchAllWhenSearchEmpty: true
14 : })
15 :
16 179 : return computed(() =>
17 : query.value.trim()
18 : ? results.value.map(({ item }) => item)
19 : : toValue(options)
20 : )
21 : }
|