-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
423 additions
and
37 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// @flow | ||
/* eslint-disable react/jsx-key */ | ||
import React, { Fragment } from 'react'; | ||
|
||
import { createStyledTag, createTheme } from '../../utils'; | ||
import type { BreadcrumbsRoutes, BreadcrumbsMatchPath } from './Breadcrumbs.types'; | ||
import { getBreadcrumbs } from './Breadcrumbs.utils'; | ||
import { BreadcrumbsItem } from './BreadcrumbsItem'; | ||
import { BreadcrumbsDivider } from './BreadcrumbsDivider'; | ||
|
||
type BreadcrumbsProps = {| | ||
/* the location pathname */ | ||
pathname: string, | ||
/* list of breadcrumbs routes */ | ||
routes: BreadcrumbsRoutes, | ||
/* custom match path function */ | ||
matchPath?: BreadcrumbsMatchPath, | ||
/* custom breadcrum's item tag */ | ||
itemTagName?: string | React$ComponentType<*>, | ||
|}; | ||
|
||
const name = 'breadcrumbs'; | ||
|
||
const theme = createTheme(name, { | ||
modifiers: { | ||
}, | ||
defaults: { | ||
}, | ||
}); | ||
|
||
const BreadcrumbsTag = createStyledTag(name, {}); | ||
|
||
const Breadcrumbs = ({ itemTagName, pathname, routes, matchPath, ...rest }: BreadcrumbsProps) => { | ||
const breadcrumbs = getBreadcrumbs(pathname, routes, matchPath); | ||
|
||
return ( | ||
<BreadcrumbsTag { ...rest }> | ||
{ | ||
React.Children.toArray( | ||
breadcrumbs.map((item, index) => ( | ||
<Fragment> | ||
<BreadcrumbsItem to={ item.originalPath } { ...{ ...rest, ...item, tagName: itemTagName } } /> | ||
{ index !== breadcrumbs.length - 1 && <BreadcrumbsDivider>></BreadcrumbsDivider> } | ||
</Fragment> | ||
)), | ||
) | ||
} | ||
</BreadcrumbsTag> | ||
); | ||
}; | ||
|
||
export { Breadcrumbs, theme }; |
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,27 @@ | ||
import React from 'react'; | ||
|
||
const routes = [ | ||
{ path: '/', label: 'Dashboard' }, | ||
{ path: '/app', component: ({ param }) => param }, | ||
{ path: '/app/settings', label: 'Settings' }, | ||
{ path: '/app/settings/security', label: 'Security' }, | ||
]; | ||
|
||
export default (asStory) => { | ||
asStory('ATOMS/Breadcrumbs', module, (story, { Breadcrumbs, Button, Link, Row }) => { | ||
story | ||
.add('with default modifiers', () => ( | ||
<Breadcrumbs pathname="/app/settings/security" routes={ routes } param={ 1 } /> | ||
)); | ||
story | ||
.add('with custom tagName', () => ( | ||
<Breadcrumbs | ||
tagName={ Row } | ||
pathname="/app/settings/security" | ||
routes={ routes } | ||
param={ 1 } | ||
itemTagName={ ({ label, ...rest }) => <Button tagName={ Link } { ...rest } text={ label } /> } | ||
/> | ||
)); | ||
}); | ||
}; |
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,8 @@ | ||
export type BreadcrumbsRoutes = Array<{ | ||
path: string, | ||
component?: React$ComponentType<*>, | ||
label?: string, | ||
matchOptions?: Object, | ||
}>; | ||
|
||
export type BreadcrumbsMatchPath = (path: string, options: Object) => boolean; |
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,37 @@ | ||
// @flow | ||
import type { BreadcrumbsRoutes, BreadcrumbsMatchPath } from './Breadcrumbs.types'; | ||
|
||
const getPaths = (pathname: string) => pathname.replace(/\/$/, '').split('/').reduce((result, path, index) => [ | ||
...result, | ||
index < 2 ? `/${path}` : `${result[result.length - 1]}/${path}`, | ||
], []); | ||
|
||
|
||
const matchPathDefault: BreadcrumbsMatchPath = (path: string, options = {}) => path === options.path; | ||
|
||
const getBreadcrumbs = ( | ||
pathname: string, | ||
routes: BreadcrumbsRoutes, | ||
matchPath: BreadcrumbsMatchPath = matchPathDefault, | ||
) => { | ||
const paths = getPaths(pathname); | ||
|
||
const breadcrumbs = paths.reduce((result, path) => { | ||
const matchedRoute = routes.find( | ||
(route) => matchPath ? matchPath(path, { path: route.path, ...(route.matchOptions || {}) }) : route.path === path, | ||
); | ||
|
||
if (matchedRoute) { | ||
const match = matchPath(path, { path: matchedRoute.path, ...(matchedRoute.matchOptions || {}) }); | ||
|
||
result = [...result, { ...matchedRoute, originalPath: path, match }]; | ||
} | ||
|
||
return result; | ||
}, []); | ||
|
||
return breadcrumbs; | ||
}; | ||
|
||
|
||
export { getBreadcrumbs }; |
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,10 @@ | ||
// @flow | ||
import styled from 'react-emotion'; | ||
|
||
const BreadcrumbsDivider = styled('span')({ | ||
padding: '0 1rem', | ||
fontSize: '1.6rem', | ||
fontFamily: 'Poppins', | ||
}); | ||
|
||
export { BreadcrumbsDivider }; |
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,25 @@ | ||
// @flow | ||
import React from 'react'; | ||
|
||
import { Link } from '../typography/Link'; | ||
|
||
type BreadcrumbsItemProps = {| | ||
tagName: string | React$ComponentType<*>, | ||
to: string, | ||
label?: string, | ||
component?: React$ComponentType<*>, | ||
|}; | ||
|
||
const BreadcrumbsItem = ({ tagName, to, label, component: Component, ...rest }: BreadcrumbsItemProps) => React.createElement( | ||
tagName, | ||
{ | ||
to, | ||
}, | ||
Component ? <Component { ...rest } to={ to } label={ label } /> : label, | ||
); | ||
|
||
BreadcrumbsItem.defaultProps = { | ||
tagName: Link, | ||
}; | ||
|
||
export { BreadcrumbsItem }; |
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 @@ | ||
export * from './Breadcrumbs'; |
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
Oops, something went wrong.