Search code examples
reactjsdatepickerdefault-valueredux-form

Default Value in react-datepicker


i'm using react-datepicker module in my react-redux app and i made it compatible with redux form like this:

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>
);

The problem is that i dont know how to add a default value in my module. This default value must be today. Any ideas of how to handle this?


Solution

  • change:

     selected={props.input.value
            ? moment(props.input.value, 'DD-MM-YYYY')
            : null}
    

    t0

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