Sha256: 6cbd8079f2b90e43fcfeb1395b4f1c0443e50ad4a9925c0eb96db7124b83e8e3
Contents?: true
Size: 1.26 KB
Versions: 2
Compression:
Stored size: 1.26 KB
Contents
import React, { useContext } from "react"; import DropdownContext from "../context"; export const useHandleOnKeyDown = () => { const { autocomplete, focusedOptionIndex, filteredOptions, setFocusedOptionIndex, handleOptionClick, setIsDropDownClosed, handleBackspace, selected }= useContext(DropdownContext) return (e: React.KeyboardEvent) => { switch (e.key) { case "Backspace": case "Delete": if (autocomplete) { handleBackspace(); } break; case "ArrowDown": { e.preventDefault(); setIsDropDownClosed(false); const nextIndex = (focusedOptionIndex + 1) % filteredOptions.length; setFocusedOptionIndex(nextIndex); break; } case "ArrowUp": { e.preventDefault(); const nextIndexUp = (focusedOptionIndex - 1 + filteredOptions.length) % filteredOptions.length; setFocusedOptionIndex(nextIndexUp); break; } case "Enter": if (focusedOptionIndex !== -1) { e.preventDefault(); handleOptionClick(filteredOptions[focusedOptionIndex]); setFocusedOptionIndex(-1) } break; default: if (selected && selected.label) { e.preventDefault(); handleBackspace(); } break; } } };
Version data entries
2 entries across 2 versions & 1 rubygems