Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(components,website): 重构Breadcrumb #132

Merged
merged 1 commit into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions packages/components/src/components/breadcrumb/Breadcrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ import React from 'react';
import classNames from 'classnames';
import BreadcrumbItem from './BreadcrumbItem';
import BreadcrumbSeparator from './BreadcrumbSeparator';
import { ConfigContext } from '../config-provider';

export interface Route {
path: string;
breadcrumbName: string;
children?: Omit<Route, 'children'>[];
}

export interface BreadcrumbProps {
prefixCls?: string;
routes?: Route[];
params?: any;
separator?: string;
style?: React.CSSProperties;
separator?: React.ReactNode;
className?: string;
itemRender?: (route: Route, params: any, routes: Array<Route>, paths: Array<string>) => React.ReactNode;
children?: React.ReactNode;
}

function getBreadcrumbName(route: Route, params: any) {
Expand All @@ -36,7 +39,7 @@ function isLastItem(route: Route, routes: Route[]) {
/* eslint-disable-next-line */
function defaultItemRender(route: Route, params: any, routes: Route[], paths: string[]) {
const name = getBreadcrumbName(route, params);
return isLastItem(route, routes) ? <span>{name}</span> : <a href={`#/${paths.join('/')}`}>{name}</a>;
return isLastItem(route, routes) ? <span>{name}</span> : <a href={`/${paths.join('/')}`}>{name}</a>;
}

const getPath = (path: string, params: any) => {
Expand All @@ -52,9 +55,21 @@ interface BreadcrumbInterface extends React.FC<BreadcrumbProps> {
Separator: typeof BreadcrumbSeparator;
}

const Breadcrumb: BreadcrumbInterface = (props: BreadcrumbProps) => {
const { routes, separator = '/', itemRender = defaultItemRender, params = {}, children } = props;
const Breadcrumb: BreadcrumbInterface = ({
prefixCls: customizePrefixCls,
separator = '/',
style,
className,
routes,
children,
itemRender = defaultItemRender,
params = {},
...restProps
}) => {
const { getPrefixCls } = React.useContext(ConfigContext);

let crumbs;
const prefixCls = getPrefixCls('breadcrumb', customizePrefixCls);
if (routes && routes.length > 0) {
const paths: string[] = [];
crumbs = routes.map((route: Route) => {
Expand All @@ -63,22 +78,20 @@ const Breadcrumb: BreadcrumbInterface = (props: BreadcrumbProps) => {
if (path) {
paths.push(path);
}

return (
<BreadcrumbItem
separator={separator}
key={path || route.breadcrumbName}
{...route}
isLastItem={isLastItem(route, routes)}
>
<BreadcrumbItem separator={separator} key={path || route.breadcrumbName}>
{itemRender(route, params, routes, paths)}
</BreadcrumbItem>
);
});
} else if (children) {
crumbs = children;
}
return <div className={classNames(props.className, 'gio-breadcrumb')}>{crumbs}</div>;
return (
<div className={classNames(className, prefixCls)} style={style} {...restProps}>
{crumbs}
</div>
);
};

Breadcrumb.Item = BreadcrumbItem;
Expand Down
11 changes: 6 additions & 5 deletions packages/components/src/components/breadcrumb/BreadcrumbItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import BreadcrumbSeparator from './BreadcrumbSeparator';
import classnames from 'classnames';

export interface BreadcrumbItemProps {
separator?: string;
separator?: React.ReactNode;
href?: string;
onClick?: React.MouseEventHandler<HTMLAnchorElement | HTMLSpanElement>;
children?: React.ReactNode;
Expand All @@ -12,15 +12,16 @@ export interface BreadcrumbItemProps {

const BreadcrumbItem: React.FC<BreadcrumbItemProps> = (props: BreadcrumbItemProps) => {
const { href, children, separator, isLastItem } = props;
/* eslint-disable prettier/prettier */
const link = href ? (
<a className="gio-breadcrumb-item-link-target" {...props}>
{children}
</a>
) : (
<span className="gio-breadcrumb-item-link-target" {...props}>
{children}
</span>
);
<span className="gio-breadcrumb-item-link-target" {...props}>
{children}
</span>
);

if (children) {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

export interface BreadcrumbSeparatorProps {
separator?: string;
separator?: React.ReactNode;
}

const BreadcrumbSeparator: React.FC<BreadcrumbSeparatorProps> = (props: BreadcrumbSeparatorProps) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@
padding: 0 8px;
text-align: center;
}
& > span:last-child &-separator {
display: none;
}
}
28 changes: 19 additions & 9 deletions packages/website/src/components/basic/breadcrumb/demo/basic.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import * as React from 'react';
import { Breadcrumb } from '@gio-design/components';
import Breadcrumb from '@gio-design/components/es/components/breadcrumb';
import '@gio-design/components/es/components/breadcrumb/style/css.js';

export default () => {
const routes = [
{
path: 'index',
path: 'components',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path是否考虑加个#不会跳转到demo别的页面

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以

breadcrumbName: '首页',
onClick: () => {
alert('首页');
},
},
{
path: 'basic',
breadcrumbName: '一级面包屑',
onClick: () => {
alert('一级面包屑');
},
},
{
path: 'breadcrumb',
breadcrumbName: '二级面包屑',
},
];
return <Breadcrumb routes={routes} />;
return (
<div>
<Breadcrumb routes={routes} />
<Breadcrumb>
<Breadcrumb.Item>首页</Breadcrumb.Item>
<Breadcrumb.Item>
<a href="/components/basic/breadcrumb">一级面包屑</a>
</Breadcrumb.Item>
<Breadcrumb.Item>
<a href="breadcrumb">二级面包屑</a>
</Breadcrumb.Item>
<Breadcrumb.Item>三级面包屑</Breadcrumb.Item>
</Breadcrumb>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ group:
| ---------- | ---------------------------------------- | ------------------------------------------- | ------ |
| routes | router 的路由栈信息 | routes[] | - |
| params | 路由的参数 | object | - |
| separator | 分隔符自定义 | string | '/' |
| separator | 分隔符自定义 | string \| ReactNode | / |
| itemRender | 自定义链接函数,和 react-router 配置使用 | (route, params, routes, paths) => ReactNode | - |

### Breadcrumb.Item

| 参数 | 说明 | 类型 | 默认值 |
| ---------- | ------------------- | -------------------- | ------ |
| separator | 分隔符自定义 | string | '/' |
| separator | 分隔符自定义 | string \| ReactNode | / |
| onClick | 单击事件 | (e:MouseEvent)=>void | - |
| href | 链接的目的地 | string | - |
| isLastItem | 是否为最后一个 item | boolean | false |

##### Option

Expand Down