当前位置: 首页 > news >正文

vue3+element-plus icons图标选择组件封装

一、最终效果

在这里插入图片描述

二、参数配置

1、代码示例

<t-select-icon v-model="selectVlaue" />

2、配置参数(Attributes)继承 el-input Attributes

参数说明类型默认值
v-model绑定值string-
prefixIcon输入框前缀iconstringSearch
isShowSearch是否显示搜索图标Booleantrue
isShowIcon是否显示选中后的图标Booleantrue
selectBindAttributesselectBind"prefix-icon": props.prefixIcon, placeholder: "请选择图标",dialogTitle:'请选择图标',clearable: true,width: "50%"

3、events

事件名说明返回值
select选择完图标返回选中的图标name

三、源码

<template><div class="t_select_icon"><el-inputref="inputRef"v-model="valueIcon"v-bind="attrs"@clear="clearIcon"@click="() => {;(dialogVisible = true), (inputValue = '')}"><template #append v-if="isShowIcon"><el-button :icon="customIcons[modelValue]" /></template></el-input><el-dialog v-model="dialogVisible" :title="attrs.dialogTitle" draggable :width="attrs.width"><el-inputv-model="inputValue"v-if="isShowSearch":placeholder="attrs.searchPlaceholder"clearable:prefix-icon="Icons.Search"/><el-scrollbar v-if="Object.keys(iconsList).length"><div class="icon-list"><divv-for="item in iconsList":key="item"class="icon-item":class="{ 'icon-active': item.name == valueIcon }"@click="selectIcon(item)"><component :is="item"></component><span>{{ item.name }}</span></div></div></el-scrollbar><el-empty :description="attrs.emptyDescription" v-else /></el-dialog></div>
</template><script setup lang="ts" name="TSelectIcon">
import { ref, computed, useAttrs } from "vue"
import * as Icons from "@element-plus/icons-vue"const props = defineProps({modelValue: {type: String,default: ""},prefixIcon: {type: String,default: "Search"},selectBind: Object,isShowSearch: {type: Boolean,default: true},isShowIcon: {type: Boolean,default: true}
})
const emit = defineEmits(["update:modelValue", "select"])
// v-model简写
let valueIcon = computed({get() {return props.modelValue},set(val) {// console.log("v-model简写", val);emit("update:modelValue", val)}
})
const $attrs: any = useAttrs()
const attrs = computed(() => {const selectBind = {"prefix-icon": customIcons[props.prefixIcon],placeholder: "请选择图标",dialogTitle: "请选择图标",searchPlaceholder: "搜索图标",emptyDescription: "未搜索到您要找的图标",clearable: true,width: "50%",...props.selectBind}return { ...$attrs, ...selectBind }
})
// open Dialog
const dialogVisible = ref(false)// 选择图标
const selectIcon = (item: any) => {dialogVisible.value = falsevalueIcon.value = item.nameemit("update:modelValue", item.name)emit("select", item.name)
}// 清空图标
const inputRef = ref()
const clearIcon = () => {valueIcon.value = ""emit("update:modelValue", "")setTimeout(() => inputRef.value.blur(), 0)
}// 监听搜索框值
const inputValue = ref("")
const customIcons: { [key: string]: any } = Icons
const iconsList = computed((): { [key: string]: any } => {if (!inputValue.value) return Iconslet result: { [key: string]: any } = {}for (const key in customIcons) {if (key.toLowerCase().indexOf(inputValue.value.toLowerCase()) > -1)result[key] = customIcons[key]}return result
})
</script><style scoped lang="scss">
.t_select_icon {width: 100%;.el-button {display: flex;align-items: center;justify-content: center;font-size: 18px;color: var(--el-text-color-regular);:deep(.el-icon) {color: var(--el-color-primary);}}:deep(.el-dialog__body) {padding: 25px 20px 20px;.el-input {margin-bottom: 10px;}.icon-list {display: grid;grid-template-columns: repeat(auto-fill, 115px);justify-content: space-evenly;max-height: 60vh;.icon-item {display: flex;flex-direction: column;align-items: center;width: 42px;padding: 20px 30px;cursor: pointer;transition: all 0.2s;svg {width: 30px;height: 30px;}&:hover {transform: scale(1.3);}&.icon-active {color: var(--el-color-primary);}span {margin-top: 5px;line-height: 20px;text-align: center;}}}}
}
</style>

六、组件地址

gitHub组件地址

gitee码云组件地址

七、相关文章

基于ElementUi&antdUi再次封装基础组件文档


vue3+ts基于Element-plus再次封装基础组件文档

http://www.lryc.cn/news/446746.html

相关文章:

  • Spring validation校验框架
  • UBUNTU20.04安装CH384串口卡驱动
  • JWT(JSON Web Tokens) 详细介绍
  • 数据结构练习题————(二叉树)——考前必备合集!
  • 一天认识一个硬件之鼠标
  • Django 请求配置
  • 轮播图组件更加完善版
  • cpu路、核、线程
  • 鸿蒙开发(NEXT/API 12)【硬件(注册出行业务事件监听)】车载系统
  • 安卓中有main函数吗?
  • js-17-对数组、对象进行浅拷贝和深拷贝
  • Ubuntu24.04中安装Electron
  • CPU中也应用到了缓存:CPU3层高速缓存,以及它的缓存一致性问题、MESI协议和Java的一些应用
  • 如何使用开发者工具捕获鼠标右键点击事件
  • 【几何】个人练习-Leetcode-1453. Maximum Number of Darts Inside of a Circular Dartboard
  • 啤酒:从饮品到文化的演变
  • Java 中 Map 常用类和数据结构详解
  • 实时监控,动态调整 —— 淘宝商品详情API助力商家实现灵活经营
  • WebGL常用接口和事件
  • Golang | Leetcode Golang题解之第429题N叉树的层序遍历
  • 数据库的全透明加密和半透明加密主要是针对数据存储安全的不同处理方式
  • MySQL的登录、访问、退出
  • 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-09-25
  • PyTorch框架安装
  • 分布式锁优化之 使用lua脚本改造分布式锁保证判断和删除的原子性(优化之LUA脚本保证删除的原子性)
  • 从安防视频监控行业发展趋势看EasyCVR平台如何驱动行业智能升级
  • TIOBE 编程指数 9 月排行榜公布 VB.Net第七
  • 如何用ChatGPT制作一款手机游戏应用
  • 0基础学前端 day4
  • 功能测试详解