Material-ui-pickers: Invalid Date Format when field is cleared

Created on 15 Feb 2018  路  1Comment  路  Source: mui-org/material-ui-pickers

When the CLEAR button is used, the input value becomes Unknown and the error Invalid Date Format is displayed under the input field.

I would expect the value to be an empty string and no error to be displayed.

Here is an example of the issue:
https://codesandbox.io/s/3vm7219x81

Most helpful comment

I found the problem in my code - my onChange function was trying to format the value without checking for validity:

handleDateChange = date => {
    const searchDate = moment(date).format("YYYY-MM-DD");

    this.setState({ searchDate });
};

this works:

handleDateChange = date => {
    const searchDate = moment(date).isValid()
        ? moment(date).format('YYYY-MM-DD')
        : ''

    this.setState({ searchDate })
}

>All comments

I found the problem in my code - my onChange function was trying to format the value without checking for validity:

handleDateChange = date => {
    const searchDate = moment(date).format("YYYY-MM-DD");

    this.setState({ searchDate });
};

this works:

handleDateChange = date => {
    const searchDate = moment(date).isValid()
        ? moment(date).format('YYYY-MM-DD')
        : ''

    this.setState({ searchDate })
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

aditya81070 picture aditya81070  路  3Comments

harvitronix picture harvitronix  路  3Comments

nicky-dev picture nicky-dev  路  3Comments

danmce picture danmce  路  3Comments

filipenevola picture filipenevola  路  4Comments