All files / src ExpandingCol.js

100% Statements 15/15
100% Branches 4/4
100% Functions 5/5
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                    2x 2x           1x       1x             4x   4x                                                                   5x 5x   5x                       1x 1x 1x 1x 1x 1x  
import React, {cloneElement} from 'react'
import PropTypes from 'prop-types'
import {Col} from './grid'
import Button from './Button'
 
// An expanding column that manages its own state. This is to be used in
// non-Redux applications where the state is not managed by another component
// If you are using Redux, or if a different component controls whether the
// column is open or not, use an ExpandingCol instead
export class ExpandingColStateContainer extends React.Component {
  static propTypes = {
    children: PropTypes.node,
    className: PropTypes.string,
    closer: PropTypes.element,
    expander: PropTypes.element,
  }
 
  static defaultProps = {
    // intentionally blank to override the 'expanderless' class of the stateless
    // version of the component
    className: '',
    closer: (
      <Button className="rev-ExpandingCol-closer rev-Button rev-Button--small rev-Button--secondary">
        -
      </Button>
    ),
    expander: (
      <Button className="rev-ExpandingCol-expander rev-Button rev-Button--small rev-Button--secondary">
        +
      </Button>
    ),
  }
 
  constructor(props) {
    super(props)
    this.state = {
      open: false,
    }
  }
 
  expandPane = () => {
    this.setState({open: true})
  }
 
  closePane = () => {
    this.setState({open: false})
  }
 
  // Feeds open state, closer and opener down to an ExpandingCol. Note that
  // we clone the closer and expander so that we can accept them as props
  // from up the tree but still inject our onClick handlers for open/close
  render() {
    const {children, closer, expander, ...remainingProps} = this.props
 
    return (
      <ExpandingCol
        open={this.state.open}
        closer={cloneElement(closer, {onClick: this.closePane})}
        expander={cloneElement(expander, {onClick: this.expandPane})}
        {...remainingProps}
      >
        {children}
      </ExpandingCol>
    )
  }
}
 
export default class ExpandingCol extends React.Component {
  static defaultProps = {
    className: 'rev-ExpandingCol--expanderless',
  }
 
  static propTypes = {
    children: PropTypes.node,
    className: PropTypes.string,
    closer: PropTypes.node,
    expander: PropTypes.node,
    open: PropTypes.bool,
  }
 
  render() {
    const {
      children,
      closer,
      expander,
      open,
      className,
      ...remainingProps
    } = this.props
    const openClass = open ? 'is-open' : 'is-closed'
 
    return (
      <Col
        {...remainingProps}
        className={`rev-ExpandingCol-pane ${className} ${openClass}`}
      >
        {open ? closer : expander}
        <div className="rev-ExpandingCol-pane-content">{children}</div>
      </Col>
    )
  }
}
 
ExpandingCol.ExpandingCol = ExpandingCol
ExpandingCol.Stateful = ExpandingColStateContainer
ExpandingCol.ExpandingColStateContainer = ExpandingColStateContainer
exports.ExpandingCol = ExpandingCol
exports.Stateful = ExpandingColStateContainer
exports.ExpandingColStateContainer = ExpandingColStateContainer