All files / src/DatePicker Calendar.js

92.59% Statements 25/27
80% Branches 8/10
83.33% Functions 10/12
92.59% Lines 25/27

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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223                                                                          54x   54x       2x                     15x 15x                           10x 3x                       18x 6x     12x   12x 1x   11x                   326x                         162x   162x                                   2x 2x     2x 2x                                       27x 27x 27x   27x                                                                           135x                                          
/** @jsx createElement */
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {DateTime, Duration} from 'luxon'
import {omit} from 'lodash'
import CalendarHeaderRow from './CalendarHeaderRow'
import CalendarWeekRow from './CalendarWeekRow'
import Card from '../Card'
import createElementWithOverride from '../Utilities/createElementWithOverride'
 
/**
 * A component representing a Calendar for a given focus month (& including the
 * leading days of the first week and trailing days of the last week).
 */
export default class Calendar extends Component {
  static propTypes = {
    selectedDate: PropTypes.string,
    focuser: PropTypes.func,
    isSelectable: PropTypes.func,
    dateChanger: PropTypes.func,
    overrides: PropTypes.object,
    week: PropTypes.any,
    overlay: PropTypes.bool,
    highlights: PropTypes.any,
    headerDay: PropTypes.any,
    day: PropTypes.any,
    nextLabel: PropTypes.node,
    previousLabel: PropTypes.node,
    className: PropTypes.string,
    getCalendarRef: PropTypes.func,
  }
 
  /**
   * The default values for props of this component
   * @return {object} the default value object
   */
  static get defaultProps() {
    const createElement = React.createElement
 
    return {
      nextLabel: <span>&rsaquo;</span>,
      previousLabel: <span>&lsaquo;</span>,
      dateChanger: () => null,
      focuser: () => null,
      isSelectable: () => true,
    }
  }
 
  /**
   * Creates a Calendar. Sets state.date to a Luxon DateTime based on the
   * selectedDate prop.
   * @param {*} props - the props
   */
  constructor(props) {
    super(props)
    this.state = {
      date: this.asLuxon(this.props.selectedDate),
    }
  }
 
  /**
   * Update state when props change. In particular, if we receive a different
   * `selectedDate` prop from up the hierarchy, set state.date to a new Luxon
   * DateTime appropriately (in order to force the calendar to focus on the new
   * date).
   * @param {*} nextProps - the next props
   * @return {void}
   */
  componentWillReceiveProps(nextProps) {
    if (nextProps.selectedDate !== this.props.selectedDate) {
      this.setState({date: this.asLuxon(nextProps.selectedDate)})
    }
  }
 
  /**
   * Convert an iso date string to a Luxon DateTime. If iso date is blank / null,
   * or invalid (e.g. 2018-06-66), return the local current date instead.
   * @param {string} date - the date to convert, as either an iso date, or a
   *   blank / null
   * @return {DateTime} - a local DateTime
   */
  asLuxon(date) {
    if (!date) {
      return DateTime.local()
    }
 
    const luxon = DateTime.fromISO(date)
 
    if (luxon.invalid) {
      return DateTime.local()
    }
    return luxon
  }
 
  /**
   * The start of the month of that the current focus date is in (e.g. if the
   * input is filled with 2018-08-05 then this value is 2018-08-1).
   * @returns {DateTime} - a Luxon DateTime represented the first day of the
   *   month in question.
   */
  startOfMonth() {
    return this.state.date.startOf('month')
  }
 
  /**
   * Returns the nearest Sunday falling on or before the start of the month.
   * This is the first day of the first row of the calendar display. Note that
   * this date is either equal to startOfMonth (if the month starts on a Sunday)
   * or in the previous month (if the month starts on a different day of the
   * week).
   * @returns {DateTime} - a Luxon DateTime representing the Sunday when the first
   *   week of the focus month starts.
   */
  startOfWeekOfStartOfMonth() {
    const weekday = this.startOfMonth().weekday % 7
 
    return this.startOfMonth().minus(Duration.fromObject({days: weekday}))
  }
 
  /**
   * Move the focus month up n months (or back |n| months if n is negative).
   * It moves it to n months from startOfMonth to handle advancing between
   * months with different numbers of days (2018-01-31 + 1 month in Luxon =
   * 2018-03-03). We actually want Jan -> Feb -> Mar which only works if we do
   * (2018-01-31).startOfMonth() + 1month = 2018-02-01.
   *
   * This function does not change the date in the input (only calendar display,
   * so we can get away with using the first of the month like this.
   * @param {int} num - the number of months to move the focus month
   * @param {Event} event - the event that caused this handler to be invoked
   *   (e.g. the click event from the next or previous button on the calendar)
   * @return {void}
   */
  addMonth(num, event) {
    event.preventDefault()
    this.setState({
      date: this.startOfMonth().plus(Duration.fromObject({month: num})),
    })
    Eif (this.props.focuser) {
      this.props.focuser()
    }
  }
 
  render() {
    const {
      className,
      week,
      overrides,
      day,
      headerDay,
      isSelectable,
      dateChanger,
      selectedDate,
      highlights,
      nextLabel,
      previousLabel,
      overlay,
      getCalendarRef,
      ...props
    } = this.props
    const createElement = createElementWithOverride.bind(this, overrides)
    const divProps = omit(props, 'focuser')
 
    return (
      <div
        {...divProps}
        className={`rev-Calendar ${
          overlay ? 'rev-Calendar--overlay' : ''
        } ${className}`}
      >
        <Card getCalendarRef={getCalendarRef}>
          <Card.Header className="rev-Calendar-header">
            <button
              onClick={this.addMonth.bind(this, -1)}
              className="rev-Calendar-header-button rev-Calendar-header-button--previous"
              aria-label="Previous Month"
            >
              {previousLabel}
            </button>
            <span className="rev-Calendar-header-label">
              {this.state.date.toLocaleString({
                month: 'short',
                year: 'numeric',
              })}
            </span>
            <button
              onClick={this.addMonth.bind(this, 1)}
              className="rev-Calendar-header-button rev-Calendar-header-button--next"
              aria-label="Next Month"
            >
              {nextLabel}
            </button>
          </Card.Header>
          <table className="rev-Calendar-body">
            <CalendarHeaderRow
              firstDay={this.startOfWeekOfStartOfMonth()}
              overrides={overrides}
              headerDay={headerDay}
            />
            <tbody>
              {[0, 7, 14, 21, 28].map((i) => {
                return (
                  <CalendarWeekRow
                    {...week}
                    day={day}
                    firstDay={this.startOfWeekOfStartOfMonth().plus({days: i})}
                    currentMonth={this.state.date.toFormat('yyyy-MM')}
                    isSelectable={isSelectable}
                    dateChanger={dateChanger}
                    selectedDate={selectedDate}
                    highlights={highlights}
                    key={i}
                  />
                )
              })}
            </tbody>
          </table>
        </Card>
      </div>
    )
  }
}