Vue 2.x折騰記 - (3)寫一個不大靠譜的typeahead組件

前言

typeahead在網站中的應用不少..今天跟着我來寫一個不大靠譜的typeahead;javascript

你能學到什麼?css

  • 自定義事件
  • 遍歷的思想
  • 功能細節的考慮

一切都挺不靠譜的,可完善的地方不少.廢話很少說,看效果圖vue

效果圖

有哪些功能點?java

  • 粗糙的模糊搜索 - 藉助indexOf
  • ESCblur事件清除輸入框,沒有找到匹配的狀況下
  • Enter默認在找到只剩下一個狀況下選中
  • 方向盤的上下(已經阻止光標的移動)選中子項,回車選中
  • 鼠標點擊選擇子項
  • 搜索框清空狀況下默認不觸發自定義事件值的返回
  • 鼠標移動+鍵盤方向鍵移動位置的同步
  • placeholder及遍歷數據data支持外部傳入,也就是綁定props;前者字符串,後者數組對象

代碼

typeahead.vue

<template>
  <div class="typeahead"> <div class="typeahead-header"> <input type="text" v-model="searchVal" @input="filterList(searchVal)" ref="input" :placeholder="placeholder" @keydown.down.prevent="selectChildWidthArrowDown" @keydown.up.prevent="selectChildWidthArrowUp" @keydown.enter="selectChildWidthEnter" @blur="ifNotFoundClear" @keydown.esc="ifNotFoundClear" autocomplete="off"> </div> <transition name="el-fade-in-linear" mode="out-in"> <div class="typeahead-content" v-show="searchVal && searchVal.length"> <transition-group tag="ul" name="el-fade-in-linear" v-show="searchList && searchList.length > 0 && isExpand "> <li v-for="(item,index) in searchList" :key="index" :class="item.active ? 'active':''" @click="selectChild(index)" @mouseenter="setActiveClass(index)" @mouseleave="setActiveClass(index)"> <a href="javascript:;"> {{item.text}} </a> </li> </transition-group> <p class="noFound" v-show="searchList && searchList.length === 0">未能查詢到,請從新輸入!</p> </div> </transition> </div> </template> <script> export default { name: 'typeahead', data: function () { return { searchVal: '', // 搜索關鍵字 resultVal: '', // 保存搜索到的值 searchList: [], //保存過濾的結果集 currentIndex: -1, // 當前默認選中的index, isExpand: false } }, computed: { typeaheadData () { return this.mapData; } }, props: { placeholder: { type: String, default: '請輸入您要查詢的關鍵字' }, mapData: { type: Array, default: function () { return [ { text: 'wofsdf', value: 0 }, { text: '我是技術渣1', value: '1' }, { text: '我是技術渣2', value: '2' }, { text: '我是天坑', value: '2' }, { text: '我是天坑,分身乏術', value: '3' }, { text: '我是天坑2,分身乏術', value: '3' }, { text: '我是天坑3,分身乏術', value: '3' } ] } } }, methods: { filterList (searchVal) { // 過濾數組 if (this.searchVal === '') { this.ifNotFoundClear(); } else { this.searchList = []; // 清空列表 let tempArr = []; // 一個臨時數組 this.currentIndex = -1; // 重置index this.typeaheadData && this.typeaheadData.forEach(item => { if (item.text.indexOf(searchVal.trim()) !== -1) { tempArr.push(item); } }); this.searchList = tempArr; // 爲何要一個臨時數組,否則每次push都會觸發視圖更新..數據量一大.... this.isExpand = true; } }, ifNotFoundClear () { // 如果結果集長度爲0就證實沒有找到...賦值爲空 if (this.searchList.length === 0) { this.searchVal = ''; this.isExpand = false; } this.searchList.forEach(item => { item.active = false; }) }, selectChild (index) { // 鼠標點擊選擇子項 this.searchList.forEach((item, innerIndex) => { if (index === innerIndex || item.active) { this.searchVal = item.text; this.resultVal = item.value; this.isExpand = false; } this.$set(item, 'active', false); }) this.$emit('selectValue', { text: this.searchVal, value: this.resultVal }); }, selectChildWidthArrowDown () { // 判斷index選中子項 if (this.currentIndex < this.searchList.length) { this.currentIndex++; this.searchList.forEach((item, index) => { this.currentIndex === index ? this.$set(item, 'active', true) : this.$set(item, 'active', false); }) } }, selectChildWidthArrowUp () { // 判斷index選中子項 if (this.currentIndex > 0) { this.currentIndex--; this.searchList.forEach((item, index) => { this.currentIndex === index ? this.$set(item, 'active', true) : this.$set(item, 'active', false); }) } }, selectChildWidthEnter () { // 如果結果集只有一個,則默認選中 if (this.searchList.length === 1) { this.searchVal = this.searchList[0].text; this.resultVal = this.searchList[0].value; this.isExpand = false; this.$emit('selectValue', { text: this.searchVal, value: this.resultVal }) } else { // 如果搜索的內容徹底匹配到項內的內容,則默認選中 this.searchList.forEach(item => { if (this.searchVal === item.text || item.active === true) { this.searchVal = item.text; this.resultVal = item.value; this.isExpand = false; this.$emit('selectValue', { text: this.searchVal, value: this.resultVal }) } }) } }, setActiveClass (index) { this.searchList.forEach((item, innerIndex) => { if (index === innerIndex) { this.$set(item, 'active', true); this.currentIndex = index; // 這句話是用來修正index,,就是鍵盤上下鍵的索引,否則會跳位 } else { this.$set(item, 'active', false) } }) } } } </script> <style scoped lang="scss"> .el-fade-in-linear-enter-active, .el-fade-in-linear-leave-active, .fade-in-linear-enter-active, .fade-in-linear-leave-active { transition: opacity .2s linear; } .el-fade-in-enter, .el-fade-in-leave-active, .el-fade-in-linear-enter, .el-fade-in-linear-leave, .el-fade-in-linear-leave-active, .fade-in-linear-enter, .fade-in-linear-leave, .fade-in-linear-leave-active { opacity: 0; } .typeahead { position: relative; background-color: #fff; a { color: #333; text-decoration: none; padding: 5px; } ul { list-style: none; padding: 6px 0; margin: 0; overflow: visible; li { display: block; width: 100%; padding: 5px; font-size: 14px; padding: 8px 10px; position: relative; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #48576a; height: 36px; line-height: 1.5; box-sizing: border-box; cursor: pointer; &.active { background-color: #20a0ff; a { color: #fff; } } } } input { -webkit-appearance: none; -moz-appearance: none; appearance: none; background-color: #fff; background-image: none; border-radius: 4px; border: 1px solid #bfcbd9; box-sizing: border-box; color: #1f2d3d; font-size: inherit; height: 36px; line-height: 1; outline: 0; padding: 3px 10px; transition: border-color .2s cubic-bezier(.645, .045, .355, 1); width: 100%; display: inline-block; } .typeahead-header, .typeahead-content { width: 100%; } .typeahead-content { position: absolute; border-radius: 2px; background-color: #fff; box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04); box-sizing: border-box; margin: 5px 0; } .noFound { text-align: center; } } </style> 複製代碼

總結

自此,一個挺粗糙的模糊搜索組件就完成了,有更好的方案或者實現方法的能夠留言,謝謝閱讀。web

相關文章
相關標籤/搜索