优化IconPicker组件

This commit is contained in:
jingrow 2025-11-02 13:52:09 +08:00
parent 463088f77c
commit d84f56d61e

View File

@ -22,7 +22,7 @@
v-model:show="showPicker"
:width="900"
:placement="'right'"
:trap-focus="false"
:trap-focus="true"
:close-on-esc="true"
>
<n-drawer-content :title="t('Select Icon')" :closable="true">
@ -32,11 +32,13 @@
<div class="search-section">
<div class="search-controls">
<n-input
ref="searchInputRef"
v-model:value="searchQuery"
:placeholder="t('Search icon name...')"
clearable
size="large"
@update:value="handleSearch"
@keydown="handleInputKeydown"
style="flex: 1;"
>
<template #prefix>
@ -118,8 +120,9 @@
</template>
<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { ref, computed, onMounted, watch, nextTick } from 'vue'
import { NButton, NDrawer, NDrawerContent, NInput, NSelect, useMessage } from 'naive-ui'
import type { InputInst } from 'naive-ui'
import { Icon } from '@iconify/vue'
import { getIconLibraryConfig, getAvailableIconLibraries, DEFAULT_ICON_LIBRARY } from '@/shared/utils/icon-libraries'
import { t } from '@/shared/i18n'
@ -153,6 +156,7 @@ const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(200) // 200
const hasMore = ref(true)
const searchInputRef = ref<InputInst | null>(null)
//
const currentLibraryConfig = computed(() => {
@ -351,6 +355,12 @@ async function openPicker() {
searchQuery.value = ''
}
showPicker.value = true
// drawer
await nextTick()
setTimeout(() => {
searchInputRef.value?.focus()
}, 100)
}
//
@ -391,6 +401,12 @@ function handleSearch() {
currentPage.value = 1
}
//
function handleInputKeydown(event: KeyboardEvent) {
//
event.stopPropagation()
}
//
async function handleLibraryChange(newLibrary: string) {
currentLibrary.value = newLibrary
@ -430,6 +446,17 @@ watch(() => props.modelValue, (newValue) => {
}
}, { immediate: true })
// drawer
watch(showPicker, async (isOpen) => {
if (isOpen) {
// drawer DOM
await nextTick()
setTimeout(() => {
searchInputRef.value?.focus()
}, 150)
}
})
//
onMounted(() => {
loadAllIcons()