UNPKG

@activecollab/components

Version:

ActiveCollab Components

32 lines (31 loc) 1.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleKeyboardMovement = void 0; var handleKeyboardMovement = exports.handleKeyboardMovement = function handleKeyboardMovement(e, hover, flatOptions, showAddNew, showDefaultOption) { if (e.key === "ArrowDown" || e.key === "ArrowUp") { e.preventDefault(); } var options = flatOptions; if (showDefaultOption) options = [{ id: null }, ...options]; if (showAddNew) options = [...options, { id: "addNew" }]; var hoveredIndex = options.findIndex(function (option) { return option.id === hover; }); if (e.key === "ArrowDown") { var isHoveredLastOption = options.findIndex(function (v) { return v.id === hover; }) + 1 >= options.length; return hoveredIndex === -1 || isHoveredLastOption ? options[0].id : options[hoveredIndex + 1].id; } if (e.key === "ArrowUp") { var isHoveredFirstOption = options[0].id === hover; return hoveredIndex === -1 || isHoveredFirstOption ? options[options.length - 1].id : options[hoveredIndex - 1].id; } }; //# sourceMappingURL=HandleKeyboard.js.map