UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

115 lines 6.36 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SegmentedControl = 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 classnames_1 = __importDefault(require("classnames")); const react_1 = __importStar(require("react")); const Tooltip_1 = require("../tooltip/Tooltip"); // eslint-disable-next-line no-redeclare const SegmentedControlContext = // eslint-disable-next-line @typescript-eslint/no-explicit-any react_1.default.createContext(null); const useSelectorContext = () => { const context = (0, react_1.useContext)(SegmentedControlContext); if (context === null) { throw new Error('SegmentedControl used without context'); } return context; }; const SegmentedControlComponent = (_a) => { var { size = 'medium', hasOnlyIcons = false, children, selected, onChange, as, className, style, isFloating = false, htmlAttributes, ref } = _a, restProps = __rest(_a, ["size", "hasOnlyIcons", "children", "selected", "onChange", "as", "className", "style", "isFloating", "htmlAttributes", "ref"]); const classes = (0, classnames_1.default)('ndl-segmented-control', className, { 'ndl-floating': isFloating, 'ndl-large': size === 'large', 'ndl-medium': size === 'medium', 'ndl-small': size === 'small', }); const Component = as !== null && as !== void 0 ? as : 'div'; return ((0, jsx_runtime_1.jsx)(Component, Object.assign({ className: classes, style: style, role: "group", ref: ref }, restProps, htmlAttributes, { children: (0, jsx_runtime_1.jsx)(SegmentedControlContext.Provider, { value: { hasOnlyIcons, onChange, selected }, children: children }) }))); }; SegmentedControlComponent.displayName = 'SegmentedControl'; const SegmentedControlItem = (_a) => { var { as, children, className, style, value, htmlAttributes, description, tooltipProps, isDisabled = false, ref } = _a, restProps = __rest(_a, ["as", "children", "className", "style", "value", "htmlAttributes", "description", "tooltipProps", "isDisabled", "ref"]); const { onChange, selected, hasOnlyIcons } = useSelectorContext(); const isSelected = value === selected; const baseClasses = (0, classnames_1.default)(className, { 'ndl-current': isSelected, 'ndl-segment-icon': hasOnlyIcons, 'ndl-segment-item': !hasOnlyIcons, }); const Component = as !== null && as !== void 0 ? as : 'button'; return ((0, jsx_runtime_1.jsxs)(Tooltip_1.Tooltip, Object.assign({ isPortaled: false, isDisabled: Boolean(description) === false }, tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.root, { type: "simple", children: [(0, jsx_runtime_1.jsx)(Tooltip_1.Tooltip.Trigger, Object.assign({}, tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.trigger, { hasButtonWrapper: true, children: (0, jsx_runtime_1.jsx)(Component, Object.assign({ onClick: () => !isSelected && onChange(value), className: baseClasses, style: style, ref: ref, "aria-label": description, disabled: isDisabled }, restProps, htmlAttributes, { children: children })) })), (0, jsx_runtime_1.jsx)(Tooltip_1.Tooltip.Content, Object.assign({}, tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.content, { children: (0, jsx_runtime_1.jsx)(Tooltip_1.Tooltip.Body, { children: description }) }))] }))); }; SegmentedControlItem.displayName = 'SegmentedControl.Item'; const SegmentedControl = Object.assign(SegmentedControlComponent, { Item: SegmentedControlItem, }); exports.SegmentedControl = SegmentedControl; //# sourceMappingURL=SegmentedControl.js.map