# All files / layouts/Flow/fields FlowDatePicker.js

0% Statements 0/8
0% Branches 0/4
0% Functions 0/3
0% Lines 0/8

    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

     import React from 'react';
    import { Flow } from '../..';
    import { DatePicker } from '../../../components';
    import PropTypes from 'prop-types';

    export default class FlowDatePicker extends React.Component {
      static propTypes = {
        input: PropTypes.shape({
          onChange: PropTypes.func.isRequired,
          value: PropTypes.instanceOf(Date).isRequired,
        }).isRequired,
        error: PropTypes.string,
        label: PropTypes.string,
      };

      static defaultProps = {
        error: undefined,
        label: 'Select a date',
      };

      constructor(props) {
        super(props);
        this.state = {
          error: undefined,
        };
      }
      handleError = err => {
        const error = (err) ? err.message : undefined;
        this.setState(Object.assign({}, this.state, { error }));
      }

      render() {
        const { onChange, value } = this.props.input;
        const { label } = this.props;
        const error = this.props.error || this.state.error;  // Error from props takes precedence

        return (
          <Flow.Item error={error} label={label}>
            <DatePicker
              onChange={onChange}
              value={value}
              onError={this.handleError}
            />
          </Flow.Item>
        );
      }
    }