UNPKG

aws-northstar

Version:
63 lines (59 loc) 3.95 kB
/** ******************************************************************************************************************* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. * ******************************************************************************************************************** */ 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 React from 'react'; import { Route } from 'react-router'; import Typography from '@material-ui/core/Typography'; import MaterialBreadcrumbs from '@material-ui/core/Breadcrumbs'; import NavigateNextIcon from '@material-ui/icons/NavigateNext'; import { matchPath } from 'react-router'; import Link from '../Link'; const matchRoute = (path, availableRoutes) => { return availableRoutes.length > 0 ? availableRoutes.some((r) => matchPath(path, r)) : true; }; /** * The Breadcrumb group is rendered as part of AppLayout on top of main content area to provide page navigation information. */ const BreadcrumbGroup = (_a) => { var { items, rootPath = 'Home', availableRoutes = [] } = _a, props = __rest(_a, ["items", "rootPath", "availableRoutes"]); const testId = props['data-testid'] || 'breadcrumb'; if (!items) { return (React.createElement(Route, null, ({ location }) => { const pathnames = location.pathname.split('/').filter((e) => e); pathnames.unshift('/'); return (React.createElement(MaterialBreadcrumbs, { separator: React.createElement(NavigateNextIcon, { fontSize: "small" }), "aria-label": "Breadcrumb", "data-testid": testId }, pathnames.map((value, index) => { const last = index === pathnames.length - 1; const segment = pathnames.slice(0, index + 1); const to = segment.length === 1 ? '/' : `/${segment.slice(1).join('/')}`; const text = value .split(' ') .map((s) => s.charAt(0).toUpperCase() + s.substring(1)) .join(' '); return last ? (React.createElement(Typography, { color: "inherit", key: to }, pathnames.length === 1 && value === '/' ? rootPath : text)) : matchRoute(to, availableRoutes) ? (React.createElement(Link, { key: to, href: to }, value === '/' ? rootPath : text)) : (React.createElement(Typography, { color: "inherit", key: to }, value === '/' ? rootPath : text)); }))); })); } return (React.createElement(Route, null, React.createElement(MaterialBreadcrumbs, { separator: React.createElement(NavigateNextIcon, { fontSize: "small" }), "aria-label": "Breadcrumb", "data-testid": testId }, items.map((item, idx) => idx === items.length - 1 ? (React.createElement(Typography, { key: item.text, color: "textPrimary" }, item.text)) : (React.createElement(Link, { key: item.text, href: item.href }, item.text)))))); }; export default BreadcrumbGroup;