index.vue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <div class="icon-box">
  3. <el-input
  4. ref="inputRef"
  5. v-model="valueIcon"
  6. v-bind="$attrs"
  7. :placeholder="placeholder"
  8. :clearable="clearable"
  9. @clear="clearIcon"
  10. @click="openDialog"
  11. >
  12. <template #append>
  13. <el-button :icon="customIcons[iconValue] || ''" />
  14. </template>
  15. </el-input>
  16. <el-dialog v-model="dialogVisible" :title="placeholder" top="10%" width="40%">
  17. <el-input v-model="inputValue" placeholder="搜索图标" size="large" :prefix-icon="Icons.Search" />
  18. <el-scrollbar v-if="Object.keys(iconsList).length">
  19. <div class="icon-list">
  20. <div v-for="item in iconsList" :key="item" class="icon-item" @click="selectIcon(item)">
  21. <component :is="item"></component>
  22. <span>{{ item.name }}</span>
  23. </div>
  24. </div>
  25. </el-scrollbar>
  26. <el-empty v-else description="未搜索到您要找的图标~" />
  27. </el-dialog>
  28. </div>
  29. </template>
  30. <script setup lang="ts" name="SelectIcon">
  31. import * as Icons from '@element-plus/icons-vue'
  32. interface SelectIconProps {
  33. iconValue: string
  34. title?: string
  35. clearable?: boolean
  36. placeholder?: string
  37. }
  38. const props = withDefaults(defineProps<SelectIconProps>(), {
  39. iconValue: '',
  40. title: '请选择图标',
  41. clearable: true,
  42. placeholder: '请选择图标'
  43. })
  44. // 重新接收一下,防止打包后 clearable 报错
  45. const valueIcon = ref(props.iconValue)
  46. // open Dialog
  47. const dialogVisible = ref(false)
  48. const openDialog = () => (dialogVisible.value = true)
  49. // 选择图标(触发更新父组件数据)
  50. const emit = defineEmits<{
  51. 'update:iconValue': [value: string]
  52. }>()
  53. const selectIcon = (item: any) => {
  54. dialogVisible.value = false
  55. valueIcon.value = item.name
  56. emit('update:iconValue', item.name)
  57. setTimeout(() => inputRef.value.blur(), 0)
  58. }
  59. // 清空图标
  60. const inputRef = ref()
  61. const clearIcon = () => {
  62. valueIcon.value = ''
  63. emit('update:iconValue', '')
  64. setTimeout(() => inputRef.value.blur(), 0)
  65. }
  66. // 监听搜索框值
  67. const inputValue = ref('')
  68. const customIcons: { [key: string]: any } = Icons
  69. const iconsList = computed((): { [key: string]: any } => {
  70. if (!inputValue.value) return Icons
  71. let result: { [key: string]: any } = {}
  72. for (const key in customIcons) {
  73. if (key.toLowerCase().indexOf(inputValue.value.toLowerCase()) > -1) result[key] = customIcons[key]
  74. }
  75. return result
  76. })
  77. </script>
  78. <style scoped lang="scss">
  79. @import './index.scss';
  80. </style>