UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

135 lines (130 loc) 5.37 kB
/** * Copyright IBM Corp. 2020, 2025 * * 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 React__default, { useContext, useState } from 'react'; import PropTypes from '../../../_virtual/index.js'; import cx from 'classnames'; import { Close } from '@carbon/react/icons'; import { Section, Heading } from '@carbon/react'; import { ConditionBuilderItem } from '../ConditionBuilderItem/ConditionBuilderItem.js'; import { ConditionBuilderContext } from '../ConditionBuilderContext/ConditionBuilderProvider.js'; import ConditionBuilderAdd from '../ConditionBuilderAdd/ConditionBuilderAdd.js'; import uuidv4 from '../../../global/js/utils/uuidv4.js'; import { ConditionBuilderButton } from '../ConditionBuilderButton/ConditionBuilderButton.js'; import { useTranslations } from '../utils/useTranslations.js'; import { ItemOptionForValueField } from '../ConditionBuilderItem/ConditionBuilderItemOption/ItemOptionForValueField.js'; import { blockClass } from '../utils/util.js'; const ConditionBuilderActions = _ref => { let { actions, className } = _ref; const { actionState = [], setActionState } = useContext(ConditionBuilderContext); const [showDeletionPreview, setShowDeletionPreview] = useState(-1); const [actionsText, thenText, andText, removeActionText, addActionText, actionSectionText] = useTranslations(['actionsText', 'then', 'and', 'removeActionText', 'addActionText', 'actionSectionText']); const addActionHandler = () => { const action = { id: uuidv4(), label: undefined, popoverToOpen: 'valueField' }; setActionState?.([...actionState, action]); }; const onchangeHandler = (selectedId, actionIndex) => { const action = actions.find(action => action.id === selectedId); //fetch the selected action from the input action array // same actions can be added multiple times const newAction = { ...action, id: actionState[actionIndex].id }; setActionState?.([...actionState.slice(0, actionIndex), newAction, ...actionState.slice(actionIndex + 1)]); }; const onRemove = selectedId => { setActionState?.(actionState.filter(action => action.id !== selectedId)); }; const handleShowDeletionPreview = index => { setShowDeletionPreview(index); }; const handleHideDeletionPreview = () => { setShowDeletionPreview(-1); }; return /*#__PURE__*/React__default.createElement("div", { className: className }, /*#__PURE__*/React__default.createElement(Section, { className: `${blockClass}__heading`, level: 4 }, /*#__PURE__*/React__default.createElement(Heading, null, actionsText)), /*#__PURE__*/React__default.createElement("div", { className: `${blockClass}__condition-wrapper`, role: "grid", "aria-label": actionSectionText }, actionState?.map((action, index) => /*#__PURE__*/React__default.createElement("div", { key: action.id, role: "row", className: cx(`${blockClass}__condition-block ${blockClass}__gap ${blockClass}__gap-bottom`, { [`${blockClass}__condition__deletion-preview`]: showDeletionPreview == index }) }, /*#__PURE__*/React__default.createElement(ConditionBuilderItem, { className: `${blockClass}__statement-button`, tabIndex: 0, popOverClassName: `${blockClass}__gap`, label: index === 0 ? thenText : andText }), /*#__PURE__*/React__default.createElement(ConditionBuilderItem, { label: action.label, title: actionsText, condition: action, "data-name": "valueField", type: "option" }, /*#__PURE__*/React__default.createElement(ItemOptionForValueField, { conditionState: { value: action.label }, onChange: selection => onchangeHandler(selection.id, index), config: { options: actions } })), /*#__PURE__*/React__default.createElement("span", { role: "gridcell", "aria-label": removeActionText }, /*#__PURE__*/React__default.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" })), actionState.length === index + 1 && /*#__PURE__*/React__default.createElement(ConditionBuilderAdd, { onClick: addActionHandler, className: `${blockClass}__gap ${blockClass}__gap-left`, buttonLabel: addActionText, tabIndex: 0 }))), actionState.length === 0 && /*#__PURE__*/React__default.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 }; export { ConditionBuilderActions as default };