This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(breadcrumb): add a breadcrumb underneath the page header
fix #1770
- Loading branch information
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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( | ||
<Router history={history}> | ||
<Breadcrumb /> | ||
</Router>, | ||
) | ||
} | ||
|
||
it('should render the breadcrumb items', () => { | ||
const wrapper = setup('/patients') | ||
const breadcrumbItem = wrapper.find(HrBreadcrumbItem) | ||
|
||
expect(wrapper.find(HrBreadcrumb)).toHaveLength(1) | ||
expect( | ||
breadcrumbItem.matchesElement(<HrBreadcrumbItem active>patients</HrBreadcrumbItem>), | ||
).toBeTruthy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<HrBreadcrumb> | ||
{items.map((item, index) => { | ||
const isLast = index === lastIndex | ||
const onClick = !isLast ? () => history.push(item.url) : undefined | ||
|
||
return ( | ||
<HrBreadcrumbItem key={item.name} active={isLast} onClick={onClick}> | ||
{item.name} | ||
</HrBreadcrumbItem> | ||
) | ||
})} | ||
</HrBreadcrumb> | ||
) | ||
} | ||
|
||
export default Breadcrumb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters