Commit e23486db by Netfan Committed by GitHub

feat: form component `IconPicker` (#5005)

1 parent 935df713
...@@ -8,7 +8,7 @@ import type { BaseFormComponentType } from '@vben/common-ui'; ...@@ -8,7 +8,7 @@ import type { BaseFormComponentType } from '@vben/common-ui';
import type { Component, SetupContext } from 'vue'; import type { Component, SetupContext } from 'vue';
import { h } from 'vue'; import { h } from 'vue';
import { globalShareState } from '@vben/common-ui'; import { globalShareState, IconPicker } from '@vben/common-ui';
import { $t } from '@vben/locales'; import { $t } from '@vben/locales';
import { import {
...@@ -54,6 +54,7 @@ export type ComponentType = ...@@ -54,6 +54,7 @@ export type ComponentType =
| 'DatePicker' | 'DatePicker'
| 'DefaultButton' | 'DefaultButton'
| 'Divider' | 'Divider'
| 'IconPicker'
| 'Input' | 'Input'
| 'InputNumber' | 'InputNumber'
| 'InputPassword' | 'InputPassword'
...@@ -87,6 +88,7 @@ async function initComponentAdapter() { ...@@ -87,6 +88,7 @@ async function initComponentAdapter() {
return h(Button, { ...props, attrs, type: 'default' }, slots); return h(Button, { ...props, attrs, type: 'default' }, slots);
}, },
Divider, Divider,
IconPicker,
Input: withDefaultPlaceholder(Input, 'input'), Input: withDefaultPlaceholder(Input, 'input'),
InputNumber: withDefaultPlaceholder(InputNumber, 'input'), InputNumber: withDefaultPlaceholder(InputNumber, 'input'),
InputPassword: withDefaultPlaceholder(InputPassword, 'input'), InputPassword: withDefaultPlaceholder(InputPassword, 'input'),
......
...@@ -8,7 +8,7 @@ import type { BaseFormComponentType } from '@vben/common-ui'; ...@@ -8,7 +8,7 @@ import type { BaseFormComponentType } from '@vben/common-ui';
import type { Component, SetupContext } from 'vue'; import type { Component, SetupContext } from 'vue';
import { h } from 'vue'; import { h } from 'vue';
import { globalShareState } from '@vben/common-ui'; import { globalShareState, IconPicker } from '@vben/common-ui';
import { $t } from '@vben/locales'; import { $t } from '@vben/locales';
import { import {
...@@ -45,6 +45,7 @@ export type ComponentType = ...@@ -45,6 +45,7 @@ export type ComponentType =
| 'CheckboxGroup' | 'CheckboxGroup'
| 'DatePicker' | 'DatePicker'
| 'Divider' | 'Divider'
| 'IconPicker'
| 'Input' | 'Input'
| 'InputNumber' | 'InputNumber'
| 'RadioGroup' | 'RadioGroup'
...@@ -73,6 +74,7 @@ async function initComponentAdapter() { ...@@ -73,6 +74,7 @@ async function initComponentAdapter() {
return h(ElButton, { ...props, attrs, type: 'primary' }, slots); return h(ElButton, { ...props, attrs, type: 'primary' }, slots);
}, },
Divider: ElDivider, Divider: ElDivider,
IconPicker,
Input: withDefaultPlaceholder(ElInput, 'input'), Input: withDefaultPlaceholder(ElInput, 'input'),
InputNumber: withDefaultPlaceholder(ElInputNumber, 'input'), InputNumber: withDefaultPlaceholder(ElInputNumber, 'input'),
RadioGroup: ElRadioGroup, RadioGroup: ElRadioGroup,
......
...@@ -8,7 +8,7 @@ import type { BaseFormComponentType } from '@vben/common-ui'; ...@@ -8,7 +8,7 @@ import type { BaseFormComponentType } from '@vben/common-ui';
import type { Component, SetupContext } from 'vue'; import type { Component, SetupContext } from 'vue';
import { h } from 'vue'; import { h } from 'vue';
import { globalShareState } from '@vben/common-ui'; import { globalShareState, IconPicker } from '@vben/common-ui';
import { $t } from '@vben/locales'; import { $t } from '@vben/locales';
import { import {
...@@ -46,6 +46,7 @@ export type ComponentType = ...@@ -46,6 +46,7 @@ export type ComponentType =
| 'CheckboxGroup' | 'CheckboxGroup'
| 'DatePicker' | 'DatePicker'
| 'Divider' | 'Divider'
| 'IconPicker'
| 'Input' | 'Input'
| 'InputNumber' | 'InputNumber'
| 'RadioGroup' | 'RadioGroup'
...@@ -75,6 +76,7 @@ async function initComponentAdapter() { ...@@ -75,6 +76,7 @@ async function initComponentAdapter() {
return h(NButton, { ...props, attrs, type: 'primary' }, slots); return h(NButton, { ...props, attrs, type: 'primary' }, slots);
}, },
Divider: NDivider, Divider: NDivider,
IconPicker,
Input: withDefaultPlaceholder(NInput, 'input'), Input: withDefaultPlaceholder(NInput, 'input'),
InputNumber: withDefaultPlaceholder(NInputNumber, 'input'), InputNumber: withDefaultPlaceholder(NInputNumber, 'input'),
RadioGroup: NRadioGroup, RadioGroup: NRadioGroup,
......
...@@ -87,7 +87,7 @@ import type { BaseFormComponentType } from '@vben/common-ui'; ...@@ -87,7 +87,7 @@ import type { BaseFormComponentType } from '@vben/common-ui';
import type { Component, SetupContext } from 'vue'; import type { Component, SetupContext } from 'vue';
import { h } from 'vue'; import { h } from 'vue';
import { globalShareState } from '@vben/common-ui'; import { globalShareState, IconPicker } from '@vben/common-ui';
import { $t } from '@vben/locales'; import { $t } from '@vben/locales';
import { import {
...@@ -149,6 +149,7 @@ export type ComponentType = ...@@ -149,6 +149,7 @@ export type ComponentType =
| 'TimePicker' | 'TimePicker'
| 'TreeSelect' | 'TreeSelect'
| 'Upload' | 'Upload'
| 'IconPicker';
| BaseFormComponentType; | BaseFormComponentType;
async function initComponentAdapter() { async function initComponentAdapter() {
...@@ -166,6 +167,7 @@ async function initComponentAdapter() { ...@@ -166,6 +167,7 @@ async function initComponentAdapter() {
return h(Button, { ...props, attrs, type: 'default' }, slots); return h(Button, { ...props, attrs, type: 'default' }, slots);
}, },
Divider, Divider,
IconPicker,
Input: withDefaultPlaceholder(Input, 'input'), Input: withDefaultPlaceholder(Input, 'input'),
InputNumber: withDefaultPlaceholder(InputNumber, 'input'), InputNumber: withDefaultPlaceholder(InputNumber, 'input'),
InputPassword: withDefaultPlaceholder(InputPassword, 'input'), InputPassword: withDefaultPlaceholder(InputPassword, 'input'),
......
<script setup lang="ts"> <script setup lang="ts">
import { ref, useTemplateRef, watch, watchEffect } from 'vue'; import { computed, ref, watch, watchEffect } from 'vue';
import { usePagination } from '@vben/hooks'; import { usePagination } from '@vben/hooks';
import { EmptyIcon, Grip } from '@vben/icons'; import { EmptyIcon, Grip, listIcons } from '@vben/icons';
import { $t } from '@vben/locales';
import { import {
Button, Button,
Input,
Pagination, Pagination,
PaginationEllipsis, PaginationEllipsis,
PaginationFirst, PaginationFirst,
...@@ -18,9 +20,11 @@ import { ...@@ -18,9 +20,11 @@ import {
VbenPopover, VbenPopover,
} from '@vben-core/shadcn-ui'; } from '@vben-core/shadcn-ui';
import { refDebounced } from '@vueuse/core';
interface Props { interface Props {
value?: string;
pageSize?: number; pageSize?: number;
prefix?: string;
/** /**
* 图标列表 * 图标列表
*/ */
...@@ -28,48 +32,65 @@ interface Props { ...@@ -28,48 +32,65 @@ interface Props {
} }
const props = withDefaults(defineProps<Props>(), { const props = withDefaults(defineProps<Props>(), {
value: '', prefix: 'ant-design',
pageSize: 36, pageSize: 36,
icons: () => [], icons: () => [],
}); });
const emit = defineEmits<{ const emit = defineEmits<{
change: [string]; change: [string];
'update:value': [string];
}>(); }>();
const refTrigger = useTemplateRef<HTMLElement>('refTrigger'); const modelValue = defineModel({ default: '', type: String });
const visible = ref(false);
const currentSelect = ref(''); const currentSelect = ref('');
const currentList = ref(props.icons);
const currentPage = ref(1); 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 [];
}
});
watch( const showList = computed(() => {
() => props.icons, return currentList.value.filter((item) =>
(newIcons) => { item.includes(keywordDebounce.value),
currentList.value = newIcons; );
}, });
{ immediate: true },
);
const { paginationList, total, setCurrentPage } = usePagination( const { paginationList, total, setCurrentPage } = usePagination(
currentList, showList,
props.pageSize, props.pageSize,
); );
watchEffect(() => { watchEffect(() => {
currentSelect.value = props.value; currentSelect.value = modelValue.value;
}); });
watch( watch(
() => currentSelect.value, () => currentSelect.value,
(v) => { (v) => {
emit('update:value', v);
emit('change', v); emit('change', v);
}, },
); );
const handleClick = (icon: string) => { const handleClick = (icon: string) => {
currentSelect.value = icon; currentSelect.value = icon;
modelValue.value = icon;
close();
}; };
const handlePageChange = (page: number) => { const handlePageChange = (page: number) => {
...@@ -77,22 +98,46 @@ const handlePageChange = (page: number) => { ...@@ -77,22 +98,46 @@ const handlePageChange = (page: number) => {
setCurrentPage(page); setCurrentPage(page);
}; };
function changeOpenState() { function toggleOpenState() {
refTrigger.value?.click?.(); visible.value = !visible.value;
} }
defineExpose({ changeOpenState }); function open() {
visible.value = true;
}
function close() {
visible.value = false;
}
defineExpose({ toggleOpenState, open, close });
</script> </script>
<template> <template>
<VbenPopover <VbenPopover
v-model:open="visible"
:content-props="{ align: 'end', alignOffset: -11, sideOffset: 8 }" :content-props="{ align: 'end', alignOffset: -11, sideOffset: 8 }"
content-class="p-0 pt-3" content-class="p-0 pt-3"
> >
<template #trigger> <template #trigger>
<div ref="refTrigger"> <slot :close="close" :icon="currentSelect" :open="open" name="trigger">
<VbenIcon :icon="currentSelect || Grip" class="size-5" /> <div class="flex items-center gap-2">
</div> <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> </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"> <template v-if="paginationList.length > 0">
<div class="grid max-h-[360px] w-full grid-cols-6 justify-items-center"> <div class="grid max-h-[360px] w-full grid-cols-6 justify-items-center">
......
...@@ -21,6 +21,10 @@ ...@@ -21,6 +21,10 @@
"pointAriaLabel": "Click point", "pointAriaLabel": "Click point",
"clickInOrder": "Please click in order" "clickInOrder": "Please click in order"
}, },
"iconPicker": {
"placeholder": "Select an icon",
"search": "Search icon..."
},
"fallback": { "fallback": {
"pageNotFound": "Oops! Page Not Found", "pageNotFound": "Oops! Page Not Found",
"pageNotFoundDesc": "Sorry, we couldn't find the page you were looking for.", "pageNotFoundDesc": "Sorry, we couldn't find the page you were looking for.",
......
...@@ -21,6 +21,10 @@ ...@@ -21,6 +21,10 @@
"pointAriaLabel": "点击点", "pointAriaLabel": "点击点",
"clickInOrder": "请依次点击" "clickInOrder": "请依次点击"
}, },
"iconPicker": {
"placeholder": "选择一个图标",
"search": "搜索图标..."
},
"fallback": { "fallback": {
"pageNotFound": "哎呀!未找到页面", "pageNotFound": "哎呀!未找到页面",
"pageNotFoundDesc": "抱歉,我们无法找到您要找的页面。", "pageNotFoundDesc": "抱歉,我们无法找到您要找的页面。",
......
...@@ -8,7 +8,7 @@ import type { BaseFormComponentType } from '@vben/common-ui'; ...@@ -8,7 +8,7 @@ import type { BaseFormComponentType } from '@vben/common-ui';
import type { Component, SetupContext } from 'vue'; import type { Component, SetupContext } from 'vue';
import { h } from 'vue'; import { h } from 'vue';
import { globalShareState } from '@vben/common-ui'; import { globalShareState, IconPicker } from '@vben/common-ui';
import { $t } from '@vben/locales'; import { $t } from '@vben/locales';
import { import {
...@@ -54,6 +54,7 @@ export type ComponentType = ...@@ -54,6 +54,7 @@ export type ComponentType =
| 'DatePicker' | 'DatePicker'
| 'DefaultButton' | 'DefaultButton'
| 'Divider' | 'Divider'
| 'IconPicker'
| 'Input' | 'Input'
| 'InputNumber' | 'InputNumber'
| 'InputPassword' | 'InputPassword'
...@@ -87,6 +88,7 @@ async function initComponentAdapter() { ...@@ -87,6 +88,7 @@ async function initComponentAdapter() {
return h(Button, { ...props, attrs, type: 'default' }, slots); return h(Button, { ...props, attrs, type: 'default' }, slots);
}, },
Divider, Divider,
IconPicker,
Input: withDefaultPlaceholder(Input, 'input'), Input: withDefaultPlaceholder(Input, 'input'),
InputNumber: withDefaultPlaceholder(InputNumber, 'input'), InputNumber: withDefaultPlaceholder(InputNumber, 'input'),
InputPassword: withDefaultPlaceholder(InputPassword, 'input'), InputPassword: withDefaultPlaceholder(InputPassword, 'input'),
......
<script lang="ts" setup>
import { computed, ref } from 'vue';
import { IconPicker } from '@vben/common-ui';
import { listIcons } from '@vben/icons';
import { Input } from 'ant-design-vue';
import iconsData from './icons.data';
export interface Props {
allowClear?: boolean;
pageSize?: number;
/**
* 可以通过prefix获取系统中使用的图标集
*/
prefix?: string;
readonly?: boolean;
value?: string;
width?: string;
}
// Don't inherit FormItem disabled、placeholder...
defineOptions({
inheritAttrs: false,
});
const props = withDefaults(defineProps<Props>(), {
allowClear: true,
pageSize: 36,
prefix: '',
readonly: false,
value: '',
width: '100%',
});
const refIconPicker = ref();
const currentSelect = ref('');
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 {
const prefix = iconsData.prefix;
return iconsData.icons.map((icon) => `${prefix}:${icon}`);
}
} catch (error) {
console.error('Failed to load icons:', error);
return [];
}
});
const triggerPopover = () => {
refIconPicker.value?.changeOpenState?.();
};
const handleChange = (icon: string) => {
currentSelect.value = icon;
};
</script>
<template>
<Input
v-model:value="currentSelect"
:allow-clear="props.allowClear"
:readonly="props.readonly"
:style="{ width }"
class="cursor-pointer"
placeholder="点击选中图标"
@click="triggerPopover"
>
<template #addonAfter>
<IconPicker
ref="refIconPicker"
:icons="currentList"
:page-size="pageSize"
:value="currentSelect"
@change="handleChange"
/>
</template>
</Input>
</template>
<script lang="ts" setup> <script lang="ts" setup>
import { Page } from '@vben/common-ui'; import { ref } from 'vue';
import { IconPicker, Page } from '@vben/common-ui';
import { import {
IconifyIcon,
MdiGithub, MdiGithub,
MdiGoogle, MdiGoogle,
MdiKeyboardEsc, MdiKeyboardEsc,
...@@ -16,9 +19,9 @@ import { ...@@ -16,9 +19,9 @@ import {
SvgDownloadIcon, SvgDownloadIcon,
} from '@vben/icons'; } from '@vben/icons';
import { Card } from 'ant-design-vue'; import { Card, Input } from 'ant-design-vue';
import IconPicker from './icon-picker.vue'; const iconValue = ref('ant-design:trademark-outlined');
</script> </script>
<template> <template>
...@@ -71,15 +74,53 @@ import IconPicker from './icon-picker.vue'; ...@@ -71,15 +74,53 @@ import IconPicker from './icon-picker.vue';
</div> </div>
</Card> </Card>
<Card class="mb-5" title="图标选择器(Iconify)"> <Card class="mb-5" title="图标选择器">
<div class="flex items-center gap-5"> <div class="mb-5 flex items-center gap-5">
<IconPicker width="300px" /> <span>原始样式(Iconify):</span>
<IconPicker class="w-[200px]" />
</div>
<div class="mb-5 flex items-center gap-5">
<span>原始样式(svg):</span>
<IconPicker class="w-[200px]" prefix="svg" />
</div>
<div class="mb-5 flex items-center gap-5">
<span>完整替换触发组件:</span>
<IconPicker class="w-[200px]">
<template #trigger="{ icon }">
<Input
:value="icon"
placeholder="点击这里选择图标"
style="width: 300px"
>
<template #addonAfter>
<IconifyIcon
:icon="icon || 'ant-design:appstore-filled'"
class="text-2xl"
/>
</template>
</Input>
</template>
</IconPicker>
</div> </div>
</Card>
<Card title="图标选择器(Svg)">
<div class="flex items-center gap-5"> <div class="flex items-center gap-5">
<IconPicker prefix="svg" width="300px" /> <span>可手动输入,只能点击图标打开弹窗:</span>
<Input
v-model:value="iconValue"
allow-clear
placeholder="点击这里选择图标"
style="width: 300px"
>
<template #addonAfter>
<IconPicker v-model="iconValue" class="w-[200px]">
<template #trigger="{ icon }">
<IconifyIcon
:icon="icon || 'ant-design:appstore-filled'"
class="text-2xl"
/>
</template>
</IconPicker>
</template>
</Input>
</div> </div>
</Card> </Card>
</Page> </Page>
......
...@@ -58,6 +58,11 @@ const [BaseForm, baseFormApi] = useVbenForm({ ...@@ -58,6 +58,11 @@ const [BaseForm, baseFormApi] = useVbenForm({
suffix: () => '¥', suffix: () => '¥',
}, },
{ {
component: 'IconPicker',
fieldName: 'icon',
label: '图标',
},
{
component: 'Select', component: 'Select',
componentProps: { componentProps: {
allowClear: true, allowClear: true,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!