All files / components Section.js

100% Statements 10/10
100% Branches 12/12
100% Functions 4/4
100% Lines 10/10
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          4x 4x         5x           9x 5x 5x   5x             9x                   1x                           1x        
import React from 'react'
import Base from './Base'
 
export default class Section extends Base {
  constructor(props) {
    super(props)
    this.state = {
      classes: [typeof props.title === 'string' && props.title || '']
    }
  }
  componentWillReceiveProps(props) {
    this.setState({
      classes: [typeof props.title === 'string' && props.title || '']
    })
  }
 
  render() {
    const headers = this.props.headers.map((header, index) => {
      const title = typeof header === 'string' ? header : ''
      const classes = [title, 'heading']
 
      return (
        <div key={index} className={classes.join(' ')}>
          {header}
        </div>
      )
    })
 
    return (
      <div className={this.classString}>
        {this.props.title && <div className="title">{this.props.title}</div>}
        {headers.length !== 0 && <div className="header">{headers}</div>}
        {this.props.children}
      </div>
    )
  }
}
 
Section.propTypes = {
  className: React.PropTypes.oneOfType([
    React.PropTypes.string,
    React.PropTypes.arrayOf(React.PropTypes.string),
  ]),
  headers: React.PropTypes.arrayOf(React.PropTypes.oneOfType([
    React.PropTypes.string,
    React.PropTypes.element,
  ])).isRequired,
  title: React.PropTypes.oneOfType([
    React.PropTypes.string,
    React.PropTypes.array,
  ]),
}
Section.defaultProps = {
  headers: [],
  title: '',
}