All files / date-picker/components/DatePicker Form.jsx

52.94% Statements 18/34
25% Branches 6/24
22.22% Functions 2/9
56.25% Lines 18/32
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238                        1x       1x           1x           1x         1x                             1x           1x           1x                                                                                                     1x                       1x   1x 1x 1x 1x 1x   1x                                                                                                                                                               1x           1x                                        
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import styled, { css } from 'styled-components';
 
import {
  Button,
  Text,
} from '@bufferapp/components';
import Calendar from './Calendar';
import Presets from './Presets';
 
const Header = styled.div`
  margin: 0.75rem 0;
`;
 
const Clear = styled.div`
  position: absolute;
  right: 8px;
  top: 6px;
`;
 
const Inputs = styled.form`
  display: flex;
  align-content: center;
  justify-content: space-between;
`;
 
const InputContainer = styled.div`
  width: 48%;
  position: relative;
`;
 
const DateInput = styled.input`
  width: 100%;
  font-size: 0.75rem;
  padding: 0.5rem;
  margin: 0;
  box-sizing: border-box;
  background: #FFFFFF;
  border: 1px solid #DEDEDE;
  border-radius: 3px;
 
  &:focus {
    outline: none;
  }
`;
 
const CustomDateRangeContainer = styled.div`
  overflow: hidden;
  max-height: 0;
  opacity: 0;
  transition: all 500ms ease-in-out;
 
  ${props => props.calendarOpen && css`
    max-height: 1000px;
    opacity: 1;
  `}
`;
 
const Footer = styled.div`
  margin: 0.75rem 0 0.5rem;
  display: flex;
  justify-content: center;
`;
 
class Form extends Component {
  componentWillUpdate (nextProps) {
    if (nextProps.startDate === null && this._startDate) {
      this._startDate.focus();
    }
  }
 
  updateSelectedDateRange() {
    const {
      startDate,
      endDate,
      // Actions
      setDateRange,
      close,
    } = this.props;
 
    if (startDate !== null && endDate !== null) {
      setDateRange(startDate, endDate);
      close();
    }
  }
 
  applyCustomDateRange() {
    const {
      startDate,
      endDate,
      // Actions
      setDateRange,
      close,
    } = this.props;
 
    if (startDate !== null && endDate !== null) {
      setDateRange(startDate, endDate);
      close();
    }
  }
 
  render () {
    // State
    const {
      presets,
      calendarOpen,
      startDate,
      endDate,
      month,
    } = this.props;
 
    // Actions
    const {
      setStartDate,
      setEndDate,
      selectPreset,
      setMonth,
      clearStartDate,
      clearEndDate,
      maxDate,
      minDate,
    } = this.props;
 
    const startDateFocus = startDate === null;
    const endDateFocus = startDate !== null && endDate === null;
    const dateFormat = 'MM/DD/YY';
    const startDateFormat = startDate ? `${moment.unix(startDate).format(dateFormat)}` : '';
    const endDateFormat = endDate ? `${moment.unix(endDate).format(dateFormat)}` : '';
 
    return (
      <div>
        <Presets
          presets={presets}
          selectPreset={selectPreset}
          minStartDate={minDate}
          startDate={startDate}
          endDate={endDate}
        />
        <CustomDateRangeContainer calendarOpen={calendarOpen}>
          { calendarOpen &&
          <div>
            <Header><Text size="small">Select a custom date range:</Text></Header>
            <Inputs>
              <InputContainer>
                <DateInput
                  innerRef={(node) => { this._startDate = node; }}
                  type="text"
                  name="start"
                  value={startDateFormat}
                  placeholder="Date from"
                  readOnly
                />
                <Clear>
                  <Button
                    noStyle
                    onClick={(e) => { e.preventDefault(); clearStartDate(); }}
                  >
                    <Text>x</Text>
                  </Button>
                </Clear>
              </InputContainer>
              <InputContainer>
                <DateInput
                  type="text"
                  name="end"
                  value={endDateFormat}
                  placeholder="Date to"
                  readOnly
                />
                <Clear>
                  <Button
                    noStyle
                    onClick={(e) => { e.preventDefault(); clearEndDate(); }}
                  >
                    <Text>x</Text>
                  </Button>
                </Clear>
              </InputContainer>
            </Inputs>
            <Calendar
              isOpen
              startDate={startDate}
              endDate={endDate}
              focusStartDate={startDateFocus}
              focusEndDate={endDateFocus}
              currentMonth={month}
 
              maxStartDate={minDate}
              maxEndDate={maxDate}
              selectStartDate={setStartDate}
              selectEndDate={setEndDate}
              selectMonth={setMonth}
            />
            <Footer>
              <Button
                disabled={(startDate === null) || (endDate === null)}
                onClick={() => this.applyCustomDateRange()}
              >
                Apply This Date Range
              </Button>
            </Footer>
          </div>
          }
        </CustomDateRangeContainer>
      </div>
    );
  }
}
 
Form.defaultProps = {
  startDate: null,
  endDate: null,
  minDate: null,
};
 
Form.propTypes = {
  startDate: PropTypes.number,
  endDate: PropTypes.number,
  month: PropTypes.number.isRequired,
  calendarOpen: PropTypes.bool.isRequired,
  minDate: PropTypes.number,
  maxDate: PropTypes.number.isRequired,
 
  // Actions
  selectPreset: PropTypes.func.isRequired,
  setStartDate: PropTypes.func.isRequired,
  clearStartDate: PropTypes.func.isRequired,
  setEndDate: PropTypes.func.isRequired,
  clearEndDate: PropTypes.func.isRequired,
  setDateRange: PropTypes.func.isRequired,
  close: PropTypes.func.isRequired,
  setMonth: PropTypes.func.isRequired,
};
 
export default Form;