# All files / components/Calendar MonthBlock.js

97.83% Statements 45/46
92.59% Branches 25/27
92.31% Functions 12/13
97.67% Lines 42/43

    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

    6x

    6x

    6x

    6x

    6x

    732x

    732x
    732x

    732x

    6x
    18x

    48x

    6x
    24x

    6x
    24x

    732x
    732x
    366x

    24x
    24x
    24x
    24x
    24x
    732x
    732x

    24x
    732x
    18x
    18x

    732x
    732x
    732x

    732x
    108x
    108x
    108x

    624x

    24x
    24x

    24x
    24x

     import React from 'react';
    import styled from 'styled-components';
    import PropTypes from 'prop-types';

    const OuterContainer = styled.div`
      display: inline-block;
      width: 50%;
      font-weight: 600;
      text-transform: uppercase;
      padding: 3px;
    `;

    const DaysHeader = styled.div`
      display: flex;
      margin-top: 12px;
      width: 100%;
    `;

    const DayHeader = styled.div`
      text-align: center;
      width: 14.29%;
      font-weight: 600;
      color: #585858;
      font-size: 14px;
    `;

    const BlockRow = styled.div`
      display: flex;
    `;

    const Block = styled.div`
      padding: 5px 2px 2px 2px;
      border: 1px solid #DDD;
      color: ${props => (props.selected) ? '#fff' : '#2D3B4D'};
      overflow: hidden;
      cursor: pointer;
      display: inline-block;
      width: 14.29%;
      text-align: end;
      font-weight: ${props => (props.selected) ? '600' : '300'};
      background-color: ${props => (props.selected) ? '#2D3B4D' : '#fff'};

      &:hover {
        background-color: ${props => (props.selected) ? '#2D384D' : '#D9F5FC'};
      }
    `;

    const Spacer = styled.div`
      width: ${props => (props.blockWidth * 100) / 7}%;
    `;

    const isValidDate = date => (date && date instanceof Date && !isNaN(date.getTime()));

    const getStartDate = month => (
      isValidDate(month) ? new Date(month.getFullYear(), month.getMonth(), 1) : getStartDate(new Date())
    );

    const getEndDate = month => (
      isValidDate(month) ? new Date(month.getFullYear(), month.getMonth() + 1, 0) : getEndDate(new Date())
    );

    export default class MonthBlock extends React.Component {
      static propTypes = {
        month: PropTypes.instanceOf(Date).isRequired,
        startDate: PropTypes.instanceOf(Date).isRequired,
        endDate: PropTypes.instanceOf(Date),
        handleClick: PropTypes.func.isRequired,
      };

      static defaultProps = {
        endDate: undefined,
      }

      isDateInRange = date => {
        const { endDate } = this.props;
        if (!endDate) return (date.getTime() === this.props.startDate.getTime());
        return (date.getTime() >= this.props.startDate.getTime() && date.getTime() <= this.props.endDate.getTime());
      }

      renderBlocks = (startDate, endDate) => {
        let lastDay = 0;
        let dayOfWeek = 0;
        const rows = [];
        let blocks = [];
        const onClick = day => {
          const clickedDate = new Date(startDate.getFullYear(), startDate.getMonth(), day);
          return () => {
            this.props.handleClick(clickedDate);
          };
        };

        while (lastDay < endDate.getDate()) {
          if (lastDay === 0 && startDate.getDay() !== 0) {
            blocks.push(<Spacer key={'spacer'} blockWidth={startDate.getDay()} />);
            dayOfWeek = startDate.getDay();
          }
          lastDay++;
          const selected = this.isDateInRange(new Date(startDate.getFullYear(), startDate.getMonth(), lastDay));
          blocks.push(
            <Block
              key={lastDay}
              selected={selected}
              onClick={onClick(lastDay)}
            >
              {lastDay}
            </Block>,
          );

          if (dayOfWeek === 6) {
            dayOfWeek = 0;
            rows.push(<BlockRow key={lastDay}>{blocks}</BlockRow>);
            blocks = [];
          } else {
            dayOfWeek++;
          }
        }

        if (dayOfWeek !== 0) rows.push(<BlockRow key={lastDay}>{blocks}</BlockRow>);
        return rows;
      }

      render() {
        const { month } = this.props;
        return (
          <OuterContainer>
            <DaysHeader>
              <DayHeader>S</DayHeader>
              <DayHeader>M</DayHeader>
              <DayHeader>T</DayHeader>
              <DayHeader>W</DayHeader>
              <DayHeader>T</DayHeader>
              <DayHeader>F</DayHeader>
              <DayHeader>S</DayHeader>
            </DaysHeader>
            {this.renderBlocks(getStartDate(month), getEndDate(month))}
          </OuterContainer>
        );
      }
    }