forked from achievethecore/atc-coherence-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collapse.js
77 lines (69 loc) · 2.45 KB
/
collapse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var Component = require('react').Component;
var shallowEqual = require('react/lib/shallowEqual');
var classNames = require('classnames');
export default class Collapse extends Component {
static propTypes = {
children: React.PropTypes.oneOfType([
React.PropTypes.element,
React.PropTypes.arrayOf(React.PropTypes.element)
]),
title: React.PropTypes.string.isRequired,
onAdjustParentHeight: React.PropTypes.func,
onExpand: React.PropTypes.func,
onCollapse: React.PropTypes.func,
minHeight: React.PropTypes.number,
disabled: React.PropTypes.bool
};
shouldComponentUpdate(nextProps, nextState) {
return this.state.open != nextState.open;
return (
!shallowEqual(this.props, nextProps) ||
!shallowEqual(this.state, nextState)
);
}
state = {open: this.props.disabled};
_onClick = () => {
if(this.props.disabled) return;
if(this.props.onExpand && !this.state.open) this.props.onExpand();
if(this.props.onCollapse && this.state.open) this.props.onCollapse();
this.setState({open:!this.state.open});
window.disableScrollTop = true;
setTimeout(() => {
window.disableScrollTop = false;
}, 1000);
// if (this.props.isProgressions) {
// window.disableScrollTop = true;
// setTimeout(() => {
// window.disableScrollTop = false;
// }, 1000);
// }
};
componentWillUnmount() {
window.disableScrollTop = false;
}
componentDidUpdate() {
var domnode = React.findDOMNode(this);
var minHeight = this.props.minHeight || 60;
domnode.style.maxHeight = minHeight + 'px';
if(domnode && domnode.scrollHeight && this.state.open) {
domnode.style.maxHeight = domnode.scrollHeight + 'px';
if(this.props.onAdjustParentHeight) this.props.onAdjustParentHeight(domnode.scrollHeight - minHeight);
}
else {
if(this.props.onAdjustParentHeight) this.props.onAdjustParentHeight(-domnode.scrollHeight + minHeight);
}
}
render() {
return (
<div
style={{minHeight:this.props.minHeight||60}}
className={classNames('collapse', {'open':this.state.open, disabled:this.props.disabled})}>
<div className="collapse-header" onClick={this._onClick}>
<h3>{this.props.title}</h3>
<i className={classNames('fa', this.state.open ? 'fa-chevron-up': 'fa-chevron-down')}></i>
</div>
{this.props.children}
</div>
);
}
}