All files / src/TimePicker TimeContainer.js

100% Statements 28/28
88.46% Branches 23/26
100% Functions 9/9
100% Lines 28/28

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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162                              72x 8x 8x                 16x 16x                         10x 2x                       18x 12x     6x   6x 1x     5x                     72x 34x   38x     72x 72x                           9x   9x   9x 9x   9x   9x       9x 9x         34x   34x                                                                                          
/** @jsx createElement */
import React, {createElement} from 'react'
import Card from '../Card'
import TimeTicker from './TimeTicker'
import {DateTime, Duration} from 'luxon'
 
/**
 * A component containing the tickers of a time picker.
 */
export default class TimeContainer extends React.Component {
  /**
   * The default values for props of this component
   * @return {object} the default value object
   */
  static get defaultProps() {
    return {
      refocusOnClick: () => null,
      updateTime: () => null,
    }
  }
 
  /**
   * Creates a container for the time tickers. Sets state.time to the input's current value
   * @param {object} props the props
   */
  constructor(props) {
    super(props)
    this.state = {
      time: this.getLuxonDateTime(this.props.selectedTime),
    }
  }
 
  /**
   * Update state when props change. In particular, if we receive a different
   * `selectedTime` prop from up the hierarchy, set state.time to a new Luxon
   * DateTime appropriately (in order to force the tickers to the new time)
   * @param {*} nextProps the nextProps
   * @return {void}
   */
  componentWillReceiveProps(nextProps) {
    if (nextProps.selectedTime !== this.props.selectedTime) {
      this.setState({time: this.getLuxonDateTime(nextProps.selectedTime)})
    }
  }
 
  /**
   * Convert an iso time string to a Luxon DateTime. If iso time is blank / null,
   * or invalid (e.g. 11 : 17), return the local current time instead.
   * @param {string} time - the time to convert, as either an iso time, or a
   *   blank / null
   * @return {object} - the DateTime object
   */
  getLuxonDateTime(time) {
    if (!time) {
      return DateTime.local()
    }
 
    const luxon = DateTime.fromISO(time)
 
    if (luxon.invalid) {
      return DateTime.local()
    }
 
    return luxon
  }
 
  /** Get the formatted value of the given unit for the currently selected time
   * @param {string} unit - the unit to be incremented (i.e. hour or minute)
   * @returns {string} formatted value as a string
   */
  /* eslint complexity: [2, 5] */
  getFormattedUnit(unit) {
    let value
 
    if (unit === 'hour' && !this.props.use24hr) {
      value = this.state.time[unit] % 12 ? this.state.time[unit] % 12 : 12
    } else {
      value = this.state.time[unit]
    }
 
    value = (value < 10 ? '0' : '') + value
    return value
  }
 
  /**
   * Increment the inputted unit by the inputted amount, n
   *
   * This function does not change the time in the input (only container display)
   * @param {int} num - the amount to increment the unit by
   * @param {int} unit - the unit to be incremented (i.e. hour or minute)
   * @param {Event} event - the event that caused this handler to be invoked
   *   (e.g. the click event from the next or previous button on a ticker)
   * @return {void}
   */
  incrementUnit(num, unit, event) {
    event.preventDefault()
 
    const durationObject = {}
 
    durationObject[unit] = num
    const newTime = this.state.time.plus(Duration.fromObject(durationObject))
 
    this.props.updateTime(newTime.toISOTime())
 
    this.setState({
      time: newTime,
    })
 
    Eif (this.props.refocusOnClick) {
      this.props.refocusOnClick()
    }
  }
  /* eslint complexity: [2, 6] */
  render() {
    const {className, use24hr, showSeconds, overlay} = this.props
 
    return (
      <div
        className={`rev-TimeContainer ${
          overlay ? 'rev-TimeContainer--overlay' : ''
        } ${className}`}
      >
        <Card>
          <Card.Header>
            <span className="rev-TimeContainer-header">Time Picker</span>
          </Card.Header>
          <Card.Body>
            <TimeTicker
              value={this.getFormattedUnit('hour')}
              onIncrement={this.incrementUnit.bind(this, 1, 'hours')}
              onDecrement={this.incrementUnit.bind(this, -1, 'hours')}
            />
            <span className="rev-TimeTicker-divider">:</span>
            <TimeTicker
              value={this.getFormattedUnit('minute')}
              onIncrement={this.incrementUnit.bind(this, 1, 'minutes')}
              onDecrement={this.incrementUnit.bind(this, -1, 'minutes')}
            />
            {showSeconds ? (
              <span className="rev-TimeTicker-divider">:</span>
            ) : null}
            {showSeconds ? (
              <TimeTicker
                value={this.getFormattedUnit('second')}
                onIncrement={this.incrementUnit.bind(this, 1, 'seconds')}
                onDecrement={this.incrementUnit.bind(this, -1, 'seconds')}
              />
            ) : null}
            {use24hr ? null : (
              <TimeTicker
                value={this.state.time.hour >= 12 ? 'PM' : 'AM'}
                onIncrement={this.incrementUnit.bind(this, 12, 'hours')}
                onDecrement={this.incrementUnit.bind(this, -12, 'hours')}
              />
            )}
          </Card.Body>
        </Card>
      </div>
    )
  }
}