简体   繁体   中英

onDateChange returns null when user types a past date using react-dates

I'm using SingleDatePicker from react-dates and it works fine when selecting a date from the datepicker(even a past date), but when the user types a past date the onDateChange handler returns null, but when the user types a future date onDateChange returns a moment object.

      <SingleDatePicker
        date={moment(Date.now())}
        focused={focused}
        displayFormat="MM/DD/YYYY"
        onFocusChange={() => setFocused(!focused)} 
        onDateChange={date => {
          console.log(date);
        }}
      />

Here's what i get when typing the current or future date and null is when typing a past date.

来自 devtools 的屏幕截图

If you want to select past days, pass in isOutsideRange={() => false} prop.

source

<SingleDatePicker
  date={moment(Date.now())}
  focused={true}
  displayFormat="MM/DD/YYYY"
  onFocusChange={() => true}
  onDateChange={date => {
    console.log(date);
  }}
  isOutsideRange={() => false}
/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM