All files / datepicker month.js

100% Statements 17/17
80% Branches 4/5
50% Functions 3/6
100% Lines 17/17

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                                          5x                                           47x 47x 47x       8x 8x       8x 8x   8x 47x                                                 47x 47x   47x   8x       8x 8x 8x              
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import * as React from 'react';
import Week from './week.js';
import {StyledMonth} from './styled-components.js';
import {
  addDays,
  isSameMonth,
  getStartOfWeek,
  getStartOfMonth,
  getMonth,
  addWeeks,
} from './utils/index.js';
import {getOverrides} from '../helpers/overrides.js';
import type {MonthPropsT} from './types.js';
 
const defaultProps = {
  date: new Date(),
  excludeDates: null,
  filterDate: null,
  highlightDates: null,
  includeDates: null,
  locale: null,
  maxDate: null,
  minDate: null,
  month: null,
  onDayClick: () => {},
  onDayMouseOver: () => {},
  onDayMouseLeave: () => {},
  overrides: {},
  peekNextMonth: false,
  value: null,
};
 
export default class CalendarMonth extends React.Component<MonthPropsT> {
  static defaultProps = defaultProps;
 
  isWeekInMonth = (startOfWeek: Date) => {
    const date = this.props.date;
    const endOfWeek = addDays(startOfWeek, 6);
    return isSameMonth(startOfWeek, date) || isSameMonth(endOfWeek, date);
  };
 
  renderWeeks = () => {
    const weeks = [];
    let currentWeekStart = getStartOfWeek(
      getStartOfMonth(this.props.date),
      this.props.locale,
    );
    let i = 0;
    let isWithinMonth = true;
 
    while (isWithinMonth) {
      weeks.push(
        <Week
          date={currentWeekStart}
          excludeDates={this.props.excludeDates}
          filterDate={this.props.filterDate}
          highlightedDate={this.props.highlightedDate}
          includeDates={this.props.includeDates}
          focusedCalendar={this.props.focusedCalendar}
          range={this.props.range}
          key={i}
          locale={this.props.locale}
          minDate={this.props.minDate}
          maxDate={this.props.maxDate}
          month={getMonth(this.props.date)}
          onDayBlur={this.props.onDayBlur}
          onDayFocus={this.props.onDayFocus}
          onDayClick={this.props.onDayClick}
          onDayMouseOver={this.props.onDayMouseOver}
          onDayMouseLeave={this.props.onDayMouseLeave}
          onChange={this.props.onChange}
          overrides={this.props.overrides}
          peekNextMonth={this.props.peekNextMonth}
          value={this.props.value}
        />,
      );
      i++;
      currentWeekStart = addWeeks(currentWeekStart, 1);
      // It will break on the next week if the week is out of the month
      isWithinMonth = this.isWeekInMonth(currentWeekStart);
    }
    return weeks;
  };
 
  render() {
    const {overrides = {}} = this.props;
    const [Month, monthProps] = getOverrides(overrides.Month, StyledMonth);
    return (
      <Month aria-multiselectable={this.props.range || null} {...monthProps}>
        {this.renderWeeks()}
      </Month>
    );
  }
}