Search code examples
javascriptdatereactjsredux-formreact-day-picker

React-day picker with redux form


i need to make react-day picker component compatible with redux form. I know this is not absolutey right but redux form is manadatory for my current project. But i struggle to make it. I used react date picker which i made it compatible with this way:

import React from 'react';
import { PropTypes } from 'prop-types';
import DatePicker from 'react-datepicker';
import moment from 'moment';
import { injectIntl, intlShape } from 'react-intl';
import 'react-datepicker/dist/react-datepicker.css';

    const MyDatePicker = props => (
      <div>
        <DatePicker
          {...props.input}
          dateFormat="DD-MM-YYYY"
          selected={props.input.value
            ? moment(props.input.value, 'DD-MM-YYYY')
            : null}
          placeholderText={props.placeholder}
          disabled={props.disabled}
        />
        {
          props.meta.touched && props.meta.error &&
          <span className="error">
            { props.intl.formatMessage({ id: props.meta.error }) }
          </span>
        }
      </div>
    );

    MyDatePicker.propTypes = {
      input: PropTypes.shape().isRequired,
      placeholder: PropTypes.string,
      disabled: PropTypes.bool,
      meta: PropTypes.shape().isRequired,
      intl: intlShape.isRequired
    };

    export default injectIntl(MyDatePicker);

but i struggle to make it with react day picker. Can anyone help me for achieving this?


Solution

  • I had a similar issue months back where the project called for redux forms and a date picker. The solution I came to was to wrap the date picker into it's own component and then wrap that component in another component, which then got used as a custom input component in redux forms.

    I imagine you won't have to wrap as many times as I did but the concept should still be similar. Take a look at the code in this question as it shows an example of how to incorporate a date picker with redux-forms:

    How to onFocus and onBlur a React/Redux form field that's connected to React Date Picker?