# All files / components/Calendar MonthNavigation.js

84.62% Statements 11/13
100% Branches 0/0
50% Functions 2/4
84.62% Lines 11/13

    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

    6x

    6x

    6x

    6x

    6x
    12x

    12x
    12x
    12x
    12x
    12x

     import React from 'react';
    import { MONTHS } from '../../extensions/dateUtils';
    import PropTypes from 'prop-types';
    import { Icon } from '..';
    import styled from 'styled-components';

    const OuterContainer = styled.div`
      height: 50px;
      padding: 8px;
      line-height: 34px;
      width: 100%;
      display: flex;
      justify-content: space-between;
      border: 1px solid #E1E1E1;
      background-color: #fff;
    `;

    const MonthHeader = styled.div`
      display: flex;
      justify-content: space-between;
      width: 100%;
      user-select: none;
    `;

    const NavIcon = styled(Icon)`
      height: 100%;
      vertical-align: middle;
      float: left;
      cursor: pointer;
    `;

    const CapsSpan = styled.span`
      text-transform: capitalize;
      font-weight: 600;
      font-size: 14px;
      color: #585858;
    `;

    const addMonthsToDate = (date, monthsToAdd) => (
      new Date(date.getFullYear(), date.getMonth() + monthsToAdd, date.getDate())
    );

    export default class MonthNavigation extends React.Component {
      static propTypes = {
        displayedMonth: PropTypes.instanceOf(Date).isRequired,
        handleChange: PropTypes.func.isRequired,
      };

      navigateBackwards = () => {
        this.props.handleChange(addMonthsToDate(this.props.displayedMonth, -1));
      };

      navigateForwards = () => {
        this.props.handleChange(addMonthsToDate(this.props.displayedMonth, 1));
      }

      render() {
        const { displayedMonth } = this.props;
        const nextMonth = addMonthsToDate(displayedMonth, 1);
        const firstMonthLabel = `${MONTHS[displayedMonth.getMonth()]}  ${displayedMonth.getFullYear()}`;
        const nextMonthLabel = `${MONTHS[nextMonth.getMonth()]}  ${nextMonth.getFullYear()}`;
        return (
          <OuterContainer>
            <MonthHeader>
              <NavIcon name="back" onClick={this.navigateBackwards} />
              <CapsSpan>{firstMonthLabel}</CapsSpan>
              <CapsSpan>{nextMonthLabel}</CapsSpan>
              <NavIcon name="forward" onClick={this.navigateForwards} />
            </MonthHeader>
          </OuterContainer>
        );
      }
    }