Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(breadcrumbs): component, make it functional and pluggable #32 #94

Merged
merged 4 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ui/Breadcrumbs/Breadcrumb.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const Template = (args) => (
<Breadcrumb.Divider icon={`${args.icon}`} key={index} />
),
index < sections.length - 1 ? (
<Link key={section.href} to={section.href} className="section">
<Link key={section.key} to={section.href} className="section">
{section.content}{' '}
</Link>
) : (
Expand Down
151 changes: 41 additions & 110 deletions src/ui/Breadcrumbs/Breadcrumbs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,125 +3,56 @@
* @module components/theme/Breadcrumbs/Breadcrumbs
*/

import React, { Component } from 'react';
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';

import { compose } from 'redux';
import { Link } from 'react-router-dom';
import { Breadcrumb, Container, Segment } from 'semantic-ui-react';
import { defineMessages, injectIntl } from 'react-intl';

import { Icon } from '@plone/volto/components';
import { getBreadcrumbs } from '@plone/volto/actions';
import { getBaseUrl, hasApiExpander } from '@plone/volto/helpers';

import homeSVG from '@plone/volto/icons/home.svg';

const messages = defineMessages({
home: {
id: 'Home',
defaultMessage: 'Home',
},
breadcrumbs: {
id: 'Breadcrumbs',
defaultMessage: 'Breadcrumbs',
},
});

/**
* Breadcrumbs container class.
* @class Breadcrumbs
* @extends Component
*/
class Breadcrumbs extends Component {
/**
* Property types.
* @property {Object} propTypes Property types.
* @static
*/
static propTypes = {
getBreadcrumbs: PropTypes.func.isRequired,
pathname: PropTypes.string.isRequired,
root: PropTypes.string,
items: PropTypes.arrayOf(
PropTypes.shape({
title: PropTypes.string,
url: PropTypes.string,
}),
).isRequired,
};

componentDidMount() {
if (!hasApiExpander('breadcrumbs', getBaseUrl(this.props.pathname))) {
this.props.getBreadcrumbs(getBaseUrl(this.props.pathname));
}
}

/**
* Component will receive props
* @method componentWillReceiveProps
* @param {Object} nextProps Next properties
* @returns {undefined}
*/
UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.pathname !== this.props.pathname) {
if (!hasApiExpander('breadcrumbs', getBaseUrl(this.props.pathname))) {
this.props.getBreadcrumbs(getBaseUrl(nextProps.pathname));
}
}
}

/**
* Render method.
* @method render
* @returns {string} Markup for the component.
*/
render() {
return (
<Segment
role="navigation"
aria-label={this.props.intl.formatMessage(messages.breadcrumbs)}
className="breadcrumbs"
secondary
vertical
>
<Container>
<Breadcrumb size={'tiny'}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bring back size passed to breadcrumb, make it so that a prop can change the size of the breadcrumb.
We need tiny which sets size to 12px but other designs might be ok to have simply breadcrumbs which
sets the size to 16px

<Link
to={this.props.root || '/'}
className="section"
title={this.props.intl.formatMessage(messages.home)}
>
<Icon name={homeSVG} size="18px" />
</Link>
{this.props.items.map((item, index, items) => [
<Breadcrumb.Divider key={`divider-${item.url}`} />,
import { Breadcrumb, Container, Image, Segment } from 'semantic-ui-react';

import homeIcon from '@eeacms/volto-eea-design-system/../theme/themes/eea/assets/images/home-icon.svg';

const Breadcrumbs = ({ root, sections = [], icon }) => {
return (
<Segment
role="navigation"
aria-label={'breadcrumbs'}
className="breadcrumbs"
attached
ichim-david marked this conversation as resolved.
Show resolved Hide resolved
>
<Container>
<Breadcrumb>
<Link to={root || '/'} className="section" title={'Home'}>
<Image src={homeIcon} alt="home" />
</Link>
{sections.length > 0 &&
sections.map((item, index, items) => [
<Breadcrumb.Divider icon={icon} key={`divider-${item.href}`} />,
index < items.length - 1 ? (
<Link key={item.url} to={item.url} className="section">
<Link key={item.key} to={item.href} className="section">
{item.title}
</Link>
) : (
<Breadcrumb.Section key={item.url} active>
<Breadcrumb.Section key={item.key} active>
{item.title}
</Breadcrumb.Section>
),
])}
</Breadcrumb>
</Container>
</Segment>
);
}
}
</Breadcrumb>
</Container>
</Segment>
);
};

Breadcrumbs.propTypes = {
root: PropTypes.string,
sections: PropTypes.arrayOf(
PropTypes.shape({
title: PropTypes.string,
href: PropTypes.string,
key: PropTypes.string,
}),
).isRequired,
};

export const BreadcrumbsComponent = Breadcrumbs;
export default compose(
injectIntl,
connect(
(state) => ({
items: state.breadcrumbs.items,
root: state.breadcrumbs.root,
}),
{ getBreadcrumbs },
),
)(Breadcrumbs);

export default Breadcrumbs;