all files / src/explore/components/ ControlPanelSection.jsx

95% Statements 19/20
70% Branches 7/10
75% Functions 3/4
94.74% Lines 18/19
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 PropTypes from 'prop-types';
import { Panel } from 'react-bootstrap';
import InfoTooltipWithTrigger from '../../components/InfoTooltipWithTrigger';
 
const propTypes = {
  label: PropTypes.string,
  description: PropTypes.string,
  children: PropTypes.node.isRequired,
  startExpanded: PropTypes.bool,
  hasErrors: PropTypes.bool,
};
 
const defaultProps = {
  label: null,
  description: null,
  startExpanded: false,
  hasErrors: false,
};
 
export default class ControlPanelSection extends React.Component {
  constructor(props) {
    super(props);
    this.state = { expanded: this.props.startExpanded };
  }
  toggleExpand() {
    this.setState({ expanded: !this.state.expanded });
  }
  renderHeader() {
    const { label, description, hasErrors } = this.props;
    return (
      label &&
        <div>
          <i
            className={`text-primary expander fa fa-caret-${this.state.expanded ? 'down' : 'right'}`}
            onClick={this.toggleExpand.bind(this)}
          />
          {' '}
          <span onClick={this.toggleExpand.bind(this)}>{label}</span>
          {' '}
          {description && <InfoTooltipWithTrigger label={label} tooltip={description} />}
          {hasErrors &&
            <InfoTooltipWithTrigger
              label="validation-errors"
              bsStyle="danger"
              tooltip="This section contains validation errors"
            />
          }
        </div>);
  }
 
  render() {
    return (
      <Panel
        className="control-panel-section"
        collapsible
        expanded={this.state.expanded}
        header={this.renderHeader()}
      >
        {this.props.children}
      </Panel>
    );
  }
}
 
ControlPanelSection.propTypes = propTypes;
ControlPanelSection.defaultProps = defaultProps;