glitchier-soc/app/assets/javascripts/components/components/column_collapsable.jsx

64 lines
1.8 KiB
React
Raw Normal View History

2017-01-10 18:25:10 +02:00
import PureRenderMixin from 'react-addons-pure-render-mixin';
import { Motion, spring } from 'react-motion';
const iconStyle = {
fontSize: '16px',
padding: '15px',
position: 'absolute',
right: '0',
top: '-48px',
2017-02-23 03:14:35 +02:00
cursor: 'pointer',
zIndex: '3'
2017-01-10 18:25:10 +02:00
};
const ColumnCollapsable = React.createClass({
propTypes: {
icon: React.PropTypes.string.isRequired,
title: React.PropTypes.string,
2017-01-10 18:25:10 +02:00
fullHeight: React.PropTypes.number.isRequired,
children: React.PropTypes.node,
onCollapse: React.PropTypes.func
},
getInitialState () {
return {
collapsed: true
};
},
mixins: [PureRenderMixin],
handleToggleCollapsed () {
const currentState = this.state.collapsed;
this.setState({ collapsed: !currentState });
if (!currentState && this.props.onCollapse) {
this.props.onCollapse();
}
},
render () {
const { icon, title, fullHeight, children } = this.props;
2017-01-10 18:25:10 +02:00
const { collapsed } = this.state;
2017-02-10 17:35:19 +02:00
const collapsedClassName = collapsed ? 'collapsable-collapsed' : 'collapsable';
2017-02-23 03:14:35 +02:00
2017-01-10 18:25:10 +02:00
return (
<div style={{ position: 'relative' }}>
<div title={`${title}`} style={{...iconStyle }} className={`column-icon ${collapsedClassName}`} onClick={this.handleToggleCollapsed}><i className={`fa fa-${icon}`} /></div>
2017-01-10 18:25:10 +02:00
<Motion defaultStyle={{ opacity: 0, height: 0 }} style={{ opacity: spring(collapsed ? 0 : 100), height: spring(collapsed ? 0 : fullHeight, collapsed ? undefined : { stiffness: 150, damping: 9 }) }}>
2017-01-10 18:25:10 +02:00
{({ opacity, height }) =>
<div style={{ overflow: height === fullHeight ? 'auto' : 'hidden', height: `${height}px`, opacity: opacity / 100, maxHeight: '70vh' }}>
2017-01-10 18:25:10 +02:00
{children}
</div>
}
</Motion>
</div>
);
}
});
export default ColumnCollapsable;