@carbon/react
Version:
React components for the Carbon Design System
161 lines (153 loc) • 4.95 kB
JavaScript
/**
* 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.
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var PropTypes = require('prop-types');
var React = require('react');
var cx = require('classnames');
var index = require('../IconButton/index.js');
var usePrefix = require('../../internal/usePrefix.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var cx__default = /*#__PURE__*/_interopDefaultLegacy(cx);
const noopFn = () => {};
const IconSwitch = /*#__PURE__*/React__default["default"].forwardRef(function Switch(props, tabRef) {
const {
align,
children,
className,
disabled,
enterDelayMs,
index: index$1,
leaveDelayMs = 0,
name,
onClick = noopFn,
onKeyDown = noopFn,
selected = false,
size,
text,
...other
} = props;
const prefix = usePrefix.usePrefix();
const [isHovered, setIsHovered] = React.useState(false);
const handleClick = e => {
e.preventDefault();
onClick({
index: index$1,
name,
text
});
};
const handleKeyDown = event => {
const key = event.key || event.which;
onKeyDown({
index: index$1,
name,
text,
key
});
};
const handleMouseEnter = () => {
setIsHovered(true);
};
const handleMouseLeave = () => {
setIsHovered(false);
};
const classes = cx__default["default"](className, `${prefix}--content-switcher-btn`, {
[`${prefix}--content-switcher--selected`]: selected
});
const iconButtonClasses = cx__default["default"](`${prefix}--content-switcher-popover__wrapper`, {
[`${prefix}--content-switcher-popover--selected`]: selected,
[`${prefix}--content-switcher-popover--disabled`]: disabled
});
const commonProps = {
onClick: handleClick,
onKeyDown: handleKeyDown,
className: classes,
disabled,
align,
enterDelayMs,
leaveDelayMs,
size
};
return /*#__PURE__*/React__default["default"].createElement(index.IconButton, _rollupPluginBabelHelpers["extends"]({
label: text,
type: "button",
ref: tabRef,
role: "tab",
tabIndex: selected || isHovered ? 0 : -1,
onMouseEnter: handleMouseEnter,
onMouseLeave: handleMouseLeave,
onFocus: handleMouseEnter,
onBlur: handleMouseLeave,
"aria-selected": selected,
"aria-label": text,
wrapperClasses: iconButtonClasses
}, other, commonProps), children);
});
IconSwitch.displayName = 'IconSwitch';
IconSwitch.propTypes = {
/**
* Specify how the trigger should align with the tooltip
*/
align: PropTypes__default["default"].oneOf(['top', 'top-left', 'top-right', 'bottom', 'bottom-left', 'bottom-right', 'left', 'right']),
/**
* Provide child elements to be rendered inside of the Switch
*/
children: PropTypes__default["default"].node,
/**
* Specify an optional className to be added to your Switch
*/
className: PropTypes__default["default"].string,
/**
* Specify whether or not the Switch should be disabled
*/
disabled: PropTypes__default["default"].bool,
/**
* Specify the duration in milliseconds to delay before displaying the tooltip
*/
enterDelayMs: PropTypes__default["default"].number,
/**
* The index of your Switch in your ContentSwitcher that is used for event handlers.
* Reserved for usage in ContentSwitcher
*/
index: PropTypes__default["default"].number,
/**
* Specify the duration in milliseconds to delay before hiding the tooltip
*/
leaveDelayMs: PropTypes__default["default"].number,
/**
* Provide the name of your Switch that is used for event handlers
*/
name: PropTypes__default["default"].oneOfType([PropTypes__default["default"].string, PropTypes__default["default"].number]),
/**
* A handler that is invoked when a user clicks on the control.
* Reserved for usage in ContentSwitcher
*/
onClick: PropTypes__default["default"].func,
/**
* A handler that is invoked on the key down event for the control.
* Reserved for usage in ContentSwitcher
*/
onKeyDown: PropTypes__default["default"].func,
/**
* Whether your Switch is selected. Reserved for usage in ContentSwitcher
*/
selected: PropTypes__default["default"].bool,
/**
* Passed in from `ContentSwitcher` to render icon-only variant
*/
size: PropTypes__default["default"].oneOf(['sm', 'md', 'lg']),
/**
* Provide the visible text displayed by the Tooltip
*/
text: PropTypes__default["default"].string
};
var IconSwitch$1 = IconSwitch;
exports["default"] = IconSwitch$1;