diff options
Diffstat (limited to 'src/composable/fuzzySearch.ts')
-rw-r--r-- | src/composable/fuzzySearch.ts | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/composable/fuzzySearch.ts b/src/composable/fuzzySearch.ts index 66480f7..00794fd 100644 --- a/src/composable/fuzzySearch.ts +++ b/src/composable/fuzzySearch.ts @@ -11,12 +11,19 @@ function useFuzzySearch<Data>({ }: { search: MaybeRef<string> data: Data[] - options?: Fuse.IFuseOptions<Data> + options?: Fuse.IFuseOptions<Data> & { filterEmpty?: boolean } }) { const fuse = new Fuse(data, options); + const filterEmpty = options.filterEmpty ?? true; - const searchResult = computed(() => { - return fuse.search(get(search)).map(({ item }) => item); + const searchResult = computed<Data[]>(() => { + const query = get(search); + + if (!filterEmpty && query === '') { + return data; + } + + return fuse.search(query).map(({ item }) => item); }); return { searchResult }; |