2020-09-30 12:09:00 -07:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
2020-10-01 13:18:52 -07:00
|
|
|
collapse: { id: 'accordion.collapse', defaultMessage: 'Collapse' },
|
|
|
|
expand: { id: 'accordion.expand', defaultMessage: 'Expand' },
|
2020-09-30 12:09:00 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
export default @injectIntl class Accordion extends React.PureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
2020-10-01 14:17:32 -07:00
|
|
|
headline: PropTypes.node.isRequired,
|
2020-12-31 18:18:57 -08:00
|
|
|
children: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
|
2020-10-01 13:54:29 -07:00
|
|
|
expanded: PropTypes.bool,
|
|
|
|
onToggle: PropTypes.func,
|
2020-09-30 12:09:00 -07:00
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
2020-10-01 13:54:29 -07:00
|
|
|
static defaultProps = {
|
2020-09-30 12:09:00 -07:00
|
|
|
expanded: false,
|
2020-10-01 13:54:29 -07:00
|
|
|
onToggle: () => {},
|
2020-09-30 12:09:00 -07:00
|
|
|
}
|
|
|
|
|
2020-10-01 13:54:29 -07:00
|
|
|
handleToggle = (e) => {
|
|
|
|
this.props.onToggle(!this.props.expanded);
|
2020-10-01 13:18:52 -07:00
|
|
|
e.preventDefault();
|
2020-09-30 12:09:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-12-31 18:18:57 -08:00
|
|
|
const { headline, children, expanded, intl } = this.props;
|
2020-09-30 12:09:00 -07:00
|
|
|
|
|
|
|
return (
|
2020-10-01 13:18:52 -07:00
|
|
|
<div className={classNames('accordion', { 'accordion--expanded' : expanded })}>
|
|
|
|
<button
|
|
|
|
type='button'
|
|
|
|
className='accordion__title'
|
2020-10-01 13:54:29 -07:00
|
|
|
onClick={this.handleToggle}
|
2020-10-01 13:18:52 -07:00
|
|
|
title={intl.formatMessage(expanded ? messages.collapse : messages.expand)}
|
2020-09-30 12:09:00 -07:00
|
|
|
>
|
2020-10-01 13:18:52 -07:00
|
|
|
{headline}
|
|
|
|
</button>
|
|
|
|
<div className='accordion__content'>
|
2020-12-31 18:18:57 -08:00
|
|
|
{children}
|
2020-09-30 12:09:00 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|