ant-design/components/breadcrumb/useItems.ts
二货爱吃白萝卜 78e9d58ef1
fix: Breadcrumb itemRender not remove link even path is provided (#42049)
* refactor: out of bread item render

* refactor: fix too filled

* fix: render logic

* test: add test case

* fix: ts

* fix: test
2023-04-27 22:43:42 +08:00

44 lines
942 B
TypeScript

import { useMemo } from 'react';
import type { BreadcrumbItemType, BreadcrumbSeparatorType, ItemType } from './Breadcrumb';
type MergedType = BreadcrumbItemType & {
children?: ItemType['children'];
};
function route2item(route: ItemType): MergedType {
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[],
routes?: ItemType[],
): Partial<MergedType & BreadcrumbSeparatorType>[] | null {
return useMemo<ItemType[] | null>(() => {
if (items) {
return items;
}
if (routes) {
return routes.map(route2item);
}
return null;
}, [items, routes]);
}