ElementUI 实现图标选择器

本文详细介绍了如何利用ElementUI库,结合其内置的图标资源,开发一个图标选择器功能。通过步骤分解,读者可以了解到如何配置组件,实现图标预览及选择,为前端项目提供便捷的图标管理解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

<!-- 图标选择器
<select-icon v-model=[绑定变量] :data=[选项数据] />
-->

<template>
  <el-popover
    placement="bottom-start"
    popper-class="pupop-select-icon"
    transition="el-zoom-in-top"
    trigger="click"
    v-model="popoverVisible"
    :disabled="disabledSelected">
    <!-- 弹出框内容区 -->
    <el-scrollbar
      v-if="popoverVisible"
      class="hide-x"
      :native="false"
      :noresize="false">
      <!-- 图标项 -->
      <div
        class="icon-item"
        v-for="item in options"
        :key="item"
        :class="{ 'is-active': isActive(item) }"
        @click="onClickSelected(item)">
        <i :class="item"></i>
      </div>
    </el-scrollbar>
    <!-- 页面显示内容区 -->
    <template slot="reference">
      <div :class="{
        'mod-select-icon': 1,
        'is-opened': popoverVisible,
        'is-active': value,
        'is-disabled': disabledSelected
        }">
        <!-- 显示图标 -->
        <div class="icon-item">
          <i :class="value || 'el-icon-plus'"></i>
        </div>
        <!-- 清空按钮 -->
        <div v-show="value" class="btn-clear">
          <i class="el-icon-close" @click.stop="onClickClear()"></i>
        </div>
      </div>
    </template>
  </el-popover>
</template>

<script>
export default {
  name: 'SelectIcon',
  // 设置绑定参数
  model: {
    prop: 'value',
    event: 'selected',
  },
  props: {
    disabled: Boolean,
    // 接收绑定参数 - 图标类名
    value: {
      type: String,
      required: true,
    },
    // 选项数据,图标类名数组
    options: {
      type: Array,
      default: () => ([]),
    },
  },
  computed: {
    disabledSelected() {
      if (this.disabled) return true;
      return this.$parent.form ? this.$parent.form.disabled : false;
    },
  },
  data() {
    return {
      // 弹出框显示状态
      popoverVisible: false,
    };
  },
  methods: {
    // 是否为当前已选项
    isActive(item) {
      return this.value === item;
    },
    // 选中图标
    onClickSelected(item) {
      this.$emit('selected', item);
      this.popoverVisible = false;
    },
    // 清空选项
    onClickClear() {
      this.$emit('selected', '');
    },
  },
};
</script>

<style lang="sass">
.mod-select-icon {
  $size: 40px;    // 图标项大小
  $col-count: 8;  // 多少列
  $row-count: 4;  // 多少行
  $scope: 5px;    // 间隔

  position: relative;
  display: inline-block;
  width: $size + 2px;
  height: $size + 2px;
  border: 1px dashed $--border-color-base;
  border-radius: 5px;
  text-align: center;
  cursor: pointer;
  outline: none;

  // 菜单打开状态
  &.is-opened, &:hover { border-color: $--color-primary; }
  // 禁用状态
  &.is-disabled:hover { border-color: $--border-color-base !important; } 
  &.is-disabled,
  &.is-disabled > .icon-item,
  &.is-disabled > .btn-clear {
    @extend .z-not-allowed;
    background-color: $--background-color-base;
  }
  // 已选状态
  &.is-active {
    border-style: solid;
    border-radius: 0;
    > .icon-item {
      width: $size;
      height: $size;
      line-height: $size;
      padding: $scope;
      text-align: center;
      cursor: pointer;
      > i {
        display: block;
        width: 100%;
        height: 100%;
        line-height: $size - ($scope * 2);
        color: $--color-white;
        background-color: $--color-primary;
      }
    }
  }
  > .icon-item > i { font-size: 16px; }
  > .icon-item > i.el-icon-plus {
    width: 100%;
    line-height: $size;
    font-size: ($size / 2);
    font-weight: bold;
    color: $--color-info;
    cursor: inherit;
  }

  // 清空按钮
  .btn-clear {
    width: 0;
    height: 0;
    border-width: ($size / 2) 0 0 ($size / 2);
    border-style: solid;
    border-color: $--color-danger transparent transparent transparent;
    position: absolute;
    top: 0;
    right: 0;
    cursor: pointer;
    > i.el-icon-close {
      position: absolute;
      top: -($size / 2);
      right: 0;
      color: $--color-white;
      font-size: .7em;
      &:hover { color: darken($--color-white, 5%); }
    }
  }

  // 弹出内容
  @at-root .el-popover.el-popper.pupop-select-icon {
    display: block;
    padding: 0;
    width: ($size + $scope * 2) * $col-count + 2px;
    height: ($size + $scope * 2) * $row-count;

    > .el-scrollbar { height: 100%; }
    .el-scrollbar__view {
      @extend .fn-clear;
    }

    .icon-item {
      float: left;
      width: $size;
      height: $size;
      line-height: $size;
      margin: $scope;
      padding: $scope;
      text-align: center;
      cursor: pointer;
      &:hover { background-color: $--color-info-light; }
      &.is-active {
        background-color: $--color-success-light;
        border: 1px solid $--color-success;
      }
      > i {
        display: block;
        width: 100%;
        height: 100%;
        font-size: 16px;
        line-height: $size - ($scope * 2);
        color: $--color-white;
        background-color: $--color-primary;
      }
    }
  }
}
</style>
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值