Error after upgrading react-datepicker from "^0.46.0" to "^0.61.0"
Hi, after upgrading react-datepicker from "^0.46.0" to "^0.61.0" I get the following error:
Warning: Failed prop type: Invalid prop value of type object supplied to DatePicker, expected string.
The DatePicker correctly displays the date provided as initial value : "22/05/2017". Then the Error occurs when I first time select a new date , when "selected={null}". When selecting a date second, third ... time ( when selected is not null ), the error does not appear. I use DatePicker together with Redux-form.
This is my component:
import React from 'react';
import DatePicker from "react-datepicker";
import 'react-datepicker/dist/react-datepicker.css';
import {object, string, bool, shape} from 'prop-types';
import moment from 'moment';
import '../../style/css/customDaypicker.css'
const regEx = /^[0-9]{2}[/-][0-9]{2}[/-][0-9]{4}$/;
const getInputValue = (value) => value && regEx.test(moment(value).format('L')) ? moment(value) : null;
export default function DateField({input, label, placeholder, disabled, maxDate, minDate, meta: {touched, error}}) {
return (
<div>
<label>{label}:</label>
<div>
<DatePicker {...input}
selected={getInputValue(input.value)}
maxDate={maxDate}
minDate={minDate}
placeholderText={placeholder}
disabled={disabled}
/>
{touched && error && <label className="error">{label} {error}</label>}
</div>
</div>
)
}
Does anyone have an idea how to fix the issue? Thanks a lot in advance.
Sure: Stop passing the value prop. It used to be ignored, but now is used to control the input value, so if you want the old behavior, just don't set the value prop.
After refactoring this code is now working for me.
export default class DateFieldWithIcon extends Component {
static propTypes = {
input: shape({
onChange: func,
value: string,
}).isRequired,
meta: shape({
touched: bool,
error: string,
}).isRequired,
placeholder: string,
disabled: bool,
maxDate: string,
minDate: string
};
state = {
selectedDate: moment(this.props.input.value),
};
onChange = (date, v) => {
this.setState({selectedDate: date,});
const { input, meta: { onChange } } = this.props;
if (onChange && typeof onChange === 'function') {
onChange(date, v, input);
}
};
onBlur = (date) => {
const { input } = this.props;
input.onBlur(date, input);
};
render() {
const {label, placeholder, disabled, maxDate, minDate, meta: {touched, error}} = this.props;
return (
<div>
<label>{label}:</label>
<div>
<DatePicker selected={this.state.selectedDate}
onChange={this.onChange}
onBlur={this.onBlur}
className="form-control index1 darkColor"
maxDate={maxDate}
minDate={minDate}
placeholderText={placeholder}
disabled={disabled}
/>
{touched && error && <label className="error">{label} {error}</label>}
</div>
</div>
);
}
}
Thanks for help.