forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanelToggle.js
More file actions
68 lines (52 loc) · 1.52 KB
/
PanelToggle.js
File metadata and controls
68 lines (52 loc) · 1.52 KB
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
import React from 'react';
import classNames from 'classnames';
import elementType from 'react-prop-types/lib/elementType';
import SafeAnchor from './SafeAnchor';
import createChainedFunction from './utils/createChainedFunction';
const propTypes = {
onClick: React.PropTypes.func,
/**
* You can use a custom element for this component
*/
componentClass: elementType,
};
const defaultProps = {
componentClass: SafeAnchor,
};
const contextTypes = {
$bs_panel: React.PropTypes.shape({
getIds: React.PropTypes.func,
onToggle: React.PropTypes.func,
expanded: React.PropTypes.bool,
})
};
class PanelToggle extends React.Component {
constructor(...args) {
super(...args);
this.handleToggle = this.handleToggle.bind(this);
}
handleToggle(event) {
const { onToggle } = this.context.$bs_panel || {};
if (onToggle) {
onToggle(event);
}
}
render() {
const { onClick, className, componentClass, ...props } = this.props;
const { expanded, getIds } = this.context.$bs_panel || {};
const Component = componentClass;
props.onClick = createChainedFunction(onClick, this.handleToggle);
props['aria-expanded'] = expanded;
props.className = classNames(className, !expanded && 'collapsed');
if (getIds) {
props['aria-controls'] = getIds().collapseId;
}
return (
<Component {...props} />
);
}
}
PanelToggle.propTypes = propTypes;
PanelToggle.defaultProps = defaultProps;
PanelToggle.contextTypes = contextTypes;
export default PanelToggle;