Add listener to clear date pickers on certain keystrokes

This commit is contained in:
Andrew Kingston 2021-02-03 11:10:47 +00:00
parent 181143935e
commit 6b6699c857
1 changed files with 9 additions and 0 deletions

View File

@ -26,12 +26,21 @@
fieldApi.setValue(dates[0])
}
const clearDateOnBackspace = event => {
if (["Backspace", "Clear", "Delete"].includes(event.key)) {
fieldApi.setValue(null)
flatpickr.close()
}
}
const onOpen = () => {
open = true
document.addEventListener("keyup", clearDateOnBackspace)
}
const onClose = () => {
open = false
document.removeEventListener("keyup", clearDateOnBackspace)
// Manually blur all input fields since flatpickr creates a second
// duplicate input field.