material-ui-pickers icon indicating copy to clipboard operation
material-ui-pickers copied to clipboard

KeyboardDatePicker ignores specified format for intput

Open spik3r opened this issue 6 years ago • 4 comments

A GIF or MEME to give some spice of the internet

Environment

Tech Version
@material-ui/pickers 3.2.8
material-ui 4.7.2
TypeScript 3.8
React 16.12
Browser Chrome
Peer library moment 2.24
Peer library date-io/moment 1.3.13

Steps to reproduce

Create a KeyboardDatePikcer

      <Grid container justify="space-around">
        <KeyboardDatePicker
          disableToolbar
          variant="inline"
          autoOk
          format="MMM DD, YYYY"
          margin="normal"
          id="date-picker-inline"
          label="Date picker inline"
          value={selectedDate}
          onChange={handleDateChange}
          KeyboardButtonProps={{
            "aria-label": "change date"
          }}
        />
      </Grid>
    </MuiPickersUtilsProvider>
  1. Select a date using the Calendar widget - Should work as expected image

  2. Try to change the date using input - Notice how the Date gets changed to seemingly random numbers, and it's an instant error. image

Expected behavior

KeyboardDatePicker should work for both Calendar widget and input regardless of date format (or at least have the option to disable the input while still being able to use the Calendar widget)

Actual behavior

input does not work for text based date formats like MMM DD, YYYY and simply pressing backspace or trying to change the date results in an error

Live example

https://codesandbox.io/s/material-demo-l5cis

spik3r avatar Dec 11 '19 10:12 spik3r

I have had the same issue for the past couple of days and I figured a workaround I hope it helps.

I am using regular expression for refuse and rifmFormatter properties to do away with weird date formatting issue and accept month name as string. Also for onChange I am passing an empty function instead I am using onBlur and onAccept to handleDateChange()

 public render() {
        const {
            datePickerData: {
                content: { reference, validationRules, placeHolder },
            },
            showError,
        } = this.props;

<div style={{ position: "relative" }}>
    <MuiPickersUtilsProvider utils={MomentUtils}>
      <KeyboardDatePicker
             disableToolbar={true}
             variant="inline"  
             inputVariant="outlined"  
             rifmFormatter={val=> val.replace(/[^\.\ \,\[a-zA-Z0-9_]*$]+/gi, '')}
             refuse={/[^\.\ \,\[a-zA-Z0-9_]*$]+/gi}  
             format={"MMM, DD YYYY"} 
             autoOk 
             value={this.selectedDay}
             id={`datePicker-${reference}`} 
             onChange={()=>{}}  
             onAccept={this.handleDateChange}
             onBlur={this.handleDateChange} 
              />
    </MuiPickersUtilsProvider>
</div>
};

public handleDateChange= (selectedDate:any) => {  
        let newDate=null;
        if(selectedDate===null)return;
        if(!moment.isMoment(selectedDate)){
         let el = selectedDate.currentTarget.defaultValue; 
         let elementValue = moment(el).toDate();
         newDate = elementValue;
        }else{
            newDate = selectedDate.toDate();
        }
  if(moment(this.selectedDay).format('YYYY-MM-DD')!== moment(newDate).format('YYYY-MM-DD')){
               this.selectedDay = newDate;  
           }      
    };  

Reshad-A avatar Dec 17 '19 18:12 Reshad-A

Thanks @Reshad-A I'll give that a try.

spik3r avatar Dec 18 '19 20:12 spik3r

Fixed in v4

dmtrKovalenko avatar Feb 23 '20 12:02 dmtrKovalenko

I tested in v4 for Jalali and not fixed.

Manshadi1359 avatar Feb 22 '21 02:02 Manshadi1359