@carbon/ibm-products
Version:
Carbon for IBM Products
118 lines (116 loc) • 3.78 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 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 { __toESM } from "../../../_virtual/_rolldown/runtime.js";
import { require_classnames } from "../../../node_modules/classnames/index.js";
import { blockClass } from "../utils/util.js";
import { ConditionBuilderContext } from "../ConditionBuilderContext/ConditionBuilderProvider.js";
import React, { useContext } from "react";
import PropTypes from "prop-types";
import { Tooltip, usePrefix } from "@carbon/react";
import { WarningAltFilled } from "@carbon/react/icons";
//#region src/components/ConditionBuilder/ConditionBuilderButton/ConditionBuilderButton.tsx
/**
* Copyright IBM Corp. 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
var import_classnames = /* @__PURE__ */ __toESM(require_classnames());
const ConditionBuilderButton = ({ className, label, hideLabel, tooltipAlign, renderIcon: Icon, onClick, showToolTip, wrapperProps, onBlur, onFocus, onMouseEnter, onMouseLeave, isInvalid, wrapperClassName, tabIndex, description, ...rest }) => {
const tooltipText = description || label;
const carbonPrefix = usePrefix();
const Button = () => {
const dataName = rest["data-name"] ?? "";
const { readOnly } = useContext(ConditionBuilderContext);
const handleClick = (e) => {
if (readOnly) return;
onClick?.(e);
};
return /* @__PURE__ */ React.createElement("button", {
tabIndex: tabIndex !== void 0 ? tabIndex : -1,
className: (0, import_classnames.default)([
className,
`${blockClass}__button`,
{ [`${blockClass}__text-ellipsis`]: showToolTip && !hideLabel && !isInvalid },
{ [`${blockClass}__invalid-input`]: isInvalid }
]),
type: "button",
onClick: handleClick,
onBlur,
onFocus,
onMouseEnter,
onMouseLeave,
"data-name": dataName,
"aria-disabled": readOnly,
...rest
}, Icon && /* @__PURE__ */ React.createElement(Icon, null), !hideLabel && /* @__PURE__ */ React.createElement("span", null, label), isInvalid && /* @__PURE__ */ React.createElement(WarningAltFilled, null));
};
return hideLabel || showToolTip || description ? /* @__PURE__ */ React.createElement(Tooltip, {
label: tooltipText,
align: tooltipAlign,
className: `${wrapperClassName} ${blockClass}__tooltip ${carbonPrefix}--icon-tooltip`,
...wrapperProps,
leaveDelayMs: 0
}, Button()) : /* @__PURE__ */ React.createElement(React.Fragment, null, Button());
};
ConditionBuilderButton.propTypes = {
/**
* Provide an optional class to be applied to the containing node.
*/
className: PropTypes.string,
/**
* decides if label and tooltip to be hidden
*/
hideLabel: PropTypes.bool,
/**
* boolean to know the updated value in not valid
*/
isInvalid: PropTypes.bool,
/**
* label of the button
*/
label: PropTypes.string,
/**
* mouse events callbacks
*/
onBlur: PropTypes.func,
/**
* callback triggered on click of add button
*/
onClick: PropTypes.func,
onFocus: PropTypes.func,
onMouseEnter: PropTypes.func,
onMouseLeave: PropTypes.func,
/**
* Optional prop to allow overriding the icon rendering.
*/
renderIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
/**
*/
/**
*decides if tooltip to be shown
*/
showToolTip: PropTypes.bool,
/**
* Tab index
*/
tabIndex: PropTypes.number,
/**
* tooltip position
*/
tooltipAlign: PropTypes.string,
/**
* classname applies to the wrapper of popover
*/
wrapperClassName: PropTypes.string,
/**
* optional props for tree grid to add role and aria-label to wrapper span
*/
wrapperProps: PropTypes.object
};
//#endregion
export { ConditionBuilderButton };