ant-design/components/breadcrumb/useItems.ts
叶枫 502dac12aa
docs: format code (#48309)
* docs: fix code

* feat: lint

* feat: prettier

* feat: test

* feat: review

* feat: format html

* feat: format html
2024-04-08 14:04:08 +08:00

45 lines
943 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]);
}