forked from WikiEducationFoundation/WikiEduDashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpandable.jsx
55 lines (46 loc) · 1.13 KB
/
expandable.jsx
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
import React from 'react';
import createReactClass from 'create-react-class';
import { connect } from 'react-redux';
import { toggleUI } from '../../actions';
const mapStateToProps = state => ({
openKey: state.ui.openKey
});
const mapDispatchToProps = {
toggleUI
};
const Expandable = function (Component) {
const wrappedComponent = createReactClass({
displayName: 'Expandable',
statics: {
getDerivedStateFromProps(props, state) {
return {
is_open: state.key === props.openKey
};
}
},
getInitialState() {
return { is_open: false };
},
componentDidMount() {
this.setState({
key: this.refs.component.getKey()
});
},
open(e) {
if (e !== null) { e.stopPropagation(); }
return this.props.toggleUI(this.refs.component.getKey());
},
render() {
return (
<Component
{...this.state} {...this.props}
open={this.open}
stop={this.stop}
ref={'component'}
/>
);
}
});
return connect(mapStateToProps, mapDispatchToProps)(wrappedComponent);
};
export default Expandable;