@activecollab/components
Version:
ActiveCollab Components
22 lines • 904 B
JavaScript
export const handleKeyboardMovement = (e, hover, flatOptions, showAddNew, showDefaultOption) => {
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
e.preventDefault();
}
let options = flatOptions;
if (showDefaultOption) options = [{
id: null
}, ...options];
if (showAddNew) options = [...options, {
id: "addNew"
}];
const hoveredIndex = options.findIndex(option => option.id === hover);
if (e.key === "ArrowDown") {
const isHoveredLastOption = options.findIndex(v => v.id === hover) + 1 >= options.length;
return hoveredIndex === -1 || isHoveredLastOption ? options[0].id : options[hoveredIndex + 1].id;
}
if (e.key === "ArrowUp") {
const isHoveredFirstOption = options[0].id === hover;
return hoveredIndex === -1 || isHoveredFirstOption ? options[options.length - 1].id : options[hoveredIndex - 1].id;
}
};
//# sourceMappingURL=HandleKeyboard.js.map