@carbon/react
Version:
React components for the Carbon Design System
76 lines (74 loc) • 2.05 kB
JavaScript
/**
* 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.
*/
import { usePrefix } from "../../internal/usePrefix.js";
import { noopFn } from "../../internal/noopFn.js";
import classNames from "classnames";
import React from "react";
import PropTypes from "prop-types";
import { jsx } from "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.forwardRef((props, tabRef) => {
const { children, className, disabled, index, name, onClick = noopFn, onKeyDown = noopFn, selected = false, text, ...other } = props;
const prefix = 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: classNames(className, `${prefix}--content-switcher-btn`, { [`${prefix}--content-switcher--selected`]: selected }),
disabled
};
return /* @__PURE__ */ jsx("button", {
type: "button",
ref: tabRef,
role: "tab",
tabIndex: selected ? 0 : -1,
"aria-selected": selected,
...other,
...commonProps,
children: /* @__PURE__ */ jsx("span", {
className: `${prefix}--content-switcher__label`,
title: text,
children: text !== void 0 ? text : children
})
});
});
Switch.displayName = "Switch";
Switch.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
disabled: PropTypes.bool,
index: PropTypes.number,
name: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
onClick: PropTypes.func,
onKeyDown: PropTypes.func,
selected: PropTypes.bool,
text: PropTypes.string
};
//#endregion
export { Switch as default };