forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanelGroup.js
More file actions
131 lines (109 loc) · 3.17 KB
/
PanelGroup.js
File metadata and controls
131 lines (109 loc) · 3.17 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import classNames from 'classnames';
import React, { cloneElement } from 'react';
import uncontrollable from 'uncontrollable';
import { bsClass, getClassSet, splitBsPropsAndOmit }
from './utils/bootstrapUtils';
import ValidComponentChildren from './utils/ValidComponentChildren';
import { generatedId } from './utils/PropTypes';
const propTypes = {
accordion: React.PropTypes.bool,
activeKey: React.PropTypes.any,
onSelect: React.PropTypes.func,
role: React.PropTypes.string,
/**
* A function that takes an eventKey and type and returns a
* unique id for each Panel heading and Panel Collapse. The function _must_ be a pure function,
* meaning it should always return the _same_ id for the same set of inputs. The default
* value requires that an `id` to be set for the PanelGroup.
*
* The `type` argument will either be `"COLLAPSE"` or `"HEADING"`.
*
* @defaultValue (eventKey, type) => `${this.props.id}-${type}-${key}`
*/
generateChildId: React.PropTypes.func,
/**
* HTML id attribute, required if no `generateChildId` prop
* is specified.
*/
id: generatedId('PanelGroup')
};
const defaultProps = {
accordion: false,
};
const childContextTypes = {
$bs_panelGroup: React.PropTypes.shape({
getId: React.PropTypes.func,
headerRole: React.PropTypes.string,
panelRole: React.PropTypes.string,
})
};
class PanelGroup extends React.Component {
constructor(props, context) {
super(props, context);
this.handleSelect = this.handleSelect.bind(this);
}
getChildContext() {
const { accordion, generateChildId, id } = this.props;
let getId = null;
if (accordion) {
getId = generateChildId
|| ((key, type) => id ? `${id}-${type}-${key}` : null);
}
return {
$bs_panelGroup: {
getId,
headerRole: 'tab',
panelRole: 'tabpanel',
},
};
}
handleSelect(key, expanded, e) {
if (expanded) {
this.props.onSelect(key, e);
}
}
render() {
const {
accordion,
activeKey,
className,
children,
...props
} = this.props;
const [bsProps, elementProps] = splitBsPropsAndOmit(props, ['onSelect']);
if (accordion) {
elementProps.role = elementProps.role || 'tablist';
}
const classes = getClassSet(bsProps);
return (
<div
{...elementProps}
className={classNames(className, classes)}
>
{ValidComponentChildren.map(children, (child, index) => {
const childProps = {
bsStyle: child.props.bsStyle || bsProps.bsStyle,
key: child.key ? child.key : index,
};
if (accordion) {
Object.assign(childProps, {
collapsible: true,
expanded: (child.props.eventKey === activeKey),
onToggle: this.handleSelect.bind(null, child.props.eventKey),
});
}
return cloneElement(child, childProps);
})}
</div>
);
}
}
PanelGroup.propTypes = propTypes;
PanelGroup.defaultProps = defaultProps;
PanelGroup.childContextTypes = childContextTypes;
export default uncontrollable(
bsClass('panel-group', PanelGroup),
{
activeKey: 'onSelect'
}
);