react-bootstrap
Version:
Bootstrap 3 components build with React
82 lines (66 loc) • 1.81 kB
JavaScript
"use strict";
/** @jsx React.DOM */
var React = require("./react-es6")["default"];
var classSet = require("./react-es6/lib/cx")["default"];
var BootstrapMixin = require("./BootstrapMixin")["default"];
var utils = require("./utils")["default"];
var PanelGroup = React.createClass({displayName: 'PanelGroup',
mixins: [BootstrapMixin],
propTypes: {
onSelect: React.PropTypes.func
},
getDefaultProps: function () {
return {
bsClass: 'panel-group'
};
},
getInitialState: function () {
var defaultActiveKey = this.props.defaultActiveKey;
return {
activeKey: defaultActiveKey
};
},
render: function () {
return this.transferPropsTo(
React.DOM.div( {className:classSet(this.getBsClassSet())},
utils.modifyChildren(this.props.children, this.renderPanel)
)
);
},
renderPanel: function (child) {
var activeKey =
this.props.activeKey != null ? this.props.activeKey : this.state.activeKey;
var props = {
bsStyle: this.props.bsStyle,
key: child.props.key,
ref: child.props.ref
};
if (this.props.isAccordion) {
props.isCollapsable = true;
props.isOpen = (child.props.key === activeKey);
props.onSelect = this.handleSelect;
}
return utils.cloneWithProps(
child,
props
);
},
shouldComponentUpdate: function() {
// Defer any updates to this component during the `onSelect` handler.
return !this._isChanging;
},
handleSelect: function (key) {
if (this.props.onSelect) {
this._isChanging = true;
this.props.onSelect(key);
this._isChanging = false;
}
if (this.state.activeKey === key) {
key = null;
}
this.setState({
activeKey: key
});
}
});
exports["default"] = PanelGroup;