2023-03-05 20:57:49 +08:00
|
|
|
import { useMemo } from 'react';
|
2023-04-27 22:43:42 +08:00
|
|
|
import type { BreadcrumbItemType, BreadcrumbSeparatorType, ItemType } from './Breadcrumb';
|
2023-03-05 20:57:49 +08:00
|
|
|
|
|
|
|
type MergedType = BreadcrumbItemType & {
|
2023-04-27 22:43:42 +08:00
|
|
|
children?: ItemType['children'];
|
2023-03-05 20:57:49 +08:00
|
|
|
};
|
|
|
|
|
2023-04-27 22:43:42 +08:00
|
|
|
function route2item(route: ItemType): MergedType {
|
2023-03-05 20:57:49 +08:00
|
|
|
const { breadcrumbName, children, ...rest } = route;
|
|
|
|
|
|
|
|
const clone: MergedType = {
|
|
|
|
title: breadcrumbName,
|
|
|
|
...rest,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (children) {
|
|
|
|
clone.menu = {
|
|
|
|
items: children.map(({ breadcrumbName: itemBreadcrumbName, ...itemProps }) => ({
|
|
|
|
...itemProps,
|
|
|
|
title: itemBreadcrumbName,
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function useItems(
|
|
|
|
items?: ItemType[],
|
2023-04-27 22:43:42 +08:00
|
|
|
routes?: ItemType[],
|
2023-03-05 20:57:49 +08:00
|
|
|
): Partial<MergedType & BreadcrumbSeparatorType>[] | null {
|
|
|
|
return useMemo<ItemType[] | null>(() => {
|
|
|
|
if (items) {
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (routes) {
|
|
|
|
return routes.map(route2item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}, [items, routes]);
|
|
|
|
}
|