|
1 | 1 | # vue-fuse
|
2 | 2 |
|
3 |
| -A fully typed, lightweight, and reactive interface for [Fuse.js](https://fusejs.io/) |
4 |
| -fuzzy search lib. |
| 3 | +This is the monorepo, check out `./apps/vue-fuse` for the package source, and `./apps/demo` for a working example. |
5 | 4 |
|
6 |
| -## 4.X |
7 |
| -Version 4.X is a complete rewrite, intended to leverage Vue 3 (but still works with Vue 2 with the composition-api plugin). |
8 |
| - |
9 |
| -Check out the [3.x branch](https://github.com/shayneo/vue-fuse/tree/3.x) for older version, or look at release notes. |
10 |
| - |
11 |
| -## Vue 3 or Vue 2 |
12 |
| -`vue-fuse` uses [vue-demi](https://www.npmjs.com/package/vue-demi) to offer |
13 |
| -Vue 3.X or Vue 2.X compatibility. |
14 |
| - |
15 |
| -**Vue 2.X requires** [@vue/composition-api](https://www.npmjs.com/package/@vue/composition-api) |
16 |
| - |
17 |
| -## Getting Started |
18 |
| -``` |
19 |
| -npm i vue-fuse fuse.js |
20 |
| -``` |
21 |
| - |
22 |
| -```vue |
23 |
| -<template> |
24 |
| - <input type="text" v-model="search"> |
25 |
| - <p v-if="noResults">Sorry, no results for {{search}}</p> |
26 |
| - <div v-for="(r, i) in results" :key="i"> |
27 |
| - {{ r }} |
28 |
| - </div> |
29 |
| - </div> |
30 |
| -</template> |
31 |
| -
|
32 |
| -<script lang="ts"> |
33 |
| -import { defineComponent } from 'vue' |
34 |
| -import { useVueFuse } from 'vue-fuse' |
35 |
| -
|
36 |
| -export default defineComponent({ |
37 |
| - setup () { |
38 |
| - const myList = ['aaaa', 'bbbb', 'cccc', 'abc', 'xyz'] |
39 |
| - const { search, results, noResults } = useVueFuse(myList) |
40 |
| -
|
41 |
| - return { |
42 |
| - search, |
43 |
| - results, |
44 |
| - noResults, |
45 |
| - } |
46 |
| - } |
47 |
| -}) |
48 |
| -</script> |
49 |
| -``` |
50 |
| - |
51 |
| -## Typings |
52 |
| -A vue-fuse instance will detect the typed array of items |
53 |
| -needed to search. Thus, your results should be fully typed |
54 |
| -to match the type of the array items passed in. |
55 |
| - |
56 |
| -## Options |
57 |
| -`useVueFuse` and/or the `VueFuse` class constructor accept an optional |
58 |
| -second argument where you can pass in a [Fuse.js Options Object](https://fusejs.io/api/options.html) |
59 |
| - |
60 |
| -## results, resultsRaw, noResults |
61 |
| -```js |
62 |
| -const { results, resaltsRaw, noResults } = useVueFuse(['a', 'b', 'c']) |
63 |
| -``` |
64 |
| - |
65 |
| -`results` - is an array containing a the subset of items you passed in that match the search |
66 |
| -`resultsRaw` - exposed the full result payload from Fuse.js, this contains things like the |
67 |
| -original array index or the match "score". |
68 |
| -`noResults` - a computed boolean that will be `true` when there are no results, |
69 |
| -but the search term is not empty |
70 |
| - |
71 |
| -## Searching Array of Objects |
72 |
| -The examples above include simple arrays of strings, but you'll probably want to |
73 |
| -search accross more complex data structures. Fuse.js supports this by allowing you to |
74 |
| -pass [keys](https://fusejs.io/api/options.html#keys) into the search options. |
75 |
| - |
76 |
| -so if you have an array of objects like this: |
77 |
| -```js |
78 |
| -const bikes = [ |
79 |
| - { |
80 |
| - brand: 'Santa Cruz', |
81 |
| - model: '5010', |
82 |
| - year: 2021, |
83 |
| - wheelSize: 27.5, |
84 |
| - }, |
85 |
| - { |
86 |
| - brand: 'Canyon', |
87 |
| - model: 'Neuron', |
88 |
| - year: 2021, |
89 |
| - wheelSize: 29, |
90 |
| - } |
91 |
| -] |
92 |
| -``` |
93 |
| - |
94 |
| -To search by brand and model, you could set your config to: |
95 |
| -```js |
96 |
| -{ |
97 |
| - keys: ['brand', 'model'] |
98 |
| -} |
99 |
| -``` |
100 |
| - |
101 |
| -You can also weight each key differently |
102 |
| -```js |
103 |
| -{ |
104 |
| - keys: [ |
105 |
| - { |
106 |
| - name: 'brand', |
107 |
| - weight: 3, |
108 |
| - }, |
109 |
| - { |
110 |
| - name: 'model', |
111 |
| - weight: 1, |
112 |
| - } |
113 |
| - ] |
114 |
| -} |
115 |
| -``` |
116 |
| - |
117 |
| -You can also search nested keys by chaining the property names |
118 |
| -```js |
119 |
| -{ |
120 |
| - keys: ['foo', 'bar.baz.buzz'] |
121 |
| -} |
122 |
| -``` |
| 5 | +You can also view a working example here: [live demo](https://vue-fuse-demo.netlify.app/) |
0 commit comments