From df27b8fe06f724dc0c8b39b0c241c20cdc860c98 Mon Sep 17 00:00:00 2001 From: lengcb <649569822@qq.com> Date: Wed, 19 Aug 2020 17:28:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(components=EF=BC=8Cwebsite):=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84Breadcrumb=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit affects: @gio-design/components, website --- .../src/components/breadcrumb/Breadcrumb.tsx | 39 ++++++++++++------- .../components/breadcrumb/BreadcrumbItem.tsx | 11 +++--- .../breadcrumb/BreadcrumbSeparator.tsx | 2 +- .../components/breadcrumb/style/index.less | 3 ++ .../basic/breadcrumb/demo/basic.tsx | 28 ++++++++----- .../basic/breadcrumb/index.zh-CN.md | 5 +-- 6 files changed, 57 insertions(+), 31 deletions(-) diff --git a/packages/components/src/components/breadcrumb/Breadcrumb.tsx b/packages/components/src/components/breadcrumb/Breadcrumb.tsx index 9a4bacf885..d77a9dcfe9 100644 --- a/packages/components/src/components/breadcrumb/Breadcrumb.tsx +++ b/packages/components/src/components/breadcrumb/Breadcrumb.tsx @@ -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[]; } 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, paths: Array) => React.ReactNode; - children?: React.ReactNode; } function getBreadcrumbName(route: Route, params: any) { @@ -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) ? {name} : {name}; + return isLastItem(route, routes) ? {name} : {name}; } const getPath = (path: string, params: any) => { @@ -52,9 +55,21 @@ interface BreadcrumbInterface extends React.FC { 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) => { @@ -63,14 +78,8 @@ const Breadcrumb: BreadcrumbInterface = (props: BreadcrumbProps) => { if (path) { paths.push(path); } - return ( - + {itemRender(route, params, routes, paths)} ); @@ -78,7 +87,11 @@ const Breadcrumb: BreadcrumbInterface = (props: BreadcrumbProps) => { } else if (children) { crumbs = children; } - return
{crumbs}
; + return ( +
+ {crumbs} +
+ ); }; Breadcrumb.Item = BreadcrumbItem; diff --git a/packages/components/src/components/breadcrumb/BreadcrumbItem.tsx b/packages/components/src/components/breadcrumb/BreadcrumbItem.tsx index 7bbb2432c3..f7b8fdb5ec 100644 --- a/packages/components/src/components/breadcrumb/BreadcrumbItem.tsx +++ b/packages/components/src/components/breadcrumb/BreadcrumbItem.tsx @@ -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; children?: React.ReactNode; @@ -12,15 +12,16 @@ export interface BreadcrumbItemProps { const BreadcrumbItem: React.FC = (props: BreadcrumbItemProps) => { const { href, children, separator, isLastItem } = props; + /* eslint-disable prettier/prettier */ const link = href ? ( {children} ) : ( - - {children} - - ); + + {children} + + ); if (children) { return ( diff --git a/packages/components/src/components/breadcrumb/BreadcrumbSeparator.tsx b/packages/components/src/components/breadcrumb/BreadcrumbSeparator.tsx index 7034b64c61..8b39d7c7ac 100644 --- a/packages/components/src/components/breadcrumb/BreadcrumbSeparator.tsx +++ b/packages/components/src/components/breadcrumb/BreadcrumbSeparator.tsx @@ -1,7 +1,7 @@ import React from 'react'; export interface BreadcrumbSeparatorProps { - separator?: string; + separator?: React.ReactNode; } const BreadcrumbSeparator: React.FC = (props: BreadcrumbSeparatorProps) => { diff --git a/packages/components/src/components/breadcrumb/style/index.less b/packages/components/src/components/breadcrumb/style/index.less index 43b1c5142c..1c39645588 100644 --- a/packages/components/src/components/breadcrumb/style/index.less +++ b/packages/components/src/components/breadcrumb/style/index.less @@ -30,4 +30,7 @@ padding: 0 8px; text-align: center; } + & > span:last-child &-separator { + display: none; + } } diff --git a/packages/website/src/components/basic/breadcrumb/demo/basic.tsx b/packages/website/src/components/basic/breadcrumb/demo/basic.tsx index 283790ca6c..d83d25f8d9 100644 --- a/packages/website/src/components/basic/breadcrumb/demo/basic.tsx +++ b/packages/website/src/components/basic/breadcrumb/demo/basic.tsx @@ -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', breadcrumbName: '首页', - onClick: () => { - alert('首页'); - }, }, { + path: 'basic', breadcrumbName: '一级面包屑', - onClick: () => { - alert('一级面包屑'); - }, }, { + path: 'breadcrumb', breadcrumbName: '二级面包屑', }, ]; - return ; + return ( +
+ + + 首页 + + 一级面包屑 + + + 二级面包屑 + + 三级面包屑 + +
+ ); }; diff --git a/packages/website/src/components/basic/breadcrumb/index.zh-CN.md b/packages/website/src/components/basic/breadcrumb/index.zh-CN.md index c9219eddba..a2da20d454 100644 --- a/packages/website/src/components/basic/breadcrumb/index.zh-CN.md +++ b/packages/website/src/components/basic/breadcrumb/index.zh-CN.md @@ -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