Skip to content

Commit 9472b51

Browse files
committed
feat(projects): 增加Icon选择器组件
1 parent f3c86ef commit 9472b51

File tree

4 files changed

+121
-15
lines changed

4 files changed

+121
-15
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<template>
2+
<n-popover placement="bottom-end" trigger="click">
3+
<template #trigger>
4+
<n-input v-model:value="modelValue" readonly placeholder="点击选择图标">
5+
<template #suffix>
6+
<Icon :icon="modelValue ? modelValue : 'mdi:apps'" class="text-30px p-5px" />
7+
</template>
8+
</n-input>
9+
</template>
10+
<template #header>
11+
<n-input v-model:value="searchValue" placeholder="搜索图标"></n-input>
12+
</template>
13+
<div v-if="iconsList.length > 0" class="grid grid-cols-9 h-auto overflow-auto">
14+
<template v-for="iconItem in iconsList" :key="iconItem">
15+
<Icon
16+
:icon="iconItem"
17+
class="border-1px border-[#d9d9d9] text-30px m-2px p-5px"
18+
:style="{ 'border-color': modelValue === iconItem ? theme.themeColor : '' }"
19+
@click="handleChange(iconItem)"
20+
/>
21+
</template>
22+
</div>
23+
<n-empty v-else class="w-306px" description="你什么也找不到" />
24+
</n-popover>
25+
</template>
26+
27+
<script lang="ts" setup>
28+
import { ref, computed } from 'vue';
29+
import { NPopover, NInput, NEmpty } from 'naive-ui';
30+
import { Icon } from '@iconify/vue';
31+
import { useThemeStore } from '@/store';
32+
33+
interface Props {
34+
/** 绑定的图标 */
35+
value: string;
36+
/** 图标列表 */
37+
icons?: string[];
38+
}
39+
40+
interface Emits {
41+
(e: 'update:value', val: string): void;
42+
}
43+
44+
const props = withDefaults(defineProps<Props>(), {
45+
icons: () => []
46+
});
47+
48+
const emit = defineEmits<Emits>();
49+
50+
const theme = useThemeStore();
51+
const searchValue = ref('');
52+
53+
const modelValue = computed({
54+
get() {
55+
return props.value;
56+
},
57+
set(val: string) {
58+
emit('update:value', val);
59+
}
60+
});
61+
62+
const iconsList = computed(() => props.icons.filter(v => v.includes(searchValue.value)));
63+
64+
function handleChange(iconItem: string) {
65+
modelValue.value = iconItem;
66+
}
67+
</script>
68+
<style lang="scss" scoped>
69+
:deep(.n-input-wrapper) {
70+
padding-right: 0;
71+
}
72+
:deep(.n-input__suffix) {
73+
border: 1px solid #d9d9d9;
74+
}
75+
</style>

src/components/common/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import LoginBg from './LoginBg/index.vue';
44
import BannerSvg from './BannerSvg/index.vue';
55
import HoverContainer from './HoverContainer/index.vue';
66
import LoadingEmptyWrapper from './LoadingEmptyWrapper/index.vue';
7+
import IconSelect from './IconSelect/index.vue';
78

8-
export { NaiveProvider, SystemLogo, LoginBg, BannerSvg, HoverContainer, LoadingEmptyWrapper };
9+
export { NaiveProvider, SystemLogo, LoginBg, BannerSvg, HoverContainer, LoadingEmptyWrapper, IconSelect };

src/views/plugin/icon/icons.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export const icons = [
2+
'mdi:emoticon',
3+
'mdi:ab-testing',
4+
'ph:alarm',
5+
'ph:android-logo',
6+
'ph:align-bottom',
7+
'ph:archive-box-light',
8+
'uil:basketball',
9+
'uil:brightness-plus',
10+
'uil:capture',
11+
'mdi:apps-box',
12+
'mdi:alert',
13+
'mdi:airballoon',
14+
'mdi:airplane-edit',
15+
'mdi:alpha-f-box-outline',
16+
'mdi:arm-flex-outline',
17+
'ic:baseline-10mp',
18+
'ic:baseline-access-time',
19+
'ic:baseline-brightness-4',
20+
'ic:baseline-brightness-5',
21+
'ic:baseline-credit-card',
22+
'ic:baseline-filter-1',
23+
'ic:baseline-filter-2',
24+
'ic:baseline-filter-3',
25+
'ic:baseline-filter-4',
26+
'ic:baseline-filter-5',
27+
'ic:baseline-filter-6',
28+
'ic:baseline-filter-7',
29+
'ic:baseline-filter-8',
30+
'ic:baseline-filter-9',
31+
'ic:baseline-filter-9-plus'
32+
];

src/views/plugin/icon/index.vue

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
<template>
22
<div>
33
<n-card title="Icon组件示例" class="shadow-sm rounded-16px">
4-
<div class="flex justify-around">
4+
<div class="grid grid-cols-10">
55
<template v-for="item in icons" :key="item">
6-
<Icon :icon="item" class="text-30px" />
6+
<div class="mt-5px flex-x-center">
7+
<Icon :icon="item" class="text-30px" />
8+
</div>
79
</template>
810
</div>
11+
<div class="mt-50px">
12+
<h1 class="mb-20px text-18px font-500">Icon图标选择器</h1>
13+
<icon-select v-model:value="selectVal" :icons="icons" />
14+
</div>
915
<template #footer>
1016
<web-site-link label="iconify地址:" link="https://icones.js.org/" class="mt-10px" />
1117
</template>
@@ -14,20 +20,12 @@
1420
</template>
1521

1622
<script lang="ts" setup>
23+
import { ref } from 'vue';
1724
import { NCard } from 'naive-ui';
1825
import { Icon } from '@iconify/vue';
19-
import { WebSiteLink } from '@/components';
26+
import { IconSelect, WebSiteLink } from '@/components';
27+
import { icons } from './icons';
2028
21-
const icons = [
22-
'mdi:emoticon',
23-
'mdi:ab-testing',
24-
'ph:alarm',
25-
'ph:android-logo',
26-
'ph:align-bottom',
27-
'ph:archive-box-light',
28-
'uil:basketball',
29-
'uil:brightness-plus',
30-
'uil:capture'
31-
];
29+
const selectVal = ref('');
3230
</script>
3331
<style scoped></style>

0 commit comments

Comments
 (0)