All files / lib/DatePicker/DatePickerCalendar index.js

100% Statements 40/40
100% Branches 13/13
100% Functions 14/14
100% Lines 40/40
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                                                      16x 16x       35x 35x       6x         26x           2x 2x 2x   2x         2x 2x 2x   2x           51x             51x   51x 35x             51x 35x 35x 35x                   51x 35x 35x   35x                   51x 35x 35x 35x   245x 245x 245x 245x                 51x 35x                                           51x               89x           89x                                 89x                      
import React from 'react';
import PropTypes from 'prop-types';
import DatePickerMonth from '@collab-ui/react/DatePicker/DatePickerMonth';
import { Icon }  from '@collab-ui/react';
import {
  addDays,
  addMonths,
  getLocaleData,
  getStartOfWeek,
  getWeekdayMinInLocale,
  isSameDay,
  localizeDate,
  now,
  shouldNextMonthDisable,
  shouldPrevMonthDisable,
  subtractMonths,
} from '@collab-ui/react/utils/dateUtils';
import moment from 'moment';
 
class DatePickerCalendar extends React.Component {
  static displayName = 'DatePickerCalendar';
 
  state = {
    date: null,
  };
 
  componentDidMount () {
    const { focus, selected } = this.context;
    this.setDate(focus || selected || now());
  }
 
  componentDidUpdate (prevProps) {
    const { focus } = prevProps;
    if (
      focus &&
      !isSameDay(this.props.focus, focus)
    ) {
      this.setDate(focus);
    }
  }
 
  setDate = (date, cb) => {
    this.setState({
      date
    }, cb);
  };
 
  increaseMonth = event => {
    const { handleMonthChange } = this.context;
    const { date } = this.state;
    this.setDate(
      addMonths(date.clone(), 1),
      () => handleMonthChange && handleMonthChange(event, this.state.date)
    );
  };
 
  decreaseMonth = event => {
    const { handleMonthChange } = this.context;
    const { date } = this.state;
    this.setDate(
      subtractMonths(date.clone(), 1),
      () => handleMonthChange && handleMonthChange(event, this.state.date)
    );
  };
 
  render() {
 
    const { date } = this.state;
    const {
      locale,
      monthFormat,
      nextArialLabel,
      previousArialLabel,
      ...otherProps
    } = this.props;
 
    const renderMonthName = () => {
      return (
        <div className='cui-datepicker__navigation--current-month'>
          {localizeDate(date, locale).format(monthFormat)}
        </div>
      );
    };
 
    const renderPreviousMonthButton = () => {
      const { minDate } = this.props;
      const allPrevDaysDisabled = shouldPrevMonthDisable(date, minDate);
      return (
        <Icon
          ariaLabel={previousArialLabel}
          disabled={allPrevDaysDisabled}
          onClick={this.decreaseMonth}
          name='arrow-left_16'
        />
      );
    };
 
    const renderNextMonthButton = () => {
      const { maxDate } = this.props;
      const allNextDaysDisabled = shouldNextMonthDisable(date, maxDate);
 
      return (
        <Icon
          ariaLabel={nextArialLabel}
          disabled={allNextDaysDisabled}
          onClick={this.increaseMonth}
          name='arrow-right_16'
        />
      );
    };
 
    const header = () => {
      const startOfWeek = getStartOfWeek(date.clone());
      const dayNames = [];
      return dayNames.concat(
        [0, 1, 2, 3, 4, 5, 6].map(offset => {
          const day = addDays(localizeDate(startOfWeek, locale), offset);
          const localeData = getLocaleData(day);
          const weekDayName = getWeekdayMinInLocale(localeData, day);
          return (
            <div key={offset} className='cui-datepicker__day--name'>
              {weekDayName}
            </div>
          );
        })
      );
    };
 
    const renderMonth = () => {
      return (
        <div className='cui-datepicker__month-container'>
          <div className='cui-datepicker__header'>
            <div className='cui-datepicker__navigation'>
              {renderMonthName()}
              <div className='cui-datepicker__navigation--buttons'>
                {renderPreviousMonthButton()}
                {renderNextMonthButton()}
              </div>
            </div>
            <div className='cui-datepicker__day--names'>
              {header()}
            </div>
          </div>
          <DatePickerMonth
            day={date}
            {...otherProps}
          />
        </div>
      );
    };
 
    return (
      <div>
        {date && renderMonth()}
      </div>
    );
  }
}
 
DatePickerCalendar.contextTypes = {
  focus: PropTypes.instanceOf(moment),
  handleMonthChange: PropTypes.func,
  selected: PropTypes.instanceOf(moment),
};
 
DatePickerCalendar.propTypes = {
  /** @prop Set the focus on the current date being focused | null */
  focus: PropTypes.instanceOf(moment),
  /** @prop Sets the language for the DatePickerCalendar | 'en' */
  locale: PropTypes.string,
  /** @prop Sets the last date in which the calendar does not disable | null */
  maxDate: PropTypes.instanceOf(Date),
  /** @prop Sets the first date in which the calendar does not disable | null */
  minDate: PropTypes.instanceOf(Date),
  /** @prop Sets the format in how the Month is displayed | null */
  monthFormat: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
  /** @prop Text to display for blindness accessibility features | 'next' */
  nextArialLabel: PropTypes.string,
  /** @prop Text to display for blindness accessibility features | 'previous' */
  previousArialLabel: PropTypes.string,
};
 
DatePickerCalendar.defaultProps = {
  focus: null,
  locale: 'en',
  maxDate: null,
  minDate: null,
  monthFormat: 'MMMM YYYY',
  nextArialLabel: 'next',
  previousArialLabel: 'previous',
};
 
export default DatePickerCalendar;