diff --git a/components/breadcrumb/Breadcrumb.tsx b/components/breadcrumb/Breadcrumb.tsx index 7fa95dda1a..9520569de6 100755 --- a/components/breadcrumb/Breadcrumb.tsx +++ b/components/breadcrumb/Breadcrumb.tsx @@ -3,6 +3,7 @@ import * as PropTypes from 'prop-types'; import { cloneElement } from 'react'; import classNames from 'classnames'; import BreadcrumbItem from './BreadcrumbItem'; +import BreadcrumbSeparator from './BreadcrumbSeparator'; import Menu from '../menu'; import { ConfigConsumer, ConfigConsumerProps } from '../config-provider'; import warning from '../_util/warning'; @@ -49,6 +50,7 @@ function defaultItemRender(route: Route, params: any, routes: Route[], paths: st export default class Breadcrumb extends React.Component { static Item: typeof BreadcrumbItem; + static Separator: typeof BreadcrumbSeparator; static defaultProps = { separator: '/', @@ -141,11 +143,14 @@ export default class Breadcrumb extends React.Component { if (!element) { return element; } + warning( - element.type && element.type.__ANT_BREADCRUMB_ITEM, + element.type && + (element.type.__ANT_BREADCRUMB_ITEM || element.type.__ANT_BREADCRUMB_SEPARATOR), 'Breadcrumb', - "Only accepts Breadcrumb.Item as it's children", + "Only accepts Breadcrumb.Item and Breadcrumb.Separator as it's children", ); + return cloneElement(element, { separator, key: index, diff --git a/components/breadcrumb/BreadcrumbItem.tsx b/components/breadcrumb/BreadcrumbItem.tsx index 37936f4f81..6bf9470280 100644 --- a/components/breadcrumb/BreadcrumbItem.tsx +++ b/components/breadcrumb/BreadcrumbItem.tsx @@ -55,7 +55,9 @@ export default class BreadcrumbItem extends React.Component {link} - {separator} + {separator && separator !== '' && ( + {separator} + )} ); } diff --git a/components/breadcrumb/BreadcrumbSeparator.tsx b/components/breadcrumb/BreadcrumbSeparator.tsx new file mode 100644 index 0000000000..0ae6841fcc --- /dev/null +++ b/components/breadcrumb/BreadcrumbSeparator.tsx @@ -0,0 +1,17 @@ +import * as React from 'react'; +import { ConfigConsumer, ConfigConsumerProps } from '../config-provider'; + +export default class BreadcrumbSeparator extends React.Component { + static __ANT_BREADCRUMB_SEPARATOR = true; + + renderSeparator = ({ getPrefixCls }: ConfigConsumerProps) => { + const { children } = this.props; + const prefixCls = getPrefixCls('breadcrumb'); + + return {children || '/'}; + }; + + render() { + return {this.renderSeparator}; + } +} diff --git a/components/breadcrumb/__tests__/Breadcrumb.test.js b/components/breadcrumb/__tests__/Breadcrumb.test.js index 6c62a4c051..83f626c688 100644 --- a/components/breadcrumb/__tests__/Breadcrumb.test.js +++ b/components/breadcrumb/__tests__/Breadcrumb.test.js @@ -14,7 +14,7 @@ describe('Breadcrumb', () => { }); // https://github.com/airbnb/enzyme/issues/875 - it('warns on non-Breadcrumb.Item children', () => { + it('warns on non-Breadcrumb.Item and non-Breadcrumb.Separator children', () => { const MyCom = () =>
foo
; mount( @@ -23,7 +23,7 @@ describe('Breadcrumb', () => { ); expect(errorSpy.mock.calls).toHaveLength(1); expect(errorSpy.mock.calls[0][0]).toMatch( - "Warning: [antd: Breadcrumb] Only accepts Breadcrumb.Item as it's children", + "Warning: [antd: Breadcrumb] Only accepts Breadcrumb.Item and Breadcrumb.Separator as it's children", ); }); diff --git a/components/breadcrumb/__tests__/__snapshots__/demo.test.js.snap b/components/breadcrumb/__tests__/__snapshots__/demo.test.js.snap index 3480ab2f1d..34cd58abc4 100644 --- a/components/breadcrumb/__tests__/__snapshots__/demo.test.js.snap +++ b/components/breadcrumb/__tests__/__snapshots__/demo.test.js.snap @@ -149,99 +149,6 @@ exports[`renders ./components/breadcrumb/demo/overlay.md correctly 1`] = ` `; -exports[`renders ./components/breadcrumb/demo/router.md correctly 1`] = ` -
- - Home Page -
- - Click the navigation above to switch: - - -
-
- - - - Home - - - - / - - -
-
-`; - -exports[`renders ./components/breadcrumb/demo/router-4.md correctly 1`] = ` -
- - Home Page -
- - Click the navigation above to switch: - - -
-
-
-`; - exports[`renders ./components/breadcrumb/demo/separator.md correctly 1`] = `
`; +exports[`renders ./components/breadcrumb/demo/separator-indepent.md correctly 1`] = ` +
+ + + Location + + + + : + + + + Application Center + + + + / + + + + Application List + + + + / + + + + An Application + + +
+`; + exports[`renders ./components/breadcrumb/demo/withIcon.md correctly 1`] = `
+ Location + : + Application Center + + Application List + + An Application + , + mountNode, +); +``` diff --git a/components/breadcrumb/index.tsx b/components/breadcrumb/index.tsx index 5483e2e7b2..5e35e6e75e 100644 --- a/components/breadcrumb/index.tsx +++ b/components/breadcrumb/index.tsx @@ -1,8 +1,10 @@ import Breadcrumb from './Breadcrumb'; import BreadcrumbItem from './BreadcrumbItem'; +import BreadcrumbSeparator from './BreadcrumbSeparator'; export { BreadcrumbProps } from './Breadcrumb'; export { BreadcrumbItemProps } from './BreadcrumbItem'; Breadcrumb.Item = BreadcrumbItem; +Breadcrumb.Separator = BreadcrumbSeparator; export default Breadcrumb; diff --git a/components/breadcrumb/index.zh-CN.md b/components/breadcrumb/index.zh-CN.md index 219692b3ae..c5edbf51be 100644 --- a/components/breadcrumb/index.zh-CN.md +++ b/components/breadcrumb/index.zh-CN.md @@ -26,12 +26,19 @@ title: Breadcrumb ### Breadcrumb.Item -| 参数 | 参数 | 类型 | 默认值 | 版本 | -| --------- | -------------- | -------------------------------------- | ------ | ------ | -| href | 链接的目的地 | string | - | 3.17.0 | -| separator | 自定义的分隔符 | string\|ReactNode | '/' | 3.17.0 | -| overlay | 下拉菜单的内容 | [Menu](/components/menu) \| () => Menu | - | 3.17.0 | -| onClick | 单击事件 | (e:MouseEvent)=>void | - | 3.17.0 | +| 参数 | 参数 | 类型 | 默认值 | 版本 | +| ------- | -------------- | -------------------------------------- | ------ | ------ | +| href | 链接的目的地 | string | - | 3.17.0 | +| overlay | 下拉菜单的内容 | [Menu](/components/menu) \| () => Menu | - | 3.17.0 | +| onClick | 单击事件 | (e:MouseEvent)=>void | - | 3.17.0 | + +### Breadcrumb.Separator `3.21.0` + +| 参数 | 参数 | 类型 | 默认值 | 版本 | +| -------- | -------------- | ----------------- | ------ | ------ | +| children | 要显示的分隔符 | string\|ReactNode | '/' | 3.21.0 | + +> 注意:在使用 `Breadcrumb.Separator` 时,其父组件的分隔符必须设置为 `separator=""`,否则会出现父组件默认的分隔符。 ### routes