index.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <div class="icon-select">
  3. <el-input v-model="iconName" :disabled="disabled" clearable placeholder="请输入图标名称" @clear="filterIcons" @input="filterIcons">
  4. <template #suffix><i class="el-icon-search el-input__icon" /></template>
  5. </el-input>
  6. <div class="icon-select__list">
  7. <div
  8. v-for="(item, index) in iconList"
  9. :key="index"
  10. :title="item"
  11. :class="`item ${item === iconName ? 'active' : ''}`"
  12. @click="updateIcon(item)"
  13. >
  14. <svg-icon class="local_icon" :icon-class="item" />
  15. <span class="text text-overflow_ellipsis">{{ item }}</span>
  16. </div>
  17. </div>
  18. </div>
  19. </template>
  20. <script setup lang="ts">
  21. import { ref, watch } from 'vue'
  22. const props = defineProps({
  23. modelValue: {
  24. type: String,
  25. default: ''
  26. },
  27. disabled: {
  28. type: Boolean,
  29. default: false
  30. }
  31. })
  32. const icons = [] as string[]
  33. const modules = import.meta.glob('../../assets/icons/*.svg')
  34. for (const path in modules) {
  35. const p = path.split('assets/icons/')[1].split('.svg')[0]
  36. icons.push(p)
  37. }
  38. const iconList = ref(icons)
  39. const iconName = ref('')
  40. watch(
  41. () => props.modelValue,
  42. (val: string) => {
  43. iconName.value = val
  44. }
  45. )
  46. const emit = defineEmits(['update:modelValue'])
  47. function filterIcons() {
  48. iconList.value = icons
  49. if (iconName.value) {
  50. iconList.value = icons.filter(item => item.indexOf(iconName.value) !== -1)
  51. }
  52. }
  53. function updateIcon(name: string) {
  54. emit('update:modelValue', name)
  55. iconName.value = name
  56. document.body.click()
  57. }
  58. function reset() {
  59. iconName.value = ''
  60. iconList.value = icons
  61. }
  62. defineExpose({
  63. reset
  64. })
  65. </script>
  66. <style lang="scss" scoped>
  67. .icon-select {
  68. width: 100%;
  69. //padding: 10px;
  70. &__list {
  71. margin-top: 8px;
  72. height: 200px;
  73. overflow-y: scroll;
  74. .item {
  75. height: 30px;
  76. line-height: 30px;
  77. margin-bottom: -5px;
  78. cursor: pointer;
  79. width: 33.33%;
  80. float: left;
  81. color: #999;
  82. display: flex;
  83. align-items: center;
  84. //justify-content: ;
  85. &.active {
  86. color: var(--el-color-primary);
  87. }
  88. }
  89. .local_icon {
  90. height: 30px;
  91. width: 16px;
  92. margin-right: 5px;
  93. }
  94. .text {
  95. flex: 1;
  96. }
  97. }
  98. }
  99. </style>