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.

60 lines
1.6 KiB

  1. import PureRenderMixin from 'react-addons-pure-render-mixin';
  2. import { Motion, spring } from 'react-motion';
  3. const iconStyle = {
  4. fontSize: '16px',
  5. padding: '15px',
  6. position: 'absolute',
  7. right: '0',
  8. top: '-48px',
  9. cursor: 'pointer'
  10. };
  11. const ColumnCollapsable = React.createClass({
  12. propTypes: {
  13. icon: React.PropTypes.string.isRequired,
  14. fullHeight: React.PropTypes.number.isRequired,
  15. children: React.PropTypes.node,
  16. onCollapse: React.PropTypes.func
  17. },
  18. getInitialState () {
  19. return {
  20. collapsed: true
  21. };
  22. },
  23. mixins: [PureRenderMixin],
  24. handleToggleCollapsed () {
  25. const currentState = this.state.collapsed;
  26. this.setState({ collapsed: !currentState });
  27. if (!currentState && this.props.onCollapse) {
  28. this.props.onCollapse();
  29. }
  30. },
  31. render () {
  32. const { icon, fullHeight, children } = this.props;
  33. const { collapsed } = this.state;
  34. return (
  35. <div style={{ position: 'relative' }}>
  36. <div style={{...iconStyle, color: collapsed ? '#9baec8' : '#fff', background: collapsed ? '#2f3441' : '#373b4a' }} onClick={this.handleToggleCollapsed}><i className={`fa fa-${icon}`} /></div>
  37. <Motion defaultStyle={{ opacity: 0, height: 0 }} style={{ opacity: spring(collapsed ? 0 : 100), height: spring(collapsed ? 0 : fullHeight, collapsed ? undefined : { stiffness: 150, damping: 9 }) }}>
  38. {({ opacity, height }) =>
  39. <div style={{ overflow: 'hidden', height: `${height}px`, opacity: opacity / 100 }}>
  40. {children}
  41. </div>
  42. }
  43. </Motion>
  44. </div>
  45. );
  46. }
  47. });
  48. export default ColumnCollapsable;