新增提示Toast组件
This commit is contained in:
@@ -10,26 +10,59 @@ interface BreadcrumbProps {
|
||||
className?: string;
|
||||
}
|
||||
|
||||
interface PreviousRouteData {
|
||||
title: string;
|
||||
to: string;
|
||||
}
|
||||
|
||||
interface Handle {
|
||||
breadcrumb: string | ((data: any) => string);
|
||||
breadcrumb: string | ((data: unknown) => string);
|
||||
previousRoute?: PreviousRouteData | ((data: unknown) => PreviousRouteData | undefined);
|
||||
}
|
||||
|
||||
interface Match {
|
||||
handle?: Handle;
|
||||
pathname: string;
|
||||
data: any;
|
||||
data: unknown;
|
||||
}
|
||||
|
||||
export function Breadcrumb({ items = [], className = '' }: BreadcrumbProps) {
|
||||
const matches = useMatches() as Match[];
|
||||
|
||||
// 构建面包屑数据
|
||||
const breadcrumbs = items.length > 0 ? items : matches
|
||||
.filter(match => match.handle?.breadcrumb)
|
||||
.map(match => ({
|
||||
title: typeof match.handle?.breadcrumb === 'function'
|
||||
? match.handle.breadcrumb(match.data)
|
||||
: match.handle?.breadcrumb,
|
||||
to: match.pathname
|
||||
}));
|
||||
.map((match, index, array) => {
|
||||
// 当前路由的面包屑
|
||||
const current = {
|
||||
title: typeof match.handle?.breadcrumb === 'function'
|
||||
? match.handle.breadcrumb(match.data)
|
||||
: match.handle?.breadcrumb as string,
|
||||
to: match.pathname
|
||||
};
|
||||
|
||||
// 如果当前路由有previousRoute属性且该路由是数组中的最后一个
|
||||
if (match.handle?.previousRoute && index === array.length - 1) {
|
||||
// 获取previousRoute数据,支持函数形式
|
||||
const prevRouteData = typeof match.handle.previousRoute === 'function'
|
||||
? match.handle.previousRoute(match.data)
|
||||
: match.handle.previousRoute;
|
||||
|
||||
// 如果previousRoute存在,添加到面包屑中
|
||||
if (prevRouteData) {
|
||||
return [
|
||||
{
|
||||
title: prevRouteData.title,
|
||||
to: prevRouteData.to
|
||||
},
|
||||
current
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return [current];
|
||||
})
|
||||
.flat(); // 扁平化数组
|
||||
|
||||
if (breadcrumbs.length === 0) {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user