UNPKG

@carbon/react

Version:

React components for the Carbon Design System

80 lines (78 loc) 2.51 kB
/** * Copyright IBM Corp. 2016, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const require_runtime = require("../../_virtual/_rolldown/runtime.js"); const require_usePrefix = require("../../internal/usePrefix.js"); const require_noopFn = require("../../internal/noopFn.js"); let classnames = require("classnames"); classnames = require_runtime.__toESM(classnames); let react = require("react"); react = require_runtime.__toESM(react); let prop_types = require("prop-types"); prop_types = require_runtime.__toESM(prop_types); let react_jsx_runtime = require("react/jsx-runtime"); //#region src/components/Switch/Switch.tsx /** * Copyright IBM Corp. 2016, 2023 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const Switch = react.default.forwardRef((props, tabRef) => { const { children, className, disabled, index, name, onClick = require_noopFn.noopFn, onKeyDown = require_noopFn.noopFn, selected = false, text, ...other } = props; const prefix = require_usePrefix.usePrefix(); const handleClick = (e) => { e.preventDefault(); onClick?.({ index, name, text }); }; const handleKeyDown = (event) => { const key = event.key || event.which; onKeyDown?.({ index, name, text, key }); }; const commonProps = { onClick: handleClick, onKeyDown: handleKeyDown, className: (0, classnames.default)(className, `${prefix}--content-switcher-btn`, { [`${prefix}--content-switcher--selected`]: selected }), disabled }; return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", { type: "button", ref: tabRef, role: "tab", tabIndex: selected ? 0 : -1, "aria-selected": selected, ...other, ...commonProps, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { className: `${prefix}--content-switcher__label`, title: text, children: text !== void 0 ? text : children }) }); }); Switch.displayName = "Switch"; Switch.propTypes = { children: prop_types.default.node, className: prop_types.default.string, disabled: prop_types.default.bool, index: prop_types.default.number, name: prop_types.default.oneOfType([prop_types.default.string, prop_types.default.number]), onClick: prop_types.default.func, onKeyDown: prop_types.default.func, selected: prop_types.default.bool, text: prop_types.default.string }; //#endregion exports.default = Switch;