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 e700b99fcf
commit ea2f5ebbf9
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.