UNPKG

linkmore-design

Version:

🌈 πŸš€lmη»„δ»ΆεΊ“γ€‚πŸš€

115 lines β€’ 4.63 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; var _excluded = ["prefixCls", "separator", "style", "className", "routes", "children", "itemRender", "params"]; import classNames from 'classnames'; import toArray from 'rc-util/lib/Children/toArray'; import * as React from 'react'; import { ConfigContext } from "../config-provider"; import Menu from "../menu"; import { cloneElement } from "../_util/reactNode"; import warning from "../_util/warning"; import BreadcrumbItem from "./BreadcrumbItem"; import BreadcrumbSeparator from "./BreadcrumbSeparator"; 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, childPath, params) { var originalPaths = _toConsumableArray(paths); var path = getPath(childPath || '', params); if (path) { originalPaths.push(path); } return originalPaths; }; var Breadcrumb = function Breadcrumb(_ref) { var customizePrefixCls = _ref.prefixCls, _ref$separator = _ref.separator, separator = _ref$separator === void 0 ? '/' : _ref$separator, style = _ref.style, className = _ref.className, routes = _ref.routes, children = _ref.children, _ref$itemRender = _ref.itemRender, itemRender = _ref$itemRender === void 0 ? defaultItemRender : _ref$itemRender, _ref$params = _ref.params, params = _ref$params === void 0 ? {} : _ref$params, restProps = _objectWithoutProperties(_ref, _excluded); 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, { items: route.children.map(function (child) { return { key: child.path || child.breadcrumbName, label: itemRender(child, params, routes, addChildPath(_paths, child.path, params)) }; }) }); } var itemProps = { separator: separator }; if (overlay) { itemProps.overlay = overlay; } return /*#__PURE__*/React.createElement(BreadcrumbItem, _extends({}, itemProps, { key: path || route.breadcrumbName }), itemRender(route, params, routes, _paths)); }); } else if (children) { crumbs = toArray(children).map(function (element, index) { if (!element) { return element; } warning(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("nav", _extends({ className: breadcrumbClassName, style: style }, restProps), /*#__PURE__*/React.createElement("ol", null, crumbs)); }; Breadcrumb.Item = BreadcrumbItem; Breadcrumb.Separator = BreadcrumbSeparator; export default Breadcrumb;