-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathBreadcrumb.jsx
137 lines (128 loc) · 4.14 KB
/
Breadcrumb.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import PropTypes from '../_util/vue-types';
import { cloneElement } from '../_util/vnode';
import { filterEmpty, getComponentFromProp, getSlotOptions } from '../_util/props-util';
import warning from '../_util/warning';
import { ConfigConsumerProps } from '../config-provider';
import BreadcrumbItem from './BreadcrumbItem';
import Menu from '../menu';
const Route = PropTypes.shape({
path: PropTypes.string,
breadcrumbName: PropTypes.string,
children: PropTypes.array,
}).loose;
const BreadcrumbProps = {
prefixCls: PropTypes.string,
routes: PropTypes.arrayOf(Route),
params: PropTypes.any,
separator: PropTypes.any,
itemRender: PropTypes.func,
};
function getBreadcrumbName(route, params) {
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;
}
export default {
name: 'ABreadcrumb',
props: BreadcrumbProps,
inject: {
configProvider: { default: () => ConfigConsumerProps },
},
methods: {
defaultItemRender({ route, params, routes, paths }) {
const isLastItem = routes.indexOf(route) === routes.length - 1;
const name = getBreadcrumbName(route, params);
return isLastItem ? <span>{name}</span> : <a href={`#/${paths.join('/')}`}>{name}</a>;
},
getPath(path, params) {
path = (path || '').replace(/^\//, '');
Object.keys(params).forEach(key => {
path = path.replace(`:${key}`, params[key]);
});
return path;
},
addChildPath(paths, childPath, params) {
const originalPaths = [...paths];
const path = this.getPath(childPath, params);
if (path) {
originalPaths.push(path);
}
return originalPaths;
},
genForRoutes({ routes = [], params = {}, separator, itemRender = this.defaultItemRender }) {
const paths = [];
return routes.map(route => {
const path = this.getPath(route.path, params);
if (path) {
paths.push(path);
}
// generated overlay by route.children
let overlay = null;
if (route.children && route.children.length) {
overlay = (
<Menu>
{route.children.map(child => (
<Menu.Item key={child.path || child.breadcrumbName}>
{itemRender({
route: child,
params,
routes,
paths: this.addChildPath(paths, child.path, params),
h: this.$createElement,
})}
</Menu.Item>
))}
</Menu>
);
}
return (
<BreadcrumbItem
overlay={overlay}
separator={separator}
key={path || route.breadcrumbName}
>
{itemRender({ route, params, routes, paths, h: this.$createElement })}
</BreadcrumbItem>
);
});
},
},
render() {
let crumbs;
const { prefixCls: customizePrefixCls, routes, params = {}, $slots, $scopedSlots } = this;
const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('breadcrumb', customizePrefixCls);
const children = filterEmpty($slots.default);
const separator = getComponentFromProp(this, 'separator');
const itemRender = this.itemRender || $scopedSlots.itemRender || this.defaultItemRender;
if (routes && routes.length > 0) {
// generated by route
crumbs = this.genForRoutes({
routes,
params,
separator,
itemRender,
});
} else if (children.length) {
crumbs = children.map((element, index) => {
warning(
getSlotOptions(element).__ANT_BREADCRUMB_ITEM ||
getSlotOptions(element).__ANT_BREADCRUMB_SEPARATOR,
'Breadcrumb',
"Only accepts Breadcrumb.Item and Breadcrumb.Separator as it's children",
);
return cloneElement(element, {
props: { separator },
key: index,
});
});
}
return <div class={prefixCls}>{crumbs}</div>;
},
};