How can I clear MUI DatePicker input?

Viewed 6385

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

What I am attempting:

  • Store a date field in state, passed as the value prop to DatePicker
  • Change the date to null when desired to 'clear' the value & input

The behaviour I observe:

  • If the date is valid, it works as expected, and the input is cleared
  • If the date is not valid, the error is cleared, but the invalid date still stays in the input.

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.

3 Answers

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:

  • You clear the DatePicker when there is a valid value (like the initial Date), the state is reset successfully at first (value=null, inputValue='')
  • You enter some invalid date. The state is now (value=Invalid Date, inputValue='invalid Text')
  • Because the current value is invalid, it does not count and the DatePicker references the last valid value instead which is null, then decide that the current value doesn't change and skip dispatching the new input value (value=null, inputValue='invalid Text').

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:

  • Install patch-package: npm i patch-package
  • Add postinstall script in the package.json
"scripts": {
  "postinstall": "patch-package"
}
  • Open the buggy file at node_modules\@mui\lab\internal\pickers\hooks\useMaskedInput.js and change it based on this commit.
  • Run npx patch-package @mui/lab to create the patch that will be applied every time after you ran npm i @mui/lab again.
  • Restart your dev server.

MUI DatePicker has a clearable property. When set to true, it shows a clear button. You can change the text of the clear button using the clearText property. I would point out however, that it does not trigger an onChange event, nor is there an onClear event (at the time of writing this post). So when I am using it, its not possible to change the state that way. However the onAccept event is fired, and if you pass the value parameter, it will be null.

<MobileDatePicker
  open
  clearable
  label="some label"
  inputFormat="yyyy-MM-dd"
  value={stateDate}
  onChange={(date) => updateAvailableDate(date)}
  renderInput={(params) => (
    <TextField
      {...params}
    />
  )}
  onClose={() => hideModal()}
  clearText="Clear me"
  onAccept={(value) => handleSave(value)}
/>

See all the api settings here. https://mui.com/api/date-picker/

This is what I did to make the clear function work. I recently found this solution so I might change this in the future... but for now it's fine.

       <MobileDateTimePicker
                ...
                clearable
                clearText="Clear"
                onChange={(newScheduleDateTime) => {
                  if (newScheduleDateTime) {
                    setScheduleDateTime(newScheduleDateTime);
                  } else {
                    setScheduleDateTime(null);
                  }
                }}
              />
Related