Search code examples
reactjsformsreduxfield

Using React-Redux Form Fields with Date Range in react-datepicker


From the react-datepicker:

https://github.com/Hacker0x01/react-datepicker/blob/master/docs-site/src/examples/date_range.jsx#L4

For this :

datePicker.js

const { fields, input , meta } = this.props;
const { touched, error, warning } = meta || {}

...

return (

<DatePicker 
    selected={this.state.startDate ? this.state.startDate : undefined} 
    selectsStart 
    startDate={this.state.startDate} 
    endDate={this.state.endDate} 
    onChange={this.handleChangeStart} 
    dateFormat="DD/MM/YYYY"
/>
<span> to </span>  
<DatePicker 
    selected={this.state.endDate ? this.state.endDate : undefined} 
    selectsEnd 
    startDate={this.state.startDate}  
    endDate={this.state.endDate} 
    onChange={this.handleChangeEnd} 
    dateFormat="DD/MM/YYYY"
/>
{touched && ( /* << how to declare the meta respectively ? */  
(error && <span>ERR</span>)||
(warning && <span>WARN</span>)
)}

...

)

As there are 2 field inputs, I decided to use "Fields" in redux form, but how should I specify the input name and value I get in the component?

I try this, but the names and values seems to be wrong, and I don't know how to declare the name and get the value from the component.

callDatePicker.js

<Fields 
   labelNode={<Label>Range</Label>}
   labelStartDate="Start Date"
   labelEndDate="End Date"
   names={["nameStartDate", "nameEndDate"]}
   values={[ this.props.start , this.props.end]} << this returns wrong saying the value is object instead of string..
   component={ControlledDateRangePicker}
/>

Solution

  • Create two separated fields:

        <Field
            name="startDatePicker"
            label="Start Date"
            fieldName="startDate"
            component={ControlledDateRangePicker}
            ...
          />
        <Field
            name="endDatePicker"
            label="End Date"
            fieldName="endDate"
            component={ControlledDateRangePicker}
            ...
          />
    

    And in ControlledDateRangePicker use only one DatePicker component, that should be generic.