-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Breadcrumbs to a Component, close #51
- Loading branch information
Jovert Lota Palonpon
committed
Apr 28, 2019
1 parent
86f718e
commit f924fef
Showing
4 changed files
with
116 additions
and
86 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,106 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Link as RouterLink } from 'react-router-dom'; | ||
|
||
import { Link, Typography, withStyles } from '@material-ui/core'; | ||
|
||
import { Breadcrumbs as MuiBreadcrumbs } from '@material-ui/lab'; | ||
|
||
import { Home as HomeIcon } from '@material-ui/icons'; | ||
|
||
import * as NavigationUtils from '../utils/Navigation'; | ||
import * as StringUtils from '../utils/String'; | ||
|
||
function Breadcrumbs(props) { | ||
const { classes, segments, blacklistedSegments, ...other } = props; | ||
|
||
return ( | ||
<MuiBreadcrumbs arial-label="Breadcrumb" {...other}> | ||
{segments.length > 0 ? ( | ||
<Link | ||
color="inherit" | ||
component={linkProps => ( | ||
<RouterLink | ||
{...linkProps} | ||
to={NavigationUtils.route('backoffice.home')} | ||
/> | ||
)} | ||
className={classes.breadcrumb} | ||
> | ||
<HomeIcon className={classes.icon} /> | ||
</Link> | ||
) : ( | ||
<HomeIcon className={classes.icon} /> | ||
)} | ||
|
||
{segments.map((segment, key) => { | ||
const renderText = ( | ||
<Typography key={key} className={classes.breadcrumb}> | ||
{StringUtils.uppercaseFirst(segment)} | ||
</Typography> | ||
); | ||
|
||
// Make a blacklisted segment plain text | ||
if (blacklistedSegments.indexOf(segment) > -1) { | ||
return renderText; | ||
} | ||
|
||
// The last segment should be plain text | ||
if (key + 1 === segments.length) { | ||
return renderText; | ||
} | ||
|
||
// Make numeric segment hidden | ||
if (!isNaN(parseInt(segment))) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Link | ||
key={key} | ||
color="inherit" | ||
component={linkProps => ( | ||
<RouterLink | ||
{...linkProps} | ||
to={ | ||
'/' + | ||
segments | ||
.filter( | ||
(s, i) => | ||
i <= segments.indexOf(segment), | ||
) | ||
.join('/') | ||
} | ||
/> | ||
)} | ||
className={classes.breadcrumb} | ||
> | ||
{StringUtils.uppercaseFirst(segment)} | ||
</Link> | ||
); | ||
})} | ||
</MuiBreadcrumbs> | ||
); | ||
} | ||
|
||
Breadcrumbs.propTypes = { | ||
segments: PropTypes.array.isRequired, | ||
blacklistedSegments: PropTypes.array, | ||
}; | ||
|
||
Breadcrumbs.defaultProps = { | ||
blacklistedSegments: [], | ||
}; | ||
|
||
const styles = theme => ({ | ||
breadcrumb: { | ||
display: 'flex', | ||
}, | ||
|
||
icon: { | ||
marginRight: theme.spacing.unit / 2, | ||
width: 20, | ||
}, | ||
}); | ||
|
||
export default withStyles(styles)(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
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