ant-design/components/breadcrumb/useItems.ts
黑雨 413e44a170
feat: breadcrumb support items (#40543)
* feat: breadCrumb support  items

* feat: update snap

* feat: 删除部分不支持的test case

* feat: update snap

* feat: update snap

* feat: update snap

* feat: update ts

* feat: update ts

* feat: update ts

* feat: update for reviewer

* doc: update for doc

* doc: update for doc

* doc: replace breadcrumbName to title

* doc: replace breadcrumbName to title

* doc: replace breadcrumbName to title

* chore: adjust separator logic

* refactor: use items

* chore: update logic

* chore: fix routes logic

* chore: fix logic

* chore: clean up

* chore: adjust warning info

* doc: edit test case

* feat: update snap

---------

Co-authored-by: 二货机器人 <smith3816@gmail.com>
2023-03-05 20:57:49 +08:00

44 lines
940 B
TypeScript

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