You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

7 years ago
  1. import React from 'react';
  2. import { Motion, spring } from 'react-motion';
  3. import PropTypes from 'prop-types';
  4. class ColumnCollapsable extends React.PureComponent {
  5. static propTypes = {
  6. icon: PropTypes.string.isRequired,
  7. title: PropTypes.string,
  8. fullHeight: PropTypes.number.isRequired,
  9. children: PropTypes.node,
  10. onCollapse: PropTypes.func
  11. };
  12. state = {
  13. collapsed: true
  14. };
  15. handleToggleCollapsed = () => {
  16. const currentState = this.state.collapsed;
  17. this.setState({ collapsed: !currentState });
  18. if (!currentState && this.props.onCollapse) {
  19. this.props.onCollapse();
  20. }
  21. }
  22. render () {
  23. const { icon, title, fullHeight, children } = this.props;
  24. const { collapsed } = this.state;
  25. const collapsedClassName = collapsed ? 'collapsable-collapsed' : 'collapsable';
  26. return (
  27. <div className='column-collapsable'>
  28. <div role='button' tabIndex='0' title={`${title}`} className={`column-icon ${collapsedClassName}`} onClick={this.handleToggleCollapsed}>
  29. <i className={`fa fa-${icon}`} />
  30. </div>
  31. <Motion defaultStyle={{ opacity: 0, height: 0 }} style={{ opacity: spring(collapsed ? 0 : 100), height: spring(collapsed ? 0 : fullHeight, collapsed ? undefined : { stiffness: 150, damping: 9 }) }}>
  32. {({ opacity, height }) =>
  33. <div style={{ overflow: height === fullHeight ? 'auto' : 'hidden', height: `${height}px`, opacity: opacity / 100, maxHeight: '70vh' }}>
  34. {children}
  35. </div>
  36. }
  37. </Motion>
  38. </div>
  39. );
  40. }
  41. }
  42. export default ColumnCollapsable;