UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

41 lines (35 loc) 1.29 kB
/** * Copyright IBM Corp. 2020, 2025 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var React = require('react'); const useFocus = size => { // the state should represent the location of the item in the array // make it '' initially so that it doesn't automatically focus the first item const [currentFocus, setCurrentFocus] = React.useState(''); const handleKeyDown = React.useCallback(e => { const focus = currentFocus === '' ? 0 : currentFocus; if (e.keyCode === 40) { // Down arrow e.preventDefault(); setCurrentFocus(focus === size - 1 ? 0 : focus + 1); } else if (e.keyCode === 38) { // Up arrow e.preventDefault(); setCurrentFocus(focus === 0 ? size - 1 : focus - 1); } }, [size, currentFocus, setCurrentFocus]); React.useEffect(() => { const el = document.querySelector('#add-select-focus'); el.addEventListener('keydown', handleKeyDown, false); return () => { el.removeEventListener('keydown', handleKeyDown, false); }; }, [handleKeyDown]); return [currentFocus, setCurrentFocus]; }; exports.default = useFocus;