【Vue3】如何封装一个支持输入和单/多选InputSelect组件-Antd

写在前面:

Antd提供的select组件是不支持当作输入框使用的,并且不支持在一级状态下手动输入选项,所以就封装了一个和通用组件来实现上述功能。

组件效果:

单选模式:

多选模式:

实现步骤:

1.首先定义一个searchText存储用户输入的数据,因为会触发一个Antd定义的@Search事件,所以直接在onSearch数据收集即可

js 复制代码
const searchText = ref('')
const onSearch = val => {
  // 去除首尾空格
  const text = val.trim()
  if (text) {
    searchText.value = text
  }
}

2.定义三个prop控制输入的数据,组件模式以及是否允许输入

js 复制代码
const props = defineProps({
  value: {
    type: [Array, String, Number],
    default: undefined
  },
  mode: {
    type: String,
    default: ''
  },
  allowInputValue: {
    type: Boolean,
    default: true
  }
})

3.在组件失焦时完成数据传递,其实组件功能的核心就是在失焦时完成

首先,通过条件判断allowInputValue.value && searchText.value判断是否允许输入且存在搜索文本。 如果条件成立,继续执行下面的逻辑。

接着,通过判断tempMode.value === 'multiple'判断选择模式是否为多选。

如果选择模式为多选,则将搜索文本searchText.value添加到tempValue.value的数组中,即将搜索文本作为一个新的选项添加到选中值中。

如果选择模式不是多选,则直接将搜索文本赋值给tempValue.value,即将搜索文本作为选中值。

然后,通过emit('change', tempValue.value)触发change事件,并将当前的选中值作为参数传递给父组件。

最后,将searchText.value清空,以便下一次输入。

js 复制代码
const emit = defineEmits(['update:value', 'update:mode', 'change'])
//组件失去焦点
const onBlur = () => {
  if (allowInputValue.value && searchText.value) {
    if (tempMode.value === 'multiple') {
      tempValue.value.push(searchText.value)
    } else {
      tempValue.value = searchText.value
    }
    emit('change', tempValue.value)
    searchText.value = ''
  }
}

4.定义$attrs以及slot支持进一步拓展及支持a-select原生api

js 复制代码
<template>
  <a-select
    v-bind="$attrs"
    v-model:value="tempValue"
    :mode="tempMode"
    @search="onSearch"
    @blur="onBlur"
    @change="onChange"
  >
    <template v-for="(item, key, index) in $slots" :key="index" #[key]>
      <slot :name="key"></slot>
    </template>
  </a-select>
</template>

完整代码:

js 复制代码
<template>
  <a-select
    v-bind="$attrs"
    v-model:value="tempValue"
    :mode="tempMode"
    @search="onSearch"
    @blur="onBlur"
    @change="onChange"
  >
    <template v-for="(item, key, index) in $slots" :key="index" #[key]>
      <slot :name="key"></slot>
    </template>
  </a-select>
</template>

<script setup>
import { computed, ref, toRefs } from 'vue'

const props = defineProps({
  value: {
    type: [Array, String, Number],
    default: undefined
  },
  mode: {
    type: String,
    default: ''
  },
  allowInputValue: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['update:value', 'update:mode', 'change'])

const { value, mode, allowInputValue } = toRefs(props)

const tempValue = computed({
  set: val => emit('update:value', val),
  get: () => value.value
})

const tempMode = computed({
  set: val => emit('update:mode', val),
  get: () => mode.value
})

const searchText = ref('')
const onSearch = val => {
  // 去除首尾
  const text = val.trim()
  if (text) {
    searchText.value = text
  }
}

const onBlur = () => {
  if (allowInputValue.value && searchText.value) {
    if (tempMode.value === 'multiple') {
      tempValue.value.push(searchText.value)
    } else {
      tempValue.value = searchText.value
    }
    emit('change', tempValue.value)
    searchText.value = ''
  }
}

const onChange = () => {
  emit('change', tempValue.value)
  searchText.value = ''
}
</script>

<style lang="scss" scoped></style>

使用范例:

单选:

js 复制代码
    <InputSelect
      v-model:value="record.name"
      placeholder="请选择或输入"
      :options="headerOptions"
      allow-clear
      show-search
      max-tag-count="responsive"
     />

多选:

js 复制代码
<InputSelect
  v-model:value="modelRef.actList"
  class="value-item"
  mode="multiple"
  placeholder="请选择或输入"
  :options="ACT_TYPES"
  allow-clear
  show-search
  max-tag-count="responsive"
  @change="onChange"
 />
相关推荐
yinuo20 分钟前
轻松接入大语言模型API -04
前端
袋鼠云数栈UED团队1 小时前
基于 Lexical 实现变量输入编辑器
前端·javascript·架构
cipher1 小时前
ERC-4626 通胀攻击:DeFi 金库的"捐款陷阱"
前端·后端·安全
UrbanJazzerati1 小时前
非常友好的Vue 3 生命周期详解
前端·面试
AAA阿giao1 小时前
从零构建一个现代登录页:深入解析 Tailwind CSS + Vite + Lucide React 的完整技术栈
前端·css·react.js
亦妤2 小时前
JS执行机制、作用域及作用域链
javascript
兆子龙2 小时前
像 React Hook 一样「自动触发」:用 Git Hook 拦住忘删的测试代码与其它翻车现场
前端·架构
兆子龙3 小时前
用 Auto.js 实现挂机脚本:从找图点击到循环自动化
前端·架构
SuperEugene3 小时前
表单最佳实践:从 v-model 到自定义表单组件(含校验)
前端·javascript·vue.js
昨晚我输给了一辆AE863 小时前
为什么现在不推荐使用 React.FC 了?
前端·react.js·typescript