UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

96 lines 4.24 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ButtonGroup = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); /** * * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Neo4j is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ const react_1 = __importStar(require("react")); const ButtonGroup = ({ children }) => { const [selectedIndex, setSelectedIndex] = (0, react_1.useState)(0); const parentRef = (0, react_1.useRef)(null); const focusElement = (index) => { var _a, _b; const child = (_b = (_a = parentRef.current) === null || _a === void 0 ? void 0 : _a.children[index]) === null || _b === void 0 ? void 0 : _b.children[0]; if (child instanceof HTMLElement) { child.focus(); } }; const childrenCount = (0, react_1.useMemo)(() => react_1.default.Children.count(children), [children]); const safeSetSelectedIndex = (0, react_1.useCallback)((index) => { if (index >= childrenCount) { setSelectedIndex(childrenCount - 1); } else { setSelectedIndex(Math.max(0, index)); } }, [childrenCount, setSelectedIndex]); const handleKeyDown = (event) => { let newIndx = selectedIndex; if (event.key === 'ArrowRight' || event.key === 'ArrowDown') { newIndx = (selectedIndex + 1) % react_1.default.Children.count(children); safeSetSelectedIndex(newIndx); } else if (event.key === 'ArrowLeft' || event.key === 'ArrowUp') { newIndx = (selectedIndex - 1 + react_1.default.Children.count(children)) % react_1.default.Children.count(children); safeSetSelectedIndex(newIndx); } // set focus to new button focusElement(newIndx); }; return ( // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions (0, jsx_runtime_1.jsx)("ul", { onKeyDown: (e) => handleKeyDown(e), ref: parentRef, style: { all: 'inherit', listStyleType: 'none' }, children: react_1.default.Children.map(children, (child, index) => { if (!react_1.default.isValidElement(child)) { return null; } const clonedElement = (0, react_1.cloneElement)(child, { tabIndex: selectedIndex === index ? 0 : -1, }); return (0, jsx_runtime_1.jsx)("li", { children: clonedElement }, index); }) })); }; exports.ButtonGroup = ButtonGroup; //# sourceMappingURL=button-group.js.map