UNPKG

vue-fuse

Version:

A Vue.js pluggin for fuzzy search library, Fuse.js

60 lines (59 loc) 1.88 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; import Fuse from "fuse.js"; import { ref, watch, computed } from "vue"; class VueFuse { constructor(list, options) { __publicField(this, "fuse"); __publicField(this, "resultsRaw"); __publicField(this, "results"); __publicField(this, "search"); __publicField(this, "noResults"); __publicField(this, "runSearch"); __publicField(this, "loadItems"); var _a; this.search = ref(""); this.runSearch = (search) => { if (!this.fuse) { return; } if (!search) { this.resultsRaw.value = []; return; } this.resultsRaw.value = this.fuse.value.search(search); }; this.loadItems = (items) => { this.fuse = ref(new Fuse(items, options)); this.runSearch(this.search.value); }; let localArray = []; if (Array.isArray(list)) { localArray = list; } else if (list) { localArray = (_a = list.value) != null ? _a : []; watch(list, () => { var _a2; this.loadItems((_a2 = list.value) != null ? _a2 : []); }); } this.fuse = ref(new Fuse(localArray, options)); this.resultsRaw = ref([]); this.noResults = computed(() => { if (this.results.value.length === 0 && this.search.value.length > 0) { return true; } return false; }); this.results = computed(() => this.resultsRaw.value.map((r) => r.item)); watch(this.search, this.runSearch); } } function useVueFuse(list, options) { return new VueFuse(list, options); } export { VueFuse, useVueFuse };