React-datepicker: Can't manually change time in input

Created on 18 Mar 2019  路  9Comments  路  Source: Hacker0x01/react-datepicker

Expected behavior

Change time to the one I entered.

Actual behavior

Time does not change.
Mar-18-2019 18-14-15

Steps to reproduce

react-datepicker v2.0.0

import DatePicker from 'react-datepicker';
...
<DatePicker
  id={code}
  selected={value}
  onChange={(date) => { this.setState({ value: date }); }}
  showTimeSelect
  timeFormat="HH:mm"
  dateFormat="dd/MM/yyyy HH:mm:ss"
/>

Most helpful comment

@martijnrusschen Are there plans to fix this issue ? By when can we expect this to be resolved ?

All 9 comments

try using onChangeRaw

<DatePicker
  id={code}
  selected={value}
  onChange={(date) => { this.setState({ value: date }); }}
  onChangeRaw={(date) => { this.setState({ value: date }); }}
  showTimeSelect
  timeFormat="HH:mm"
  dateFormat="dd/MM/yyyy HH:mm:ss"
/>

onChangeRaw returns event, not date.
And I think it should act the same way when you change one number in time (now it does not work) and when you change the whole time (everything works).
Apr-02-2019 17-42-14

If someone can point me in the right direction, I would try fixing this. It seems like the picker is currently really broken to users.

Maybe it will be useful: in my case i've made a mistake in dateFormat prop, so manual input was broken although choosing dates in dropdown worked.

On official page https://reactdatepicker.com there are all examples have same functional.
And i found one decision to use onChangeRaw

In official page you can't change time manually. But you can change date manually. I think it's strange when one part of input works fine and the other works not fine.

@martijnrusschen Are there plans to fix this issue ? By when can we expect this to be resolved ?

As @mxsmirnoff mentions, all of the examples on the official website are broken. Changing the time manually on any of those resets to the previous value. This is particularly obvious (and bad UX) when using showTimeSelectOnly, and I have to explain to users that they need to use the popover for now. I'd love to create a PR but unfortunately it's beyond my current skillset. I'd be super grateful for a fix!

Does anyone have a workaround for now? onChangeRaw does not work for me.

@jdahdah I did something like this with onChangeRaw to solve this

<DatePicker
  id={code}
  selected={value}
  onChange={(date, event) => {
    if (!(event && event.target.value)) {
      this.handleChange(code, date);
    }
  }}
  onChangeRaw={(event) => {
     this.handleChangeRaw(code, dateFormat, dateTimeFormat, event.target.value);
  }}
  showTimeSelect
  timeFormat="HH:mm"
  dateFormat={dateTimeFormat}
/>

...

handleChangeRaw = (code, dateFormat, dateTimeFormat, value) => {
  if (isValid(parse(value, dateTimeFormat, new Date()))) {
    this.handleChange(code, parse(value, dateTimeFormat, new Date()));
  } else if (isValid(parse(value, dateFormat, new Date()))) {
    this.handleChange(code, parse(value, dateFormat, new Date()));
  } else {
    this.handleChange(code, null);
  }
};
Was this page helpful?
0 / 5 - 0 ratings

Related issues

evolve2k picture evolve2k  路  3Comments

formigone picture formigone  路  3Comments

ahribori picture ahribori  路  3Comments

sarav1234 picture sarav1234  路  3Comments

hoodsy picture hoodsy  路  3Comments