feat: form component IconPicker (#5005)

This commit is contained in:
Netfan
2024-12-04 21:42:21 +08:00
committed by GitHub
parent 935df713f3
commit e23486dbc6
12 changed files with 149 additions and 920 deletions

View File

@@ -1,10 +1,12 @@
<script setup lang="ts">
import { ref, useTemplateRef, watch, watchEffect } from 'vue';
import { computed, ref, watch, watchEffect } from 'vue';
import { usePagination } from '@vben/hooks';
import { EmptyIcon, Grip } from '@vben/icons';
import { EmptyIcon, Grip, listIcons } from '@vben/icons';
import { $t } from '@vben/locales';
import {
Button,
Input,
Pagination,
PaginationEllipsis,
PaginationFirst,
@@ -18,9 +20,11 @@ import {
VbenPopover,
} from '@vben-core/shadcn-ui';
import { refDebounced } from '@vueuse/core';
interface Props {
value?: string;
pageSize?: number;
prefix?: string;
/**
* 图标列表
*/
@@ -28,48 +32,65 @@ interface Props {
}
const props = withDefaults(defineProps<Props>(), {
value: '',
prefix: 'ant-design',
pageSize: 36,
icons: () => [],
});
const emit = defineEmits<{
change: [string];
'update:value': [string];
}>();
const refTrigger = useTemplateRef<HTMLElement>('refTrigger');
const currentSelect = ref('');
const currentList = ref(props.icons);
const currentPage = ref(1);
const modelValue = defineModel({ default: '', type: String });
watch(
() => props.icons,
(newIcons) => {
currentList.value = newIcons;
},
{ immediate: true },
);
const visible = ref(false);
const currentSelect = ref('');
const currentPage = ref(1);
const keyword = ref('');
const keywordDebounce = refDebounced(keyword, 300);
const currentList = computed(() => {
try {
if (props.prefix) {
const icons = listIcons('', props.prefix);
if (icons.length === 0) {
console.warn(`No icons found for prefix: ${props.prefix}`);
}
return icons;
} else {
return props.icons;
}
} catch (error) {
console.error('Failed to load icons:', error);
return [];
}
});
const showList = computed(() => {
return currentList.value.filter((item) =>
item.includes(keywordDebounce.value),
);
});
const { paginationList, total, setCurrentPage } = usePagination(
currentList,
showList,
props.pageSize,
);
watchEffect(() => {
currentSelect.value = props.value;
currentSelect.value = modelValue.value;
});
watch(
() => currentSelect.value,
(v) => {
emit('update:value', v);
emit('change', v);
},
);
const handleClick = (icon: string) => {
currentSelect.value = icon;
modelValue.value = icon;
close();
};
const handlePageChange = (page: number) => {
@@ -77,22 +98,46 @@ const handlePageChange = (page: number) => {
setCurrentPage(page);
};
function changeOpenState() {
refTrigger.value?.click?.();
function toggleOpenState() {
visible.value = !visible.value;
}
defineExpose({ changeOpenState });
function open() {
visible.value = true;
}
function close() {
visible.value = false;
}
defineExpose({ toggleOpenState, open, close });
</script>
<template>
<VbenPopover
v-model:open="visible"
:content-props="{ align: 'end', alignOffset: -11, sideOffset: 8 }"
content-class="p-0 pt-3"
>
<template #trigger>
<div ref="refTrigger">
<VbenIcon :icon="currentSelect || Grip" class="size-5" />
</div>
<slot :close="close" :icon="currentSelect" :open="open" name="trigger">
<div class="flex items-center gap-2">
<Input
:value="currentSelect"
class="flex-1 cursor-pointer"
v-bind="$attrs"
:placeholder="$t('ui.iconPicker.placeholder')"
/>
<VbenIcon :icon="currentSelect || Grip" class="size-8" />
</div>
</slot>
</template>
<div class="mb-2 flex w-full">
<Input
v-model="keyword"
:placeholder="$t('ui.iconPicker.search')"
class="mx-2"
/>
</div>
<template v-if="paginationList.length > 0">
<div class="grid max-h-[360px] w-full grid-cols-6 justify-items-center">