UNPKG

@adaptabletools/adaptable-cjs

Version:

Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

40 lines (39 loc) 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useKeyboardNavigation = void 0; const tslib_1 = require("tslib"); const React = tslib_1.__importStar(require("react")); const useKeyboardNavigation = (setCurrentSection, sections) => { const handleKeyPress = React.useCallback((event) => { if ( // (event.ctrlKey || event.metaKey) && event.code === 'ArrowUp' || event.code === 'ArrowDown') { event.preventDefault(); event.stopPropagation(); const isKeyUp = event.code === 'ArrowUp'; const validIndexes = sections.reduce((acc, item, index) => { if (item !== '-') { acc.push(index); } return acc; }, []); setCurrentSection((currentActiveSection) => { let nextActiveSection = null; const currentActiveInList = validIndexes.indexOf(currentActiveSection); if (isKeyUp) { const candidate = validIndexes[currentActiveInList - 1]; nextActiveSection = typeof candidate === 'number' ? candidate : validIndexes[validIndexes.length - 1]; } else { const candidate = validIndexes[currentActiveInList + 1]; nextActiveSection = typeof candidate === 'number' ? candidate : validIndexes[0]; } return nextActiveSection; }); } }, [sections, setCurrentSection]); return handleKeyPress; }; exports.useKeyboardNavigation = useKeyboardNavigation;