@carbon/ibm-products
Version:
Carbon for IBM Products
145 lines (143 loc) • 5.82 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 uuidv4 from "../../../global/js/utils/uuidv4.js";
import { blockClass } 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 ConditionBuilderAdd from "../ConditionBuilderAdd/ConditionBuilderAdd.js";
import { ItemOptionForValueField } from "../ConditionBuilderItem/ConditionBuilderItemOption/ItemOptionForValueField.js";
import React, { useContext, useState } from "react";
import PropTypes from "prop-types";
import { Heading, Section } from "@carbon/react";
import { Close } from "@carbon/react/icons";
//#region src/components/ConditionBuilder/ConditionBuilderActions/ConditionBuilderActions.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.
*/
/**@ts-ignore */
var import_classnames = /* @__PURE__ */ __toESM(require_classnames());
const ConditionBuilderActions = ({ actions, className }) => {
const { actionState = [], setActionState, readOnly, onRemoveItem, rootState } = useContext(ConditionBuilderContext);
const [showDeletionPreview, setShowDeletionPreview] = useState(-1);
const [actionsText, thenText, andText, removeActionText, addActionText, actionSectionText] = useTranslations([
"actionsText",
"then",
"and",
"removeActionText",
"addActionText",
"actionSectionText"
]);
const addActionHandler = () => {
if (readOnly) return;
const action = {
id: uuidv4(),
label: void 0,
popoverToOpen: "valueField"
};
setActionState?.([...actionState, action]);
};
const onchangeHandler = (selectedId, actionIndex) => {
const newAction = {
...actions.find((action) => action.id === selectedId),
id: actionState[actionIndex].id
};
setActionState?.([
...actionState.slice(0, actionIndex),
newAction,
...actionState.slice(actionIndex + 1)
]);
};
const onRemove = (selectedId) => {
const actionToRemove = actionState.find((action) => action.id === selectedId);
const { preventRemove } = onRemoveItem?.({
type: "action",
state: rootState,
item: actionToRemove
}) ?? {};
if (!preventRemove) setActionState?.(actionState.filter((action) => action.id !== selectedId));
};
const handleShowDeletionPreview = (index) => {
setShowDeletionPreview(index);
};
const handleHideDeletionPreview = () => {
setShowDeletionPreview(-1);
};
return /* @__PURE__ */ React.createElement("div", { className }, /* @__PURE__ */ React.createElement(Section, {
className: `${blockClass}__heading`,
level: 4
}, /* @__PURE__ */ React.createElement(Heading, null, actionsText)), /* @__PURE__ */ React.createElement("div", {
className: `${blockClass}__condition-wrapper`,
role: "grid",
"aria-label": actionSectionText
}, actionState?.map((action, index) => /* @__PURE__ */ React.createElement("div", {
key: action.id,
role: "row",
className: (0, import_classnames.default)(`${blockClass}__condition-block ${blockClass}__gap ${blockClass}__gap-bottom`, { [`${blockClass}__condition__deletion-preview`]: showDeletionPreview == index })
}, /* @__PURE__ */ React.createElement(ConditionBuilderItem, {
className: `${blockClass}__statement-button`,
tabIndex: 0,
popOverClassName: `${blockClass}__gap ${blockClass}__connector`,
label: index === 0 ? thenText : andText
}), /* @__PURE__ */ React.createElement(ConditionBuilderItem, {
label: action.label,
title: actionsText,
condition: action,
"data-name": "valueField",
type: "option",
popOverClassName: `${blockClass}__action-block`
}, /* @__PURE__ */ React.createElement(ItemOptionForValueField, {
conditionState: { value: action.label },
onChange: (selection) => onchangeHandler(selection.id, index),
config: { options: actions }
})), !readOnly && /* @__PURE__ */ React.createElement("span", {
role: "gridcell",
"aria-label": removeActionText
}, /* @__PURE__ */ React.createElement(ConditionBuilderButton, {
hideLabel: true,
label: removeActionText,
onClick: () => onRemove(action.id),
onMouseEnter: () => handleShowDeletionPreview(index),
onMouseLeave: handleHideDeletionPreview,
onFocus: () => handleShowDeletionPreview(index),
onBlur: handleHideDeletionPreview,
renderIcon: Close,
className: `${blockClass}__close-condition`,
"data-name": "closeCondition"
})), !readOnly && actionState.length === index + 1 && /* @__PURE__ */ React.createElement(ConditionBuilderAdd, {
onClick: addActionHandler,
className: `${blockClass}__gap ${blockClass}__gap-left`,
buttonLabel: addActionText,
tabIndex: 0
}))), actionState.length === 0 && /* @__PURE__ */ React.createElement(ConditionBuilderAdd, {
onClick: addActionHandler,
className: `${blockClass}__gap ${blockClass}__gap-left`,
buttonLabel: addActionText,
tabIndex: 0
})));
};
ConditionBuilderActions.propTypes = {
/**
* optional array of object that give the list of actions.
*/
actions: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
label: PropTypes.string.isRequired
})),
/**
* Provide an optional class to be applied to the containing node.
*/
className: PropTypes.string
};
//#endregion
export { ConditionBuilderActions as default };