From 65b40aeba2c9f75d4d5944e046cdaa7da126f1d1 Mon Sep 17 00:00:00 2001 From: oliv37 Date: Sat, 8 Feb 2020 16:28:41 +0100 Subject: [PATCH] feat(breadcrumb): add a breadcrumb underneath the page header fix #1770 --- src/HospitalRun.tsx | 2 + src/__tests__/components/Breadcrumb.test.tsx | 33 ++++++++++++++ src/components/Breadcrumb.tsx | 46 ++++++++++++++++++++ src/index.css | 14 +++++- 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/components/Breadcrumb.test.tsx create mode 100644 src/components/Breadcrumb.tsx diff --git a/src/HospitalRun.tsx b/src/HospitalRun.tsx index 158dda828d..f9bf8567d0 100644 --- a/src/HospitalRun.tsx +++ b/src/HospitalRun.tsx @@ -5,6 +5,7 @@ import { Toaster } from '@hospitalrun/components' import Appointments from 'scheduling/appointments/Appointments' import NewAppointment from 'scheduling/appointments/new/NewAppointment' import ViewAppointment from 'scheduling/appointments/view/ViewAppointment' +import Breadcrumb from 'components/Breadcrumb' import Sidebar from './components/Sidebar' import Permissions from './model/Permissions' import Dashboard from './dashboard/Dashboard' @@ -21,6 +22,7 @@ const HospitalRun = () => { return (
+
diff --git a/src/__tests__/components/Breadcrumb.test.tsx b/src/__tests__/components/Breadcrumb.test.tsx new file mode 100644 index 0000000000..3ad64f2d5c --- /dev/null +++ b/src/__tests__/components/Breadcrumb.test.tsx @@ -0,0 +1,33 @@ +import '../../__mocks__/matchMediaMock' +import React from 'react' +import { mount } from 'enzyme' +import { createMemoryHistory } from 'history' +import { Router } from 'react-router' +import Breadcrumb from 'components/Breadcrumb' +import { + Breadcrumb as HrBreadcrumb, + BreadcrumbItem as HrBreadcrumbItem, +} from '@hospitalrun/components' + +describe('Breadcrumb', () => { + let history = createMemoryHistory() + const setup = (location: string) => { + history = createMemoryHistory() + history.push(location) + return mount( + + + , + ) + } + + it('should render the breadcrumb items', () => { + const wrapper = setup('/patients') + const breadcrumbItem = wrapper.find(HrBreadcrumbItem) + + expect(wrapper.find(HrBreadcrumb)).toHaveLength(1) + expect( + breadcrumbItem.matchesElement(patients), + ).toBeTruthy() + }) +}) diff --git a/src/components/Breadcrumb.tsx b/src/components/Breadcrumb.tsx new file mode 100644 index 0000000000..e0de776d84 --- /dev/null +++ b/src/components/Breadcrumb.tsx @@ -0,0 +1,46 @@ +import React from 'react' +import { useLocation, useHistory } from 'react-router' +import { + Breadcrumb as HrBreadcrumb, + BreadcrumbItem as HrBreadcrumbItem, +} from '@hospitalrun/components' + +interface Item { + name: string + url: string +} + +function getItems(pathname: string): Item[] { + if (!pathname || pathname === '/') { + return [{ name: 'dashboard', url: '/' }] + } + + return pathname + .substring(1) + .split('/') + .map((name) => ({ name, url: '/' })) +} + +const Breadcrumb = () => { + const { pathname } = useLocation() + const history = useHistory() + const items = getItems(pathname) + const lastIndex = items.length - 1 + + return ( + + {items.map((item, index) => { + const isLast = index === lastIndex + const onClick = !isLast ? () => history.push(item.url) : undefined + + return ( + + {item.name} + + ) + })} + + ) +} + +export default Breadcrumb diff --git a/src/index.css b/src/index.css index 49f3c5114d..4dba66ac57 100644 --- a/src/index.css +++ b/src/index.css @@ -24,7 +24,7 @@ code { bottom: 0; left: 0; z-index: 0; /* Behind the navbar */ - padding: 48px 0 0; /* Height of navbar */ + padding: 75px 0 0; /* Height of navbar */ box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1); } @@ -88,3 +88,15 @@ code { border-color: transparent; box-shadow: 0 0 0 3px rgba(255, 255, 255, .25); } + +.breadcrumb { + z-index: 1; + position: relative; + padding: .2rem 1rem; + background-color: white; + border-bottom: .1rem solid lightgray; +} + +.breadcrumb-item > span, .breadcrumb-item > a { + text-transform: capitalize; +}