@carbon/ibm-products
Version:
Carbon for IBM Products
80 lines (78 loc) • 3.22 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 { blockClass, focusThisField } from "../utils/util.js";
import { ConditionBuilderContext } from "../ConditionBuilderContext/ConditionBuilderProvider.js";
import { ConditionBuilderButton } from "../ConditionBuilderButton/ConditionBuilderButton.js";
import { useTranslations } from "../utils/useTranslations.js";
import { ConditionBuilderItem } from "../ConditionBuilderItem/ConditionBuilderItem.js";
import { ItemOption } from "../ConditionBuilderItem/ConditionBuilderItemOption/ItemOption.js";
import { useDataConfigs } from "../utils/useDataConfigs.js";
import React, { useCallback, useContext } from "react";
import PropTypes from "prop-types";
//#region src/components/ConditionBuilder/ConditionBuilderConnector/ConditionConnector.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.
*/
const ConditionConnector = ({ operator, className, onChange, ...rest }) => {
const { variant, conditionBuilderRef } = useContext(ConditionBuilderContext);
const [connectorText] = useTranslations(["connectorText"]);
const { connectorConfig } = useDataConfigs();
const handleConnectorHover = useCallback((parentGroup, isHover) => {
if (isHover) parentGroup.classList.add("hoveredConnector");
else parentGroup.classList.remove("hoveredConnector");
}, []);
const activeConnectorHandler = (evt) => {
const parentGroup = evt?.currentTarget.closest(".eachGroup");
handleConnectorHover(parentGroup, true);
};
const inActiveConnectorHandler = (evt) => {
const parentGroup = evt?.currentTarget.closest(".eachGroup");
handleConnectorHover(parentGroup, false);
};
const onChangeHandler = (op, evt) => {
onChange?.(op);
focusThisField(evt, conditionBuilderRef);
};
return variant == "Hierarchical" ? /* @__PURE__ */ React.createElement("span", { className: `${className} ${blockClass}__connector ${blockClass}__connector--disabled` }, /* @__PURE__ */ React.createElement(ConditionBuilderButton, { label: operator })) : /* @__PURE__ */ React.createElement(ConditionBuilderItem, {
label: operator,
title: connectorText,
"data-name": "connectorField",
onMouseEnter: activeConnectorHandler,
onMouseLeave: inActiveConnectorHandler,
onFocus: activeConnectorHandler,
onBlur: inActiveConnectorHandler,
...rest,
popOverClassName: `${className} ${blockClass}__connector`,
className: `${blockClass}__connector-button`
}, /* @__PURE__ */ React.createElement(ItemOption, {
config: { options: connectorConfig },
conditionState: {
value: operator,
label: connectorText
},
onChange: onChangeHandler
}));
};
ConditionConnector.propTypes = {
/**
* Provide an optional class to be applied to the containing node.
*/
className: PropTypes.string,
/**
* callback to update the current condition of the state tree
*/
onChange: PropTypes.func,
/**
* string that defines the connector operator (and/or)
*/
operator: PropTypes.string
};
//#endregion
export { ConditionConnector as default };