Vue 2.x折腾记 - (3)写一个不大靠谱的typeahead组件

前言

typeahead在网站中的应用很多…今天跟着我来写一个不大靠谱的typeahead;

你能学到什么?

  • 自定义事件
  • 遍历的思想
  • 功能细节的考虑

一切都挺不靠谱的,可完善的地方很多.废话不多说,看效果图

效果图

有哪些功能点?

  • 粗糙的模糊搜索 - 借助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,
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

crper

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值