From 78844efaf5c2acfaca899d0b0108d2895fcb9734 Mon Sep 17 00:00:00 2001 From: haoxr <1490493387@qq.com> Date: Sun, 26 Mar 2023 18:31:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=9B=BE=E6=A0=87=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E5=99=A8=E7=BB=84=E4=BB=B6=E5=B0=81=E8=A3=85=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: ce238a5690133a6590bdb0de44f4b590af38940a --- src/components/IconSelect/index.vue | 51 ++++++++++++++++------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/components/IconSelect/index.vue b/src/components/IconSelect/index.vue index 6cd8efa0..88c834eb 100644 --- a/src/components/IconSelect/index.vue +++ b/src/components/IconSelect/index.vue @@ -3,26 +3,23 @@ const props = defineProps({ modelValue: { type: String, require: false - }, - /** - * 图标选择器宽度 - */ - width: { - type: String, - require: false, - default: '400px' } }); const emit = defineEmits(['update:modelValue']); -const visible = ref(false); const inputValue = toRef(props, 'modelValue'); -const width = toRef(props, 'width'); -const iconNames: string[] = []; -const filterIconNames = ref([]); -const filterValue = ref(''); +const visible = ref(false); // 弹窗显示状态 +const iconNames: string[] = []; // 所有的图标名称集合 + +const filterValue = ref(''); // 筛选的值 +const filterIconNames = ref([]); // 过滤后的图标名称集合 + +const iconSelectorRef = ref(null); +/** + * icon 加载 + */ function loadIcons() { const icons = import.meta.glob('../../assets/icons/*.svg'); for (const icon in icons) { @@ -33,9 +30,9 @@ function loadIcons() { } /** - * 筛选图标 + * icon 筛选 */ -function handleIconFilter() { +function handleFilter() { if (filterValue.value) { filterIconNames.value = iconNames.filter(iconName => iconName.includes(filterValue.value) @@ -46,22 +43,25 @@ function handleIconFilter() { } /** - * 选择图标 - * - * @param iconName 选择的图标名称 + * icon 选择 */ -function onIconSelect(iconName: string) { +function handleSelect(iconName: string) { emit('update:modelValue', iconName); visible.value = false; } +/** + * 点击容器外的区域关闭弹窗 VueUse onClickOutside + */ +onClickOutside(iconSelectorRef, () => (visible.value = false)); + onMounted(() => { loadIcons(); });