封装组件之使用vue3封装input框并显示联想数据功能

新建Input.vue

定义input框及相关事件

 <input
      type="text"
      v-model="query"
      @input="onInput"
      @blur="hideSuggestions"
      @focus="onInput"
 />

@input输入事件、@blur失去焦点、@focus获取焦点

//当输入内容时才显示ul内容
const onInput = () => {
  showSuggestions.value = query.value.trim() !== '';
};
const hideSuggestions = () => {
  setTimeout(() => {
    showSuggestions.value = false;
  }, 200); // 延迟隐藏以允许选择建议
};

 

定义组件属性

const props = defineProps<{
  suggestions: string[];
}>();

ul标签是否显示

const showSuggestions = ref(false);
 <ul
      v-if="showSuggestions && filteredSuggestions.length"
      class="suggestions"
    >
      <li
        v-for="(suggestion, index) in filteredSuggestions"
        :key="index"
        @mousedown="selectSuggestion(suggestion)"
      >
        {{ suggestion }}
      </li>
    </ul>

进行筛选

const filteredSuggestions = computed(() => {
  if (query.value.trim() === '') {
    return [];
  }
  return props.suggestions.filter((suggestion) =>
    suggestion.toLowerCase().includes(query.value.toLowerCase())
  );
});

选中内容

//选中内容
const selectSuggestion = (suggestion: string) => {
  query.value = suggestion;
  showSuggestions.value = false;
};

其他组件使用

引入

import AutoInput from './components/Input.vue';

使用

const suggestionsList = [
  'apple',
  'banana',
  'orange',
  'grape',
  'pineapple',
  'mango',
  'strawberry',
];
 <div>
    <AutoInput :suggestions="suggestionsList"></AutoInput>
  </div>

效果

完整代码

Input.vue

<template>
  <div class="autocomplete">
    <input
      type="text"
      v-model="query"
      @input="onInput"
      @blur="hideSuggestions"
      @focus="onInput"
    />
    <ul
      v-if="showSuggestions && filteredSuggestions.length"
      class="suggestions"
    >
      <li
        v-for="(suggestion, index) in filteredSuggestions"
        :key="index"
        @mousedown="selectSuggestion(suggestion)"
      >
        {{ suggestion }}
      </li>
    </ul>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed } from 'vue';

const props = defineProps<{
  suggestions: string[];
}>();

const query = ref('');
const showSuggestions = ref(false);

const filteredSuggestions = computed(() => {
  if (query.value.trim() === '') {
    return [];
  }
  return props.suggestions.filter((suggestion) =>
    suggestion.toLowerCase().includes(query.value.toLowerCase())
  );
});

//当输入内容时才显示ul内容
const onInput = () => {
  showSuggestions.value = query.value.trim() !== '';
};

const hideSuggestions = () => {
  setTimeout(() => {
    showSuggestions.value = false;
  }, 200); // 延迟隐藏以允许选择建议
};

//选中内容
const selectSuggestion = (suggestion: string) => {
  query.value = suggestion;
  showSuggestions.value = false;
};
</script>

<style scoped>
.autocomplete {
  position: relative;
  display: inline-block;
  width: 100%;
}
input {
  width: 100%;
  padding: 8px;
  box-sizing: border-box;
}
//设置垂直滚动条
.suggestions {
  position: absolute;
  border: 1px solid #ddd;
  border-top: none;
  z-index: 1000;
  background-color: #fff;
  width: 100%;
  box-sizing: border-box;
  max-height: 150px;
  overflow-y: auto;
}
.suggestions li {
  padding: 8px;
  cursor: pointer;
}
//鼠标悬停改变背景颜色
.suggestions li:hover {
  background-color: #f0f0f0;
}
li {
  list-style-type: none;
}
</style>

 其他组件

<script setup lang="ts">
import AutoInput from './components/Input.vue';
const suggestionsList = [
  'apple',
  'banana',
  'orange',
  'grape',
  'pineapple',
  'mango',
  'strawberry',
];
</script>

<template>
  <div>
    <AutoInput :suggestions="suggestionsList"></AutoInput>
  </div>
</template>

<style scoped></style>

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值