The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

58 lines
1.7 KiB

  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. constructor (props, context) {
  6. super(props, context);
  7. this.state = {
  8. collapsed: true
  9. };
  10. this.handleToggleCollapsed = this.handleToggleCollapsed.bind(this);
  11. }
  12. handleToggleCollapsed () {
  13. const currentState = this.state.collapsed;
  14. this.setState({ collapsed: !currentState });
  15. if (!currentState && this.props.onCollapse) {
  16. this.props.onCollapse();
  17. }
  18. }
  19. render () {
  20. const { icon, title, fullHeight, children } = this.props;
  21. const { collapsed } = this.state;
  22. const collapsedClassName = collapsed ? 'collapsable-collapsed' : 'collapsable';
  23. return (
  24. <div className='column-collapsable'>
  25. <div role='button' tabIndex='0' title={`${title}`} className={`column-icon ${collapsedClassName}`} onClick={this.handleToggleCollapsed}>
  26. <i className={`fa fa-${icon}`} />
  27. </div>
  28. <Motion defaultStyle={{ opacity: 0, height: 0 }} style={{ opacity: spring(collapsed ? 0 : 100), height: spring(collapsed ? 0 : fullHeight, collapsed ? undefined : { stiffness: 150, damping: 9 }) }}>
  29. {({ opacity, height }) =>
  30. <div style={{ overflow: height === fullHeight ? 'auto' : 'hidden', height: `${height}px`, opacity: opacity / 100, maxHeight: '70vh' }}>
  31. {children}
  32. </div>
  33. }
  34. </Motion>
  35. </div>
  36. );
  37. }
  38. }
  39. ColumnCollapsable.propTypes = {
  40. icon: PropTypes.string.isRequired,
  41. title: PropTypes.string,
  42. fullHeight: PropTypes.number.isRequired,
  43. children: PropTypes.node,
  44. onCollapse: PropTypes.func
  45. };
  46. export default ColumnCollapsable;