reactjsdatepickermaterial-ui

How can I clear Material UI DatePicker input?


I would like to add a 'clear' button to a DatePicker from @mui/lab (5.0.0-alpha.55).

What I am attempting:

The behaviour I observe:

The rudimentary version I have attempted which shows the behaviour above can be seen here.

If you input a partial date, then click clear, you can observe that the input does not get cleared.

I would prefer to avoid a solution that involves changing the key, as that brings other issues, such as not respecting an external source changing the date to null, and needing additional hacks to respect the label transition when clearing the input.


Solution

  • My theory is that internally, DatePicker only updates the input value if it's different with the last valid value. Below is how the bug can occur:

    I'd classify this as a bug from MUI, you can create an issue on github if you want it to be fixed. In the meanwhile, you can fix the bug by applying this patch using patch-package:

    "scripts": {
      "postinstall": "patch-package"
    }