Breadcrumb.js
4.8 KB
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
138
139
140
141
142
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import * as React from 'react';
import classNames from 'classnames';
import toArray from "rc-util/es/Children/toArray";
import BreadcrumbItem from './BreadcrumbItem';
import BreadcrumbSeparator from './BreadcrumbSeparator';
import Menu from '../menu';
import { ConfigContext } from '../config-provider';
import devWarning from '../_util/devWarning';
import { cloneElement } from '../_util/reactNode';
function getBreadcrumbName(route, params) {
if (!route.breadcrumbName) {
return null;
}
var paramsKeys = Object.keys(params).join('|');
var name = route.breadcrumbName.replace(new RegExp(":(".concat(paramsKeys, ")"), 'g'), function (replacement, key) {
return params[key] || replacement;
});
return name;
}
function defaultItemRender(route, params, routes, paths) {
var isLastItem = routes.indexOf(route) === routes.length - 1;
var name = getBreadcrumbName(route, params);
return isLastItem ? /*#__PURE__*/React.createElement("span", null, name) : /*#__PURE__*/React.createElement("a", {
href: "#/".concat(paths.join('/'))
}, name);
}
var getPath = function getPath(path, params) {
path = (path || '').replace(/^\//, '');
Object.keys(params).forEach(function (key) {
path = path.replace(":".concat(key), params[key]);
});
return path;
};
var addChildPath = function addChildPath(paths) {
var childPath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var params = arguments.length > 2 ? arguments[2] : undefined;
var originalPaths = _toConsumableArray(paths);
var path = getPath(childPath, params);
if (path) {
originalPaths.push(path);
}
return originalPaths;
};
var Breadcrumb = function Breadcrumb(_a) {
var customizePrefixCls = _a.prefixCls,
_a$separator = _a.separator,
separator = _a$separator === void 0 ? '/' : _a$separator,
style = _a.style,
className = _a.className,
routes = _a.routes,
children = _a.children,
_a$itemRender = _a.itemRender,
itemRender = _a$itemRender === void 0 ? defaultItemRender : _a$itemRender,
_a$params = _a.params,
params = _a$params === void 0 ? {} : _a$params,
restProps = __rest(_a, ["prefixCls", "separator", "style", "className", "routes", "children", "itemRender", "params"]);
var _React$useContext = React.useContext(ConfigContext),
getPrefixCls = _React$useContext.getPrefixCls,
direction = _React$useContext.direction;
var crumbs;
var prefixCls = getPrefixCls('breadcrumb', customizePrefixCls);
if (routes && routes.length > 0) {
// generated by route
var paths = [];
crumbs = routes.map(function (route) {
var path = getPath(route.path, params);
if (path) {
paths.push(path);
} // generated overlay by route.children
var overlay;
if (route.children && route.children.length) {
overlay = /*#__PURE__*/React.createElement(Menu, null, route.children.map(function (child) {
return /*#__PURE__*/React.createElement(Menu.Item, {
key: child.path || child.breadcrumbName
}, itemRender(child, params, routes, addChildPath(paths, child.path, params)));
}));
}
return /*#__PURE__*/React.createElement(BreadcrumbItem, {
overlay: overlay,
separator: separator,
key: path || route.breadcrumbName
}, itemRender(route, params, routes, paths));
});
} else if (children) {
crumbs = toArray(children).map(function (element, index) {
if (!element) {
return element;
}
devWarning(element.type && (element.type.__ANT_BREADCRUMB_ITEM === true || element.type.__ANT_BREADCRUMB_SEPARATOR === true), 'Breadcrumb', "Only accepts Breadcrumb.Item and Breadcrumb.Separator as it's children");
return cloneElement(element, {
separator: separator,
key: index
});
});
}
var breadcrumbClassName = classNames(prefixCls, _defineProperty({}, "".concat(prefixCls, "-rtl"), direction === 'rtl'), className);
return /*#__PURE__*/React.createElement("div", _extends({
className: breadcrumbClassName,
style: style
}, restProps), crumbs);
};
Breadcrumb.Item = BreadcrumbItem;
Breadcrumb.Separator = BreadcrumbSeparator;
export default Breadcrumb;