icon-picker.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <script setup lang="ts">
  2. import type { VNode } from 'vue';
  3. import { computed, ref, useAttrs, watch, watchEffect } from 'vue';
  4. import { usePagination } from '@vben/hooks';
  5. import { EmptyIcon, Grip, listIcons } from '@vben/icons';
  6. import { $t } from '@vben/locales';
  7. import {
  8. Button,
  9. Input,
  10. Pagination,
  11. PaginationEllipsis,
  12. PaginationFirst,
  13. PaginationLast,
  14. PaginationList,
  15. PaginationListItem,
  16. PaginationNext,
  17. PaginationPrev,
  18. VbenIcon,
  19. VbenIconButton,
  20. VbenPopover,
  21. } from '@vben-core/shadcn-ui';
  22. import { isFunction } from '@vben-core/shared/utils';
  23. import { objectOmit, refDebounced, watchDebounced } from '@vueuse/core';
  24. import { fetchIconsData } from './icons';
  25. interface Props {
  26. pageSize?: number;
  27. /** 图标集的名字 */
  28. prefix?: string;
  29. /** 是否自动请求API以获得图标集的数据.提供prefix时有效 */
  30. autoFetchApi?: boolean;
  31. /**
  32. * 图标列表
  33. */
  34. icons?: string[];
  35. /** Input组件 */
  36. inputComponent?: VNode;
  37. /** 图标插槽名,预览图标将被渲染到此插槽中 */
  38. iconSlot?: string;
  39. /** input组件的值属性名称 */
  40. modelValueProp?: string;
  41. /** 图标样式 */
  42. iconClass?: string;
  43. type?: 'icon' | 'input';
  44. }
  45. const props = withDefaults(defineProps<Props>(), {
  46. prefix: 'ant-design',
  47. pageSize: 36,
  48. icons: () => [],
  49. iconSlot: 'default',
  50. iconClass: 'size-4',
  51. autoFetchApi: true,
  52. modelValueProp: 'modelValue',
  53. inputComponent: undefined,
  54. type: 'input',
  55. });
  56. const emit = defineEmits<{
  57. change: [string];
  58. }>();
  59. const attrs = useAttrs();
  60. const modelValue = defineModel({ default: '', type: String });
  61. const visible = ref(false);
  62. const currentSelect = ref('');
  63. const keyword = ref('');
  64. const keywordDebounce = refDebounced(keyword, 300);
  65. const innerIcons = ref<string[]>([]);
  66. watchDebounced(
  67. () => props.prefix,
  68. async (prefix) => {
  69. if (prefix && prefix !== 'svg' && props.autoFetchApi) {
  70. innerIcons.value = await fetchIconsData(prefix);
  71. }
  72. },
  73. { immediate: true, debounce: 500, maxWait: 1000 },
  74. );
  75. const currentList = computed(() => {
  76. try {
  77. if (props.prefix) {
  78. if (
  79. props.prefix !== 'svg' &&
  80. props.autoFetchApi &&
  81. props.icons.length === 0
  82. ) {
  83. return innerIcons.value;
  84. }
  85. const icons = listIcons('', props.prefix);
  86. if (icons.length === 0) {
  87. console.warn(`No icons found for prefix: ${props.prefix}`);
  88. }
  89. return icons;
  90. } else {
  91. return props.icons;
  92. }
  93. } catch (error) {
  94. console.error('Failed to load icons:', error);
  95. return [];
  96. }
  97. });
  98. const showList = computed(() => {
  99. return currentList.value.filter((item) =>
  100. item.includes(keywordDebounce.value),
  101. );
  102. });
  103. const { paginationList, total, setCurrentPage, currentPage } = usePagination(
  104. showList,
  105. props.pageSize,
  106. );
  107. watchEffect(() => {
  108. currentSelect.value = modelValue.value;
  109. });
  110. watch(
  111. () => currentSelect.value,
  112. (v) => {
  113. emit('change', v);
  114. },
  115. );
  116. const handleClick = (icon: string) => {
  117. currentSelect.value = icon;
  118. modelValue.value = icon;
  119. close();
  120. };
  121. const handlePageChange = (page: number) => {
  122. setCurrentPage(page);
  123. };
  124. function toggleOpenState() {
  125. visible.value = !visible.value;
  126. }
  127. function open() {
  128. visible.value = true;
  129. }
  130. function close() {
  131. visible.value = false;
  132. }
  133. function onKeywordChange(v: string) {
  134. keyword.value = v;
  135. }
  136. const searchInputProps = computed(() => {
  137. return {
  138. placeholder: $t('ui.iconPicker.search'),
  139. [props.modelValueProp]: keyword.value,
  140. [`onUpdate:${props.modelValueProp}`]: onKeywordChange,
  141. class: 'mx-2',
  142. };
  143. });
  144. function updateCurrentSelect(v: string) {
  145. currentSelect.value = v;
  146. const eventKey = `onUpdate:${props.modelValueProp}`;
  147. if (attrs[eventKey] && isFunction(attrs[eventKey])) {
  148. attrs[eventKey](v);
  149. }
  150. }
  151. const getBindAttrs = computed(() => {
  152. return objectOmit(attrs, [`onUpdate:${props.modelValueProp}`]);
  153. });
  154. defineExpose({ toggleOpenState, open, close });
  155. </script>
  156. <template>
  157. <VbenPopover
  158. v-model:open="visible"
  159. :content-props="{ align: 'end', alignOffset: -11, sideOffset: 8 }"
  160. content-class="p-0 pt-3 w-full"
  161. trigger-class="w-full"
  162. >
  163. <template #trigger>
  164. <template v-if="props.type === 'input'">
  165. <component
  166. v-if="props.inputComponent"
  167. :is="inputComponent"
  168. :[modelValueProp]="currentSelect"
  169. :placeholder="$t('ui.iconPicker.placeholder')"
  170. role="combobox"
  171. :aria-label="$t('ui.iconPicker.placeholder')"
  172. aria-expanded="visible"
  173. :[`onUpdate:${modelValueProp}`]="updateCurrentSelect"
  174. v-bind="getBindAttrs"
  175. >
  176. <template #[iconSlot]>
  177. <VbenIcon
  178. :icon="currentSelect || Grip"
  179. class="size-4"
  180. aria-hidden="true"
  181. />
  182. </template>
  183. </component>
  184. <div class="relative w-full" v-else>
  185. <Input
  186. v-bind="$attrs"
  187. v-model="currentSelect"
  188. :placeholder="$t('ui.iconPicker.placeholder')"
  189. class="h-8 w-full pr-8"
  190. role="combobox"
  191. :aria-label="$t('ui.iconPicker.placeholder')"
  192. aria-expanded="visible"
  193. />
  194. <VbenIcon
  195. :icon="currentSelect || Grip"
  196. class="absolute top-1 right-1 size-6"
  197. aria-hidden="true"
  198. />
  199. </div>
  200. </template>
  201. <VbenIcon
  202. :icon="currentSelect || Grip"
  203. v-else
  204. class="size-4"
  205. v-bind="$attrs"
  206. />
  207. </template>
  208. <div class="mb-2 flex w-full">
  209. <component
  210. v-if="inputComponent"
  211. :is="inputComponent"
  212. v-bind="searchInputProps"
  213. />
  214. <Input
  215. v-else
  216. class="mx-2 h-8 w-full"
  217. :placeholder="$t('ui.iconPicker.search')"
  218. v-model="keyword"
  219. />
  220. </div>
  221. <template v-if="paginationList.length > 0">
  222. <div class="grid max-h-90 w-full grid-cols-6 justify-items-center">
  223. <VbenIconButton
  224. v-for="(item, index) in paginationList"
  225. :key="index"
  226. :tooltip="item"
  227. tooltip-side="top"
  228. @click="handleClick(item)"
  229. >
  230. <VbenIcon
  231. :class="{
  232. 'text-primary transition-all': currentSelect === item,
  233. }"
  234. :icon="item"
  235. />
  236. </VbenIconButton>
  237. </div>
  238. <div
  239. v-if="total >= pageSize"
  240. class="flex-center flex justify-end overflow-hidden border-t py-2 pr-3"
  241. >
  242. <Pagination
  243. :items-per-page="36"
  244. :sibling-count="1"
  245. :total="total"
  246. show-edges
  247. size="small"
  248. @update:page="handlePageChange"
  249. >
  250. <PaginationList
  251. v-slot="{ items }"
  252. class="flex w-full items-center gap-1"
  253. >
  254. <PaginationFirst class="size-5" />
  255. <PaginationPrev class="size-5" />
  256. <template v-for="(item, index) in items">
  257. <PaginationListItem
  258. v-if="item.type === 'page'"
  259. :key="index"
  260. :value="item.value"
  261. as-child
  262. >
  263. <Button
  264. :variant="item.value === currentPage ? 'default' : 'outline'"
  265. class="size-5 p-0 text-sm"
  266. >
  267. {{ item.value }}
  268. </Button>
  269. </PaginationListItem>
  270. <PaginationEllipsis
  271. v-else
  272. :key="item.type"
  273. :index="index"
  274. class="size-5"
  275. />
  276. </template>
  277. <PaginationNext class="size-5" />
  278. <PaginationLast class="size-5" />
  279. </PaginationList>
  280. </Pagination>
  281. </div>
  282. </template>
  283. <template v-else>
  284. <div class="flex-col-center min-h-37.5 w-full text-muted-foreground">
  285. <EmptyIcon class="size-10" />
  286. <div class="mt-1 text-sm">{{ $t('common.noData') }}</div>
  287. </div>
  288. </template>
  289. </VbenPopover>
  290. </template>