Searcher.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template lang="pug">
  2. input.searcher(type='search' :placeholder='placeholder' :style='{ width, height }' v-model='search')
  3. </template>
  4. <script>
  5. import Fuse from 'fuse.js'
  6. export default {
  7. props: {
  8. items: {
  9. type: Array,
  10. default: [],
  11. required: true
  12. },
  13. placeholder: {
  14. type: String,
  15. default: 'Buscar...'
  16. },
  17. width: {
  18. type: String,
  19. default: '100%'
  20. },
  21. height: {
  22. type: String,
  23. default: '35px'
  24. },
  25. shouldSort: {
  26. type: Boolean,
  27. default: true
  28. },
  29. threshold: {
  30. type: Number,
  31. default: 0.4,
  32. },
  33. location: {
  34. type: Number,
  35. default: 0
  36. },
  37. distance: {
  38. type: Number,
  39. default: 100
  40. },
  41. maxPatternLength: {
  42. type: Number,
  43. default: 32
  44. },
  45. minMatchCharLength: {
  46. type: Number,
  47. default: 1
  48. },
  49. keys: {
  50. type: Array,
  51. default: [],
  52. required: true
  53. }
  54. },
  55. watch: {
  56. items(values) {
  57. this.fuse.setCollection(values)
  58. },
  59. search(value) {
  60. this.performSearch(value.trim())
  61. },
  62. results(values) {
  63. this.$emit('onSearch', values)
  64. }
  65. },
  66. methods: {
  67. initFuse() {
  68. this.fuse = new Fuse(this.items, {
  69. shouldSort: this.shouldSort,
  70. threshold: this.threshold,
  71. location: this.location,
  72. distance: this.distance,
  73. maxPatternLength: this.maxPatternLength,
  74. minMatchCharLength: this.minMatchCharLength,
  75. keys: this.keys
  76. })
  77. },
  78. performSearch(value) {
  79. this.results = this.fuse.search(value)
  80. }
  81. },
  82. data() {
  83. return {
  84. fuse: null,
  85. search: '',
  86. results: []
  87. }
  88. },
  89. mounted() {
  90. this.initFuse()
  91. }
  92. }
  93. </script>
  94. <style lang="sass">
  95. .searcher
  96. text-align: center
  97. border-radius: 0 !important
  98. font:
  99. size: 11pt
  100. style: normal
  101. weight: bold
  102. </style>