# All files / components/DateTimePicker DateTimePicker.js

0% Statements 0/58
0% Branches 0/10
0% Functions 0/10
0% Lines 0/22

    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

     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/DateTimePickerContainer';
    import DateTimeRangePicker from './DateTimeRangePicker';
    import moment from 'moment';
    import { noop } from 'lodash';

    /**
     * **DateTimePicker** combines the functionality of DatePicker and TimePicker
     * into a single interface. It passes all of its props into DatePicker.
     */
    class DateTimePicker extends React.PureComponent {
      static propTypes = {
        /**
         * A function to handle the datetime when it changes.
         */
        onChange: PropTypes.func.isRequired,
        /**
         * Moment date time format to display in the input
         */
        displayFormat: PropTypes.string,
        /**
         * Use UTC datetime.
         */
        utc: PropTypes.bool,
        /**
         * The current datetime being displayed. If unset, the component will track its own internal state.
         * Datetime should be provided as 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.
         */
        disabled: PropTypes.bool,
      };

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

      state = {
        internalValue: undefined,
      };

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

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

      get momentValue() {
        return this.moment(this.value);
      }

      handleTimeChange = time => {
        // Set time on existing datetime
        const newDatetime = this.momentValue
          .hours(time.hours())
          .minutes(time.minutes());
        this.props.onChange(newDatetime);
        this.setState({ internalValue: newDatetime });
      };

      handleDateChange = date => {
        // Set time from existing datetime on incoming date, since it's easier to set hours/minutes than year/month/day
        const newDatetime = this.moment(date)
          .hours(this.momentValue.hours())
          .minutes(this.momentValue.minutes());
        this.props.onChange(newDatetime);
        this.setState({ internalValue: newDatetime });
      };

      render() {
        const { onChange, ...rest } = this.props;
        return (
          <DatePicker
            date={this.momentValue}
            onDateChange={this.handleDateChange}
            calendarInfoPosition="top"
            renderCalendarInfo={() => (
              <TimePickerContainer>
                <TimePicker
                  name="time-picker"
                  value={this.momentValue}
                  onChange={this.handleTimeChange}
                />
              </TimePickerContainer>
            )}
            {...rest}
          />
        );
      }
    }

    DateTimePicker.Range = DateTimeRangePicker;

    export default DateTimePicker;