-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (102 loc) · 3.87 KB
/
index.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import React, {Component} from 'react';
import {Row, Col, Well} from 'react-bootstrap';
import Transition from 'react-transition-group/Transition';
import './index.css';
import quotes from '../data/quotes';
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
const duration = 400;
class ExpandableComponent extends Component {
constructor(props) {
super(props);
this.quote = null;
this.constPart = null;
this.dynamicPart = null;
this.state = {
collapsed: true,
quote: this.getRandomQuote(),
minHeight: 0,
maxHeight: 0,
};
this.toggleState = this.toggleState.bind(this);
}
componentDidMount() {
let constPartHeight = this.constPart.getBoundingClientRect().height;
let dynamicPartHeight = this.dynamicPart.getBoundingClientRect().height;
this.setState({
minHeight: constPartHeight,
maxHeight: constPartHeight + dynamicPartHeight
});
}
componentDidUpdate() {
let constPartHeight = this.constPart.getBoundingClientRect().height;
let dynamicPartHeight = this.dynamicPart.getBoundingClientRect().height;
if (this.state.minHeight !== constPartHeight ||
this.state.maxHeight !== constPartHeight + dynamicPartHeight) {
this.setState({
minHeight: constPartHeight,
maxHeight: constPartHeight + dynamicPartHeight
});
}
}
toggleState() {
let newCollapsedFlag = !this.state.collapsed,
newState = {collapsed: newCollapsedFlag};
this.setState(newState);
}
getRandomQuote() {
return quotes[getRandomInt(0, 3)];
}
render() {
let {collapsed} = this.state,
defaultStyle = {
transition: `height ${duration}ms ease-in-out`,
height: this.state.minHeight + 'px'
},
transitionStyles = {
entering: {height: this.state.minHeight + 'px'},
entered: {height: this.state.maxHeight + 'px'}
},
componentClass = "expandable-component";
if (!this.state.minHeight) {
componentClass += ' invisible';
}
return (
<div className={componentClass}>
<Transition in={!collapsed}
timeout={duration}
onExited={() => {
this.setState({quote: this.getRandomQuote()});
}}>
{(state) => (
<div style={{
...defaultStyle,
...transitionStyles[state]
}}>
<div onClick={this.toggleState}>
<div ref={(elem) => this.constPart = elem}
className="const-part">
<h1>
"The colour of the magic"
</h1>
<Row>
<Col xs={6} xsOffset={6}>
Terry Pratchett, 1983
</Col>
</Row>
</div>
<div ref={(elem) => this.dynamicPart = elem}
className="dynamic-part">
<br/>
<Well>{this.state.quote}</Well>
</div>
</div>
</div>
)}
</Transition>
</div>
);
}
}
export default ExpandableComponent;