ant-design/components/breadcrumb/Breadcrumb.tsx
dependabot[bot] 20d5502193
chore(deps-dev): bump eslint-config-airbnb from 18.2.1 to 19.0.0 (#32824)
* chore(deps-dev): bump eslint-config-airbnb from 18.2.1 to 19.0.0

Bumps [eslint-config-airbnb](https://github.com/airbnb/javascript) from 18.2.1 to 19.0.0.
- [Release notes](https://github.com/airbnb/javascript/releases)
- [Commits](https://github.com/airbnb/javascript/compare/eslint-config-airbnb-v18.2.1...eslint-config-airbnb-v19.0.0)

---
updated-dependencies:
- dependency-name: eslint-config-airbnb
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* chore: code style

* memoize-one

* add comment

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* improve useMemo deps

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: afc163 <afc163@gmail.com>
2021-11-26 12:18:21 +08:00

157 lines
4.1 KiB
TypeScript
Executable File

import * as React from 'react';
import classNames from 'classnames';
import toArray from 'rc-util/lib/Children/toArray';
import BreadcrumbItem from './BreadcrumbItem';
import BreadcrumbSeparator from './BreadcrumbSeparator';
import Menu from '../menu';
import { ConfigContext } from '../config-provider';
import devWarning from '../_util/devWarning';
import { cloneElement } from '../_util/reactNode';
export interface Route {
path: string;
breadcrumbName: string;
children?: Omit<Route, 'children'>[];
}
export interface BreadcrumbProps {
prefixCls?: string;
routes?: Route[];
params?: any;
separator?: React.ReactNode;
itemRender?: (
route: Route,
params: any,
routes: Array<Route>,
paths: Array<string>,
) => React.ReactNode;
style?: React.CSSProperties;
className?: string;
}
function getBreadcrumbName(route: Route, params: any) {
if (!route.breadcrumbName) {
return null;
}
const paramsKeys = Object.keys(params).join('|');
const name = route.breadcrumbName.replace(
new RegExp(`:(${paramsKeys})`, 'g'),
(replacement, key) => params[key] || replacement,
);
return name;
}
function defaultItemRender(route: Route, params: any, routes: Route[], paths: string[]) {
const isLastItem = routes.indexOf(route) === routes.length - 1;
const name = getBreadcrumbName(route, params);
return isLastItem ? <span>{name}</span> : <a href={`#/${paths.join('/')}`}>{name}</a>;
}
const getPath = (path: string, params: any) => {
path = (path || '').replace(/^\//, '');
Object.keys(params).forEach(key => {
path = path.replace(`:${key}`, params[key]);
});
return path;
};
const addChildPath = (paths: string[], childPath: string, params: any) => {
const originalPaths = [...paths];
const path = getPath(childPath || '', params);
if (path) {
originalPaths.push(path);
}
return originalPaths;
};
interface BreadcrumbInterface extends React.FC<BreadcrumbProps> {
Item: typeof BreadcrumbItem;
Separator: typeof BreadcrumbSeparator;
}
const Breadcrumb: BreadcrumbInterface = ({
prefixCls: customizePrefixCls,
separator = '/',
style,
className,
routes,
children,
itemRender = defaultItemRender,
params = {},
...restProps
}) => {
const { getPrefixCls, direction } = React.useContext(ConfigContext);
let crumbs;
const prefixCls = getPrefixCls('breadcrumb', customizePrefixCls);
if (routes && routes.length > 0) {
// generated by route
const paths: string[] = [];
crumbs = routes.map(route => {
const path = getPath(route.path, params);
if (path) {
paths.push(path);
}
// generated overlay by route.children
let overlay;
if (route.children && route.children.length) {
overlay = (
<Menu>
{route.children.map(child => (
<Menu.Item key={child.path || child.breadcrumbName}>
{itemRender(child, params, routes, addChildPath(paths, child.path, params))}
</Menu.Item>
))}
</Menu>
);
}
return (
<BreadcrumbItem overlay={overlay} separator={separator} key={path || route.breadcrumbName}>
{itemRender(route, params, routes, paths)}
</BreadcrumbItem>
);
});
} else if (children) {
crumbs = toArray(children).map((element: any, index) => {
if (!element) {
return element;
}
devWarning(
element.type &&
(element.type.__ANT_BREADCRUMB_ITEM === true ||
element.type.__ANT_BREADCRUMB_SEPARATOR === true),
'Breadcrumb',
"Only accepts Breadcrumb.Item and Breadcrumb.Separator as it's children",
);
return cloneElement(element, {
separator,
key: index,
});
});
}
const breadcrumbClassName = classNames(
prefixCls,
{
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
);
return (
<div className={breadcrumbClassName} style={style} {...restProps}>
{crumbs}
</div>
);
};
Breadcrumb.Item = BreadcrumbItem;
Breadcrumb.Separator = BreadcrumbSeparator;
export default Breadcrumb;