SIconSelectPopover2.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. const n = `<template>
  2. <div class="s-icon-select-common">
  3. <a-popover trigger="click" placement="bottomLeft" v-model:open="visible">
  4. <template #content>
  5. <div class="picker-panel" :style="{ width: panelWidth }">
  6. <a-input-search
  7. v-model:value="keyword"
  8. class="icon-search"
  9. :placeholder="$l('搜索图标')"
  10. allow-clear
  11. />
  12. <div class="icon-list">
  13. <div
  14. v-for="icon in filteredIcons"
  15. :key="icon"
  16. class="item-icon"
  17. :class="{ active: icon === value }"
  18. @click="pick(icon)"
  19. >
  20. <component :is="icon" class="icon" />
  21. <div class="name" :title="icon">{{ icon }}</div>
  22. </div>
  23. <div v-if="!filteredIcons.length" class="icon-empty">{{ $l('暂无数据') }}</div>
  24. </div>
  25. </div>
  26. </template>
  27. <a-input
  28. class="icon-input"
  29. :readonly="true"
  30. :value="value"
  31. :placeholder="placeholder"
  32. :style="{ width: inputWidth }"
  33. >
  34. <template #prefix>
  35. <component v-if="value" :is="value" class="prefix-icon" />
  36. </template>
  37. <template #suffix>
  38. <down-outlined class="select-arrow" :class="{ open: visible }" />
  39. </template>
  40. </a-input>
  41. </a-popover>
  42. </div>
  43. </template>
  44. <script setup>
  45. import { computed, ref, watch } from 'vue'
  46. import { filterIcons } from './icon-groups'
  47. const props = defineProps({
  48. // 选中图标名,支持 v-model:value
  49. value: {
  50. type: String,
  51. default: ''
  52. },
  53. // 面板宽度,单位 px
  54. width: {
  55. type: [Number, String],
  56. default: 280
  57. },
  58. // 搜索框占位文案
  59. placeholder: {
  60. type: String,
  61. default() {
  62. return window.$l('请选择图标')
  63. }
  64. },
  65. // 保留:下拉版不再需要弹窗标题,兼容旧调用方传参
  66. title: {
  67. type: String,
  68. default() {
  69. return window.$l('图标选择')
  70. }
  71. }
  72. })
  73. const emits = defineEmits(['update:value', 'change'])
  74. const visible = ref(false)
  75. const keyword = ref('')
  76. // width 允许 280 / '280' / '280px' / '50%' / '20rem':数字或无单位字符串补 px,其余原样使用
  77. const inputWidth = computed(() => {
  78. const raw = props.width
  79. if (typeof raw === 'number') return \`\${raw}px\`
  80. const str = String(raw ?? '').trim()
  81. if (!str) return '280px'
  82. return /^-?\\d+(\\.\\d+)?$/.test(str) ? \`\${str}px\` : str
  83. })
  84. // 面板比输入框宽,保证一行能放 5 个图标;输入框本身更宽时跟随输入框
  85. const panelWidth = computed(() => {
  86. const px = parseFloat(inputWidth.value)
  87. return \`\${Math.max(280, Number.isFinite(px) ? px : 0)}px\`
  88. })
  89. const filteredIcons = computed(() => filterIcons('all', keyword.value))
  90. // 面板收起就清空关键字,下次打开回到完整列表(下拉的常规行为)
  91. watch(visible, (val) => {
  92. if (!val) keyword.value = ''
  93. })
  94. const open = () => {
  95. visible.value = true
  96. }
  97. const pick = (icon) => {
  98. emits('update:value', icon)
  99. emits('change', icon)
  100. visible.value = false
  101. }
  102. defineExpose({ open })
  103. <\/script>
  104. <style lang="less" scoped>
  105. .s-icon-select-common {
  106. display: inline-block;
  107. }
  108. .icon-input {
  109. cursor: pointer;
  110. :deep(input.ant-input) {
  111. cursor: pointer;
  112. }
  113. }
  114. .prefix-icon {
  115. font-size: 16px;
  116. }
  117. .select-arrow {
  118. font-size: 12px;
  119. color: var(--s-text-color-secondary);
  120. transition: transform 0.2s;
  121. &.open {
  122. transform: rotate(180deg);
  123. }
  124. }
  125. .picker-panel {
  126. .icon-search {
  127. margin-bottom: 10px;
  128. }
  129. }
  130. .icon-list {
  131. max-height: 264px;
  132. overflow: hidden auto;
  133. display: flex;
  134. flex-wrap: wrap;
  135. align-content: flex-start;
  136. .item-icon {
  137. width: 64px;
  138. margin: 2px;
  139. padding: 8px 0 6px;
  140. cursor: pointer;
  141. border: 1px solid transparent;
  142. border-radius: 4px;
  143. box-sizing: border-box;
  144. display: flex;
  145. flex-direction: column;
  146. justify-content: center;
  147. align-items: center;
  148. &:hover {
  149. background: var(--s-bg-color);
  150. }
  151. &.active {
  152. border-color: var(--s-color);
  153. background: var(--s-color-bg);
  154. }
  155. .icon {
  156. font-size: 20px;
  157. }
  158. .name {
  159. width: 100%;
  160. margin-top: 6px;
  161. padding: 0 2px;
  162. font-size: 10px;
  163. line-height: 14px;
  164. text-align: center;
  165. color: var(--s-text-color-secondary);
  166. text-overflow: ellipsis;
  167. overflow: hidden;
  168. white-space: nowrap;
  169. box-sizing: border-box;
  170. }
  171. }
  172. }
  173. .icon-empty {
  174. width: 100%;
  175. padding: 30px 0;
  176. text-align: center;
  177. color: var(--s-text-color-secondary);
  178. }
  179. </style>
  180. `;
  181. export {
  182. n as default
  183. };