Cannot Clear Dates on DayPickerInput in latest version (7.4.0^)
See original GitHub issueTrying to set dates to undefined to clear, causes the below error
A component is changing a controlled input of type undefined to be uncontrolled. Input elements should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component.
import * as React from 'react';
import moment from 'moment';
import { formatDate, parseDate } from 'react-day-picker/moment';
import DayPickerInput from 'react-day-picker/DayPickerInput';
class DayPickerTest extends React.Component<Props> {
constructor(props) {
super(props);
this.handleFromChange = this.handleFromChange.bind(this);
this.handleToChange = this.handleToChange.bind(this);
this.state = {
from: undefined,
to: undefined,
};
}
showFromMonth() {
const { from, to } = this.state;
if (!from) {
return;
}
if (moment(to).diff(moment(from), 'months') < 2) {
this.to.getDayPicker().showMonth(from);
}
}
handleFromChange(from) {
// Change the from date and focus the "to" input field
this.setState({ from });
}
handleToChange(to) {
this.setState({ to }, this.showFromMonth);
}
clearDates = () => {
this.setState({
from: undefined,
to: undefined,
});
}
render() {
const { from, to } = this.state;
const modifiers = { start: from, end: to };
return (
<div className="InputFromTo">
<div className="test"
onClick={this.clearDates}
/>
<DayPickerInput
value={from}
placeholder="From"
format="LL"
formatDate={formatDate}
parseDate={parseDate}
dayPickerProps={{
selectedDays: [from, { from, to }],
disabledDays: { after: to },
toMonth: to,
modifiers,
numberOfMonths: 2,
onDayClick: () => this.to.getInput().focus(),
}}
onDayChange={this.handleFromChange}
/>{' '}
—{' '}
<span className="InputFromTo-to">
<DayPickerInput
ref={el => (this.to = el)}
value={to}
placeholder="To"
format="LL"
formatDate={formatDate}
parseDate={parseDate}
dayPickerProps={{
selectedDays: [from, { from, to }],
disabledDays: { before: from },
modifiers,
month: from,
fromMonth: from,
numberOfMonths: 2,
}}
onDayChange={this.handleToChange}
/>
</span>
</div>
);
}
}
export default DayPickerTest;
`
Issue Analytics
- State:
- Created 4 years ago
- Reactions:10
- Comments:6
Top Results From Across the Web
react-day-picker/CHANGELOG.md - UNPKG
120, - (DayPickerInput) Fixed: overlay did not reset the displayed month when appearing again ... 147, > This should be the last minor...
Read more >Unable to use DayPickerInput from React-Day-Picker with ...
My impression is that the TypeScript definitions are not up to date, and someone (you or me?) needs to update them? :) Share....
Read more >Input Fields - React DayPicker
It is a common scenario to bind the date picker with an input field. ... const popper = usePopper(popperRef.current, popperElement, {
Read more >react-day-picker - Bountysource
Customizable date picker and calendar component for React.js. Become a Bounty Hunter ... We can't release it if we don't have a good...
Read more >react-day-picker - npm
Customizable Date Picker for React. Latest version: 8.4.1, last published: 9 days ago. Start using react-day-picker in your project by ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@redaxmedia Interestingly Douglas Crockford himself stopped using
null
and only usesundefined
with the rationale that undefined is already used a lot internally in javascript.Works for me with
7.4.8
… thanks for the fix