Vue3后台管理系统(十六)图标选择器

目录

一、场景效果

二、添加图标

三、封装组件

四、使用案例


一、场景效果

二、添加图标

收集需要的图标集合,全部放到src/assets/icons文件夹中

 

三、封装组件

在src/components文件夹下新建IconSelect文件夹,并在IconSelect文件夹下新建index.vue

<template>
  <div class="icon-select">
    <el-input
      v-model="iconName"
      clearable
      placeholder="请输入图标名称"
      @clear="filterIcons"
      @input="filterIcons"
    >
      <template #suffix><i class="el-icon-search el-input__icon" /></template>
    </el-input>
    <div class="icon-select__list">
      <div
        v-for="(item, index) in iconList"
        :key="index"
        @click="selectedIcon(item)"
      >
        <svg-icon
          color="#999"
          :icon-class="item"
          style="height: 30px; width: 16px; margin-right: 5px"
        />
        <span>{{ item }}</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import SvgIcon from '@/components/SvgIcon/index.vue';

const icons = [] as string[];
const modules = import.meta.glob('../../assets/icons/*.svg');
for (const path in modules) {
  const p = path.split('assets/icons/')[1].split('.svg')[0];
  icons.push(p);
}
const iconList = ref(icons);

const iconName = ref('');

const emit = defineEmits(['selected']);

function filterIcons() {
  iconList.value = icons;
  if (iconName.value) {
    iconList.value = icons.filter(item => item.indexOf(iconName.value) !== -1);
  }
}

function selectedIcon(name: string) {
  emit('selected', name);
  document.body.click();
}

function reset() {
  iconName.value = '';
  iconList.value = icons;
}

defineExpose({
  reset
});
</script>

<style lang="scss" scoped>
.icon-select {
  width: 100%;
  padding: 10px;

  &__list {
    height: 200px;
    overflow-y: scroll;

    div {
      height: 30px;
      line-height: 30px;
      margin-bottom: -5px;
      cursor: pointer;
      width: 33%;
      float: left;
    }

    span {
      display: inline-block;
      vertical-align: -0.15em;
      fill: currentColor;
      overflow: hidden;
    }
  }
}
</style>

四、使用案例

<!--src/App.vue-->
<script setup lang="ts">
import IconSelect from '@/components/IconSelect/index.vue'
import {reactive, ref} from "vue";

const state = reactive({
  icon: '',
})

function selected(name: string) {
  console.log(state.icon)
  state.icon = name;
}

</script>

<template>
  <div>
    <icon-select @selected="selected"></icon-select>
  </div>
</template>

<style>

</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

文子阳

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

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

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

打赏作者

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

抵扣说明:

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

余额充值