bigbuffet-rw/app/soapbox/features/ui/components/accordion.js

59 lines
1.6 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import { defineMessages, injectIntl } from 'react-intl';
import classNames from 'classnames';
import DropdownMenu from 'soapbox/containers/dropdown_menu_container';
const messages = defineMessages({
2020-10-01 13:18:52 -07:00
collapse: { id: 'accordion.collapse', defaultMessage: 'Collapse' },
expand: { id: 'accordion.expand', defaultMessage: 'Expand' },
});
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 21:16:50 -08:00
children: PropTypes.oneOfType([PropTypes.string, PropTypes.element, PropTypes.node]),
menu: PropTypes.array,
expanded: PropTypes.bool,
onToggle: PropTypes.func,
intl: PropTypes.object.isRequired,
};
static defaultProps = {
expanded: false,
onToggle: () => {},
}
handleToggle = (e) => {
this.props.onToggle(!this.props.expanded);
2020-10-01 13:18:52 -07:00
e.preventDefault();
}
render() {
const { headline, children, menu, expanded, intl } = this.props;
return (
2020-10-01 13:18:52 -07:00
<div className={classNames('accordion', { 'accordion--expanded' : expanded })}>
{menu && (
<div className='accordion__menu'>
<DropdownMenu items={menu} icon='ellipsis-v' size={18} direction='right' />
</div>
)}
2020-10-01 13:18:52 -07:00
<button
type='button'
className='accordion__title'
onClick={this.handleToggle}
2020-10-01 13:18:52 -07:00
title={intl.formatMessage(expanded ? messages.collapse : messages.expand)}
>
2020-10-01 13:18:52 -07:00
{headline}
</button>
<div className='accordion__content'>
{children}
</div>
</div>
);
}
}