Skip to content

Commit 0fa1581

Browse files
committed
refactor: breadcrumb
1 parent 6ad0f9d commit 0fa1581

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed

components/breadcrumb/Breadcrumb.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { cloneVNode, defineComponent, PropType, ExtractPropTypes } from 'vue';
22
import PropTypes from '../_util/vue-types';
3-
import { filterEmpty, getPropsSlot } from '../_util/props-util';
3+
import { flattenChildren, getPropsSlot } from '../_util/props-util';
44
import warning from '../_util/warning';
55
import BreadcrumbItem from './BreadcrumbItem';
66
import Menu from '../menu';
@@ -17,7 +17,7 @@ const breadcrumbProps = {
1717
prefixCls: PropTypes.string,
1818
routes: { type: Array as PropType<Route[]> },
1919
params: PropTypes.any,
20-
separator: PropTypes.VNodeChild,
20+
separator: PropTypes.any,
2121
itemRender: {
2222
type: Function as PropType<
2323
(opt: { route: Route; params: unknown; routes: Route[]; paths: string[] }) => VueNode
@@ -53,6 +53,7 @@ function defaultItemRender(opt: {
5353
export default defineComponent({
5454
name: 'ABreadcrumb',
5555
props: breadcrumbProps,
56+
slots: ['separator', 'itemRender'],
5657
setup(props, { slots }) {
5758
const { prefixCls, direction } = useConfigInject('breadcrumb', props);
5859

@@ -122,10 +123,10 @@ export default defineComponent({
122123

123124
const { routes, params = {} } = props;
124125

125-
const children = filterEmpty(getPropsSlot(slots, props));
126-
const separator = getPropsSlot(slots, props, 'separator');
126+
const children = flattenChildren(getPropsSlot(slots, props));
127+
const separator = getPropsSlot(slots, props, 'separator') ?? '/';
127128

128-
const itemRender = getPropsSlot(slots, props, 'itemRender') || defaultItemRender;
129+
const itemRender = props.itemRender || slots.itemRender || defaultItemRender;
129130
if (routes && routes.length > 0) {
130131
// generated by route
131132
crumbs = genForRoutes({
@@ -153,5 +154,4 @@ export default defineComponent({
153154
return <div class={breadcrumbClassName}>{crumbs}</div>;
154155
};
155156
},
156-
methods: {},
157157
});

components/breadcrumb/BreadcrumbItem.tsx

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ import useConfigInject from '../_util/hooks/useConfigInject';
88
const breadcrumbItemProps = {
99
prefixCls: PropTypes.string,
1010
href: PropTypes.string,
11-
separator: PropTypes.VNodeChild.def('/'),
12-
overlay: PropTypes.VNodeChild,
11+
separator: PropTypes.any,
12+
overlay: PropTypes.any,
1313
};
1414

1515
export type BreadcrumbItemProps = Partial<ExtractPropTypes<typeof breadcrumbItemProps>>;
1616
export default defineComponent({
1717
name: 'ABreadcrumbItem',
1818
__ANT_BREADCRUMB_ITEM: true,
1919
props: breadcrumbItemProps,
20+
slots: ['separator', 'overlay'],
2021
setup(props, { slots }) {
2122
const { prefixCls } = useConfigInject('breadcrumb', props);
2223
/**
@@ -39,11 +40,11 @@ export default defineComponent({
3940
};
4041

4142
return () => {
42-
const separator = getPropsSlot(slots, props, 'separator');
43+
const separator = getPropsSlot(slots, props, 'separator') ?? '/';
4344
const children = getPropsSlot(slots, props);
4445
let link: JSX.Element;
4546

46-
if ('href' in props) {
47+
if (props.href !== undefined) {
4748
link = <a class={`${prefixCls.value}-link`}>{children}</a>;
4849
} else {
4950
link = <span class={`${prefixCls.value}-link`}>{children}</span>;
@@ -54,9 +55,7 @@ export default defineComponent({
5455
return (
5556
<span>
5657
{link}
57-
{separator && separator !== '' && (
58-
<span class={`${prefixCls.value}-separator`}>{separator}</span>
59-
)}
58+
{separator && <span class={`${prefixCls.value}-separator`}>{separator}</span>}
6059
</span>
6160
);
6261
}

components/breadcrumb/BreadcrumbSeparator.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineComponent, ExtractPropTypes } from 'vue';
22
import PropTypes from '../_util/vue-types';
3-
import { getPropsSlot } from '../_util/props-util';
3+
import { flattenChildren } from '../_util/props-util';
44
import useConfigInject from '../_util/hooks/useConfigInject';
55

66
const breadcrumbSeparator = {
@@ -18,8 +18,7 @@ export default defineComponent({
1818

1919
return () => {
2020
const { separator, class: className, ...restAttrs } = attrs;
21-
const children = getPropsSlot(slots, props) || [];
22-
21+
const children = flattenChildren(slots.default?.());
2322
return (
2423
<span class={[`${prefixCls.value}-separator`, className]} {...restAttrs}>
2524
{children.length > 0 ? children : '/'}

components/breadcrumb/style/index.less

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
}
3939

4040
&-link {
41-
> .@{iconfont-css-prefix} + span {
41+
> .@{iconfont-css-prefix} + span,
42+
> .@{iconfont-css-prefix} + a {
4243
margin-left: 4px;
4344
}
4445
}
@@ -49,3 +50,5 @@
4950
}
5051
}
5152
}
53+
54+
@import './rtl';

components/breadcrumb/style/rtl.less

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.@{breadcrumb-prefix-cls} {
2+
&-rtl {
3+
.clearfix();
4+
direction: rtl;
5+
6+
> span {
7+
float: right;
8+
}
9+
}
10+
11+
&-link {
12+
> .@{iconfont-css-prefix} + span,
13+
> .@{iconfont-css-prefix} + a {
14+
.@{breadcrumb-prefix-cls}-rtl & {
15+
margin-right: 4px;
16+
margin-left: 0;
17+
}
18+
}
19+
}
20+
21+
&-overlay-link {
22+
> .@{iconfont-css-prefix} {
23+
.@{breadcrumb-prefix-cls}-rtl & {
24+
margin-right: 4px;
25+
margin-left: 0;
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)