# All files / components/TimePicker TimePicker.js

0% Statements 0/65
0% Branches 0/18
0% Functions 0/12
0% Lines 0/28

    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

     import React from 'react';
    import styled from 'styled-components';
    import PropTypes from 'prop-types';
    import Input from 'components/Input';
    import RadioGroup from 'components/RadioGroup';
    const RadioButton = RadioGroup.Button;
    import moment from 'moment';
    import TimeContainer from './styles/TimeContainer';
    import TimeRadioContainer from './styles/TimeRadioContainer';
    import InternalTimePicker from './InternalTimePicker';

    class TimePicker extends React.PureComponent {
      static propTypes = {
        /**
         * The current time value of the widget. The value should be a moment object or a unix time.
         * If value is not passed, the component will control its own time.
         */
        value: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
        /**
         * Time format to pass to moment to format the string. Probably hh:mm or HH:mm for 24 hour time
         */
        timeFormat: PropTypes.string,
        /**
         * Should the time input be disabled?
         */
        disabled: PropTypes.bool,
      };

      static defaultProps = {
        value: undefined,
        timeFormat: 'hh:mm',
        disabled: false,
      };

      state = {
        internalValue: undefined,
      };

      get twentyFourHour() {
        return /[Hk]/g.test(this.props.timeFormat);
      }

      handleOnChange = ({ value, amSelected }) => {
        if (!value) {
          return;
        }
        const timeValue = moment(value, this.props.timeFormat);
        const timeValueIsAm = timeValue.hours() < 12;
        let sign = 0;
        if (!this.twentyFourHour && amSelected !== timeValueIsAm)
          sign = amSelected ? -1 : 1;
        const newValue = timeValue.add(sign * 12, 'hours');
        this.setState({ internalValue: newValue });
        this.props.onChange(newValue);
      };

      handleTimeChange = date =>
        this.handleOnChange({ value: date, amSelected: this.isAm });

      handleAmPm = amSelected => ev =>
        this.handleOnChange({ value: this.momentValue, amSelected });

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

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

      get isAm() {
        return this.momentValue.hours() <= 11;
      }

      render() {
        const { name, disabled } = this.props;
        return (
          <TimeContainer>
            <InternalTimePicker
              disabled={disabled}
              onChange={this.handleTimeChange}
              value={this.momentValue.format(this.props.timeFormat)}
            />
            {!this.twentyFourHour && (
              <TimeRadioContainer>
                <RadioButton.Small
                  onChange={this.handleAmPm(true)}
                  checked={this.isAm}
                  name={`${name}-ampm`}
                  value="am"
                  label="AM"
                />
                <RadioButton.Small
                  onChange={this.handleAmPm(false)}
                  checked={!this.isAm}
                  name={`${name}-ampm`}
                  value="pm"
                  label="PM"
                />
              </TimeRadioContainer>
            )}
          </TimeContainer>
        );
      }
    }

    export default TimePicker;