compose: Fix numeric typing regression related to global time widget.

In commit 1d54b383bd we introduced some
changes to add better support for keyboard navigation with the global
time widget. Unfortunately, as a result of the fact that
get_keydown_hotkey returns undefined for numeric keys, we caused a
regression that prevented users from typing into the time picker.
Additionally, we also lost support for the backspace and delete keys.

Hence, this commit fixes the above bug by early returning in two
places if the key pressed is backspace, or delete or a numeric key.
This commit is contained in:
YashRE42 2021-12-15 13:04:11 +05:30 committed by Tim Abbott
parent 30ccb76e19
commit bea41e975d

View File

@ -768,6 +768,10 @@ export function content_highlighter(item) {
}
}
function is_numeric_key(key) {
return ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].includes(key);
}
export function show_flatpickr(element, callback, default_timestamp, options = {}) {
const flatpickr_input = $("<input id='#timestamp_flatpickr'>");
@ -788,8 +792,18 @@ export function show_flatpickr(element, callback, default_timestamp, options = {
formatDate: (date) => formatISO(date),
disableMobile: true,
onKeyDown: (selectedDates, dateStr, instance, event) => {
if (is_numeric_key(event.key)) {
// Don't stopPropagation for numeric inputs, let them be handled normally
return;
}
const hotkey = get_keydown_hotkey(event);
if (hotkey === "backspace" || hotkey === "delete") {
// Don't stopPropagation for backspace or delete, let them be handled normally
return;
}
if (["tab", "shift_tab"].includes(hotkey.name)) {
const elems = [
instance.selectedDateElem,
@ -815,12 +829,20 @@ export function show_flatpickr(element, callback, default_timestamp, options = {
const container = $($(instance.innerContainer).parent());
container.on("keydown", (e) => {
if (is_numeric_key(e.key)) {
return true; // Let users type numeric values
}
const hotkey = get_keydown_hotkey(e);
if (!hotkey) {
return false;
}
if (hotkey.name === "backspace" || hotkey.name === "delete") {
return true; // Let backspace or delete be handled normally
}
if (hotkey.name === "enter") {
if (e.target.classList[0] === "flatpickr-day") {
return true; // use flatpickr default implementation