Vue3?封裝一個支持輸入和單/多選InputSelect組件-Antd詳解
寫在前面:
Antd提供的select組件是不支持當(dāng)作輸入框使用的,并且不支持在一級狀態(tài)下手動輸入選項,所以就封裝了一個和通用組件來實現(xiàn)上述功能。
組件效果:
單選模式:
多選模式:
實現(xiàn)步驟:
1.首先定義一個searchText
存儲用戶輸入的數(shù)據(jù),因為會觸發(fā)一個Antd
定義的@Search
事件,所以直接在onSearch
數(shù)據(jù)收集即可
const searchText = ref('') const onSearch = val => { // 去除首尾空格 const text = val.trim() if (text) { searchText.value = text } }
2.定義三個prop
控制輸入的數(shù)據(jù),組件模式以及是否允許輸入
const props = defineProps({ value: { type: [Array, String, Number], default: undefined }, mode: { type: String, default: '' }, allowInputValue: { type: Boolean, default: true } })
3.在組件失焦時完成數(shù)據(jù)傳遞,其實組件功能的核心就是在失焦時完成
首先,通過條件判斷allowInputValue.value && searchText.value
判斷是否允許輸入且存在搜索文本。 如果條件成立,繼續(xù)執(zhí)行下面的邏輯。
接著,通過判斷tempMode.value === 'multiple'
判斷選擇模式是否為多選。
如果選擇模式為多選,則將搜索文本searchText.value
添加到tempValue.value
的數(shù)組中,即將搜索文本作為一個新的選項添加到選中值中。
如果選擇模式不是多選,則直接將搜索文本賦值給tempValue.value
,即將搜索文本作為選中值。
然后,通過emit('change', tempValue.value)
觸發(fā)change
事件,并將當(dāng)前的選中值作為參數(shù)傳遞給父組件。
最后,將searchText.value
清空,以便下一次輸入。
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
支持進(jìn)一步拓展及支持a-select原生api
<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>
完整代碼:
<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>
使用范例:
單選:
<InputSelect v-model:value="record.name" placeholder="請選擇或輸入" :options="headerOptions" allow-clear show-search max-tag-count="responsive" />
多選:
<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" />
到此這篇關(guān)于Vue3 封裝一個支持輸入和單/多選InputSelect組件-Antd詳解的文章就介紹到這了,更多相關(guān)Vue3 InputSelect組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于vue開發(fā)微信小程序mpvue-docs跳轉(zhuǎn)頁面功能
這篇文章主要介紹了基于vue寫微信小程序mpvue-docs跳轉(zhuǎn)頁面,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04Vue.js 時間轉(zhuǎn)換代碼及時間戳轉(zhuǎn)時間字符串
這篇文章主要介紹了Vue.js 時間轉(zhuǎn)換代碼及時間戳轉(zhuǎn)時間字符串,需要的朋友可以參考下2018-10-10在Vue3中使用vue3-print-nb實現(xiàn)前端打印功能
在前端開發(fā)中,經(jīng)常需要打印頁面的特定部分,比如客戶列表或商品詳情頁,要快速實現(xiàn)這些功能,可以使用 vue3-print-nb 插件,本文就給大家介紹了如何在 Vue 3 中使用 vue3-print-nb 實現(xiàn)靈活的前端打印,需要的朋友可以參考下2024-06-06使用vue-draggable-plus實現(xiàn)拖拽排序
最近遇到一個需求,在 Vue3 的一個 H5 頁面當(dāng)中點擊拖拽圖標(biāo)上下拖動 tab 子項,然后點擊保存可以保存最新的 tab 項順序,同事說可以用 vue-draggable-plus 這個庫來實現(xiàn)拖拽,所以本文給大家介紹了如何使用vue-draggable-plus實現(xiàn)拖拽排序,需要的朋友可以參考下2024-01-01