# All files / components/DatePicker DatePicker.js

0% Statements 0/53
0% Branches 0/8
0% Functions 0/5
0% Lines 0/16

    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

     import React from 'react';
    import PropTypes from 'prop-types';
    import 'react-dates/initialize';
    import {
      SingleDatePicker as LibDatePicker,
      SingleDatePickerShape,
    } from 'react-dates';
    import Icon from '../Icon';
    import DatePickerWrapper from './styles/DatePickerWrapper';

    import DateRangePicker from './DateRangePicker';
    import moment from 'moment';
    moment.updateLocale('en', {
      weekdaysMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
    });
    import { omit, pick } from 'lodash';

    /**
     * This is a styling wrapper around [react-dates](https://github.com/airbnb/react-dates).
     * Please see [here](https://github.com/airbnb/react-dates#singledatepicker) for prop types.
     *
     * The picker can be controlled by setting the props `date` and `onDateChange`.
     * `date` should be set to a moment object of the currently selected date,
     * while `onDateChange` should be a function that handles the date when it
     * changes.
     */
    class DatePicker extends React.PureComponent {
      static Range = DateRangePicker;

      static propTypes = {
        ...omit(SingleDatePickerShape, ['onFocusChange']),
        id: PropTypes.string,
        /**
         * A component to wrap and control styles of the underlying react-dates DatePicker
         */
        Wrapper: PropTypes.func,
        /**
         * Format to pass to moment to control the input's value
         */
        displayFormat: PropTypes.string,
        /**
         * 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 = {
        Wrapper: DatePickerWrapper,
        displayFormat: 'MMM DD YYYY',
        invalid: false,
        disabled: false,
      };

      state = {
        focused: false,
      };

      handleFocusChange = ({ focused }) => this.setState({ focused });

      render() {
        const { Wrapper, ...rest } = this.props;
        const { focused } = this.state;
        return (
          <Wrapper
            className={focused && 'focused'}
            openDirection={this.props.openDirection}
            invalid={this.props.invalid}
            disabled={this.props.disabled}
          >
            <LibDatePicker
              navPrev={<Icon name="back" />}
              navNext={<Icon name="forward" />}
              weekDayFormat="dd"
              daySize={37}
              horizontalMargin={0}
              numberOfMonths={1}
              hideKeyboardShortcutsPanel
              focused={focused}
              onFocusChange={this.handleFocusChange}
              {...pick(rest, Object.keys(SingleDatePickerShape))}
            />
          </Wrapper>
        );
      }
    }

    export default DatePicker;