react-datepicker icon indicating copy to clipboard operation
react-datepicker copied to clipboard

It should be possible to set input format

Open Frezzaldo opened this issue 9 years ago • 4 comments

I want to be able to give the date in format DDMMYY (by input) and then display it as DD/MM YYYY or anything else. At the moment the input format must be the same as display format.

Frezzaldo avatar May 04 '17 12:05 Frezzaldo

@Frezzaldo So you want the user to enter "30022017" and the datepicker to format it as "30/02 2017"? You could try to achieve that behavior by using the onChangeRaw property.

import moment from 'moment';

// Your custom transformation/validation
myCustomFunction = (rawValue) => {
  const parsedDate = moment(rawValue, "DDMMYY")
  if(parsedDate.isValid()) {
    this.setState({selectedDate: parsedDate});
  }
}

render() {
  <DatePicker
    dateFormat="DD/MM YYYY"
    selected={this.state.selectedDate}
    onChange={this.handleChange}
    onChangeRaw={(event) =>
      this.myCustomFunction(event.target.value)}
  />
}

mgrdevport avatar May 07 '17 22:05 mgrdevport

Thanks that worked, but now there is another problem. My config looks like this:

<DatePicker 
     selected={selectedMoment}
     onChange={date => this.handlerFunction(date)}
     onChangeRaw={event => {this.rawHandlerFunction(event.target.value)}}
     className="datepicker"
     placeholderText="MMDDYY..."
     dateFormat={"D/M YYYY"}
     locale={"nb-NO"}
     disabledKeyboardNavigation={true}
     minDate={momentFromOtherDatepicker}
     scrollableYearDropdown={true}
     showYearDropdown={true}/>

When clicking on the input the calendar is displayed. Todays date is marked with a light blue color. Then i write "260517" and a dark blue color is marking that date and I also see that the code get "260517". But if I press enter the light blue date (todays date )is then sent as input to the onChange-method and the date is changed back to todays date or whatever was selected before. If I just tab to next input field instead of pressing enter it works as expected.

Frezzaldo avatar May 09 '17 07:05 Frezzaldo

@Frezzaldo I've created a pr to solve your issue, but it may cause side effects that I'm not aware of. So let's see, what the maintainers are saying.

mgrdevport avatar May 09 '17 19:05 mgrdevport

Fixed by #857

martijnrusschen avatar Jul 10 '17 08:07 martijnrusschen