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(); });