2025-11-18 22:28:13 +08:00
|
|
|
|
import { useMemo } from 'react';
|
|
|
|
|
|
import * as LucideIcons from 'lucide-react';
|
|
|
|
|
|
|
2025-12-09 11:07:44 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 动态获取 Lucide 图标组件
|
|
|
|
|
|
* Dynamically get Lucide icon component by name
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param iconName - 图标名称(如 'Package', 'Settings')
|
|
|
|
|
|
* @param fallback - 找不到时的回退组件
|
|
|
|
|
|
* @returns Lucide 图标组件
|
|
|
|
|
|
*/
|
2025-11-18 22:28:13 +08:00
|
|
|
|
export function useDynamicIcon(iconName?: string, fallback?: React.ComponentType) {
|
|
|
|
|
|
return useMemo(() => {
|
|
|
|
|
|
if (!iconName) {
|
|
|
|
|
|
return fallback || LucideIcons.Package;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-09 11:07:44 +08:00
|
|
|
|
// 动态图标查找需要使用 any,因为 lucide-react 的类型定义不支持动态索引
|
|
|
|
|
|
// Dynamic icon lookup requires any, as lucide-react types don't support dynamic indexing
|
2025-11-18 22:28:13 +08:00
|
|
|
|
const IconComponent = (LucideIcons as any)[iconName];
|
|
|
|
|
|
return IconComponent || fallback || LucideIcons.Package;
|
|
|
|
|
|
}, [iconName, fallback]);
|
|
|
|
|
|
}
|