# All files / components/DateTimePicker DateTimeRangePicker.js

0% Statements 0/76
0% Branches 0/34
0% Functions 0/17
0% Lines 0/35

    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

     import React from 'react';
    import PropTypes from 'prop-types';
    import DatePicker from 'components/DatePicker/DatePicker';
    import TimePicker from 'components/TimePicker/TimePicker';
    import TimePickerContainer from './styles/DateTimeRangePickerContainer';
    import moment from 'moment';
    import { noop } from 'lodash';

    /**
     * **DateTimeRangePicker** combines the functionality of DatePicker.Range and TimePicker
     * into a single interface. It passes all of its props into DatePicker.Range.
     * @visibleName DateTimePicker.Range
     */
    class DateTimeRangePicker extends React.PureComponent {
      static propTypes = {
        /**
         * Callback invoked when the datetime is changed. Includes an object with the signature `{startDatetime, endDatetime}`.
         * The values are provided as moment objects.
         */
        onChange: PropTypes.func.isRequired,
        /**
         * Moment date time format to display in the input
         */
        displayFormat: PropTypes.string,
        /**
         * Moment date time format to display in the time picker
         */
        timeDisplayFormat: PropTypes.string,
        /**
         * Use UTC datetime.
         */
        utc: PropTypes.bool,
        /**
         * Text to use when the start date is disabled.
         */
        disabledStartText: PropTypes.string,
        /**
         * Text to use when the end date is disabled.
         */
        disabledEndText: PropTypes.string,
        /**
         * The current datetimes being displayed. If unset, the component will track its own internal state.
         * Datetime should be provided as an object `{start: <moment>, end: <moment>}`, where <moment>
         * is a moment object.
         */
        value: PropTypes.object,
        /**
         * Set to true if the component's value is invalid
         */
        invalid: PropTypes.bool,
        /**
         * Set to true if the component should be disabled. Alternatively, set to either 'startDate' or 'endDate'
         * to disable a particular part of the component.
         */
        disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
      };

      static defaultProps = {
        onChange: noop,
        displayFormat: 'MMM DD YYYY [at] hh:mm A',
        timeDisplayFormat: 'hh:mm',
        utc: false,
        disabledStartText: 'Now',
        disabledEndText: 'Forever',
        value: undefined,
        invalid: false,
        disabled: false,
      };

      state = {
        internalValue: { start: undefined, end: undefined },
      };

      startDateDisabled = () => [true, 'startDate'].includes(this.props.disabled);

      endDateDisabled = () => [true, 'endDate'].includes(this.props.disabled);

      get moment() {
        return this.props.utc ? moment.utc : moment;
      }

      get value() {
        return this.props.value ? this.props.value : this.state.internalValue;
      }

      get startValue() {
        return this.value && this.value.start;
      }

      get endValue() {
        return this.value && this.value.end;
      }

      get momentStartValue() {
        return this.moment(this.startValue);
      }

      get momentEndValue() {
        return this.moment(this.endValue);
      }

      // Trigger onChange prop
      triggerOnChange = ({ start, end }) =>
        this.props.onChange({
          start: this.startDateDisabled() ? null : start,
          end: this.endDateDisabled() ? null : end,
        });

      handleStartTimeChange = time => {
        const start = this.momentStartValue
          .hours(time.hours())
          .minutes(time.minutes());
        const newState = {
          start,
          end: this.momentEndValue,
        };
        this.triggerOnChange(newState);
        this.setState({ internalValue: newState });
      };

      handleEndTimeChange = time => {
        const end = this.momentEndValue.hours(time.hours()).minutes(time.minutes());
        const newState = {
          start: this.momentStartValue,
          end,
        };
        this.triggerOnChange(newState);
        this.setState({ internalValue: newState });
      };

      handleDatesChange = ({ startDate, endDate }) => {
        const newState = { start: this.momentStartValue, end: this.momentEndValue };
        if (startDate) {
          newState.start = this.moment(startDate)
            .hours(this.momentStartValue.hours())
            .minutes(this.momentStartValue.minutes());
        }
        if (endDate) {
          newState.end = this.moment(endDate)
            .hours(this.momentEndValue.hours())
            .minutes(this.momentEndValue.minutes());
        }
        this.triggerOnChange(newState);
        this.setState({ internalValue: newState });
      };

      render() {
        const {
          onChange,
          disabledStartText,
          disabledEndText,
          ...rest
        } = this.props;

        return (
          <DatePicker.Range
            startDatePlaceholderText={
              this.startDateDisabled() ? disabledStartText : null
            }
            endDatePlaceholderText={this.endDateDisabled() ? disabledEndText : null}
            startDate={this.startDateDisabled() ? null : this.momentStartValue}
            endDate={this.endDateDisabled() ? null : this.momentEndValue}
            onDatesChange={this.handleDatesChange}
            calendarInfoPosition="top"
            renderCalendarInfo={() => (
              <TimePickerContainer>
                {this.startDateDisabled() || (
                  <TimePicker
                    name="start-time-picker"
                    timeFormat={this.props.timeDisplayFormat}
                    value={this.momentStartValue}
                    onChange={this.handleStartTimeChange}
                  />
                )}
                {this.endDateDisabled() || (
                  <TimePicker
                    name="end-time-picker"
                    timeFormat={this.props.timeDisplayFormat}
                    value={this.momentEndValue}
                    onChange={this.handleEndTimeChange}
                  />
                )}
              </TimePickerContainer>
            )}
            {...rest}
          />
        );
      }
    }

    export default DateTimeRangePicker;