icon-picker.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 currentPage = ref(1);
  64. const keyword = ref('');
  65. const keywordDebounce = refDebounced(keyword, 300);
  66. const innerIcons = ref<string[]>([]);
  67. watchDebounced(
  68. () => props.prefix,
  69. async (prefix) => {
  70. if (prefix && prefix !== 'svg' && props.autoFetchApi) {
  71. innerIcons.value = await fetchIconsData(prefix);
  72. }
  73. },
  74. { immediate: true, debounce: 500, maxWait: 1000 },
  75. );
  76. const currentList = computed(() => {
  77. try {
  78. if (props.prefix) {
  79. if (
  80. props.prefix !== 'svg' &&
  81. props.autoFetchApi &&
  82. props.icons.length === 0
  83. ) {
  84. return innerIcons.value;
  85. }
  86. const icons = listIcons('', props.prefix);
  87. if (icons.length === 0) {
  88. console.warn(`No icons found for prefix: ${props.prefix}`);
  89. }
  90. return icons;
  91. } else {
  92. return props.icons;
  93. }
  94. } catch (error) {
  95. console.error('Failed to load icons:', error);
  96. return [];
  97. }
  98. });
  99. const showList = computed(() => {
  100. return currentList.value.filter((item) =>
  101. item.includes(keywordDebounce.value),
  102. );
  103. });
  104. const { paginationList, total, setCurrentPage } = usePagination(
  105. showList,
  106. props.pageSize,
  107. );
  108. watchEffect(() => {
  109. currentSelect.value = modelValue.value;
  110. });
  111. watch(
  112. () => currentSelect.value,
  113. (v) => {
  114. emit('change', v);
  115. },
  116. );
  117. const handleClick = (icon: string) => {
  118. currentSelect.value = icon;
  119. modelValue.value = icon;
  120. close();
  121. };
  122. const handlePageChange = (page: number) => {
  123. currentPage.value = page;
  124. setCurrentPage(page);
  125. };
  126. function toggleOpenState() {
  127. visible.value = !visible.value;
  128. }
  129. function open() {
  130. visible.value = true;
  131. }
  132. function close() {
  133. visible.value = false;
  134. }
  135. function onKeywordChange(v: string) {
  136. keyword.value = v;
  137. }
  138. const searchInputProps = computed(() => {
  139. return {
  140. placeholder: $t('ui.iconPicker.search'),
  141. [props.modelValueProp]: keyword.value,
  142. [`onUpdate:${props.modelValueProp}`]: onKeywordChange,
  143. class: 'mx-2',
  144. };
  145. });
  146. function updateCurrentSelect(v: string) {
  147. currentSelect.value = v;
  148. const eventKey = `onUpdate:${props.modelValueProp}`;
  149. if (attrs[eventKey] && isFunction(attrs[eventKey])) {
  150. attrs[eventKey](v);
  151. }
  152. }
  153. const getBindAttrs = computed(() => {
  154. return objectOmit(attrs, [`onUpdate:${props.modelValueProp}`]);
  155. });
  156. defineExpose({ toggleOpenState, open, close });
  157. </script>
  158. <template>
  159. <VbenPopover
  160. v-model:open="visible"
  161. :content-props="{ align: 'end', alignOffset: -11, sideOffset: 8 }"
  162. content-class="p-0 pt-3 w-full"
  163. trigger-class="w-full"
  164. >
  165. <template #trigger>
  166. <template v-if="props.type === 'input'">
  167. <component
  168. v-if="props.inputComponent"
  169. :is="inputComponent"
  170. :[modelValueProp]="currentSelect"
  171. :placeholder="$t('ui.iconPicker.placeholder')"
  172. role="combobox"
  173. :aria-label="$t('ui.iconPicker.placeholder')"
  174. aria-expanded="visible"
  175. :[`onUpdate:${modelValueProp}`]="updateCurrentSelect"
  176. v-bind="getBindAttrs"
  177. >
  178. <template #[iconSlot]>
  179. <VbenIcon
  180. :icon="currentSelect || Grip"
  181. class="size-4"
  182. aria-hidden="true"
  183. />
  184. </template>
  185. </component>
  186. <div class="relative w-full" v-else>
  187. <Input
  188. v-bind="$attrs"
  189. v-model="currentSelect"
  190. :placeholder="$t('ui.iconPicker.placeholder')"
  191. class="h-8 w-full pr-8"
  192. role="combobox"
  193. :aria-label="$t('ui.iconPicker.placeholder')"
  194. aria-expanded="visible"
  195. />
  196. <VbenIcon
  197. :icon="currentSelect || Grip"
  198. class="absolute right-1 top-1 size-6"
  199. aria-hidden="true"
  200. />
  201. </div>
  202. </template>
  203. <VbenIcon
  204. :icon="currentSelect || Grip"
  205. v-else
  206. class="size-4"
  207. v-bind="$attrs"
  208. />
  209. </template>
  210. <div class="mb-2 flex w-full">
  211. <component
  212. v-if="inputComponent"
  213. :is="inputComponent"
  214. v-bind="searchInputProps"
  215. />
  216. <Input
  217. v-else
  218. class="mx-2 h-8 w-full"
  219. :placeholder="$t('ui.iconPicker.search')"
  220. v-model="keyword"
  221. />
  222. </div>
  223. <template v-if="paginationList.length > 0">
  224. <div class="grid max-h-[360px] w-full grid-cols-6 justify-items-center">
  225. <VbenIconButton
  226. v-for="(item, index) in paginationList"
  227. :key="index"
  228. :tooltip="item"
  229. tooltip-side="top"
  230. @click="handleClick(item)"
  231. >
  232. <VbenIcon
  233. :class="{
  234. 'text-primary transition-all': currentSelect === item,
  235. }"
  236. :icon="item"
  237. />
  238. </VbenIconButton>
  239. </div>
  240. <div
  241. v-if="total >= pageSize"
  242. class="flex-center flex justify-end overflow-hidden border-t py-2 pr-3"
  243. >
  244. <Pagination
  245. :items-per-page="36"
  246. :sibling-count="1"
  247. :total="total"
  248. show-edges
  249. size="small"
  250. @update:page="handlePageChange"
  251. >
  252. <PaginationList
  253. v-slot="{ items }"
  254. class="flex w-full items-center gap-1"
  255. >
  256. <PaginationFirst class="size-5" />
  257. <PaginationPrev class="size-5" />
  258. <template v-for="(item, index) in items">
  259. <PaginationListItem
  260. v-if="item.type === 'page'"
  261. :key="index"
  262. :value="item.value"
  263. as-child
  264. >
  265. <Button
  266. :variant="item.value === currentPage ? 'default' : 'outline'"
  267. class="size-5 p-0 text-sm"
  268. >
  269. {{ item.value }}
  270. </Button>
  271. </PaginationListItem>
  272. <PaginationEllipsis
  273. v-else
  274. :key="item.type"
  275. :index="index"
  276. class="size-5"
  277. />
  278. </template>
  279. <PaginationNext class="size-5" />
  280. <PaginationLast class="size-5" />
  281. </PaginationList>
  282. </Pagination>
  283. </div>
  284. </template>
  285. <template v-else>
  286. <div class="flex-col-center text-muted-foreground min-h-[150px] w-full">
  287. <EmptyIcon class="size-10" />
  288. <div class="mt-1 text-sm">{{ $t('common.noData') }}</div>
  289. </div>
  290. </template>
  291. </VbenPopover>
  292. </template>