UNPKG

@carbon/ibm-products

Version:
156 lines (154 loc) 5.34 kB
/** * 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 { pkg } from "../../settings.js"; import { usePreviousValue } from "../../global/js/hooks/usePreviousValue.js"; import { getDevtoolsProps } from "../../global/js/utils/devtools.js"; import { usePortalTarget } from "../../global/js/hooks/usePortalTarget.js"; import uuidv4 from "../../global/js/utils/uuidv4.js"; import React, { forwardRef, useEffect, useRef, useState } from "react"; import PropTypes from "prop-types"; import { Button, ComposedModal, ModalBody, ModalFooter, ModalHeader, TextInput } from "@carbon/react"; //#region src/components/RemoveModal/RemoveModal.tsx var import_classnames = /* @__PURE__ */ __toESM(require_classnames()); const componentName = "RemoveModal"; const RemoveModal = forwardRef(({ body, className, iconDescription, inputInvalidText, inputLabelText, inputPlaceholderText, label, onClose, onRequestSubmit, open, portalTarget: portalTargetIn, preventCloseOnClickOutside, primaryButtonDisabled, primaryButtonText, primaryDangerDescription, resourceName, secondaryButtonText, textConfirmation, title, ...rest }, ref) => { const previousState = usePreviousValue({ open }); const [userInput, setUserInput] = useState(""); const idRef = useRef(uuidv4()); const renderPortalUse = usePortalTarget(portalTargetIn); const onChangeHandler = (e) => { setUserInput(e.target.value); }; const checkPrimaryButtonDisabled = () => { if (primaryButtonDisabled) return true; else if (textConfirmation && userInput !== resourceName) return true; return false; }; const primaryButtonStatus = checkPrimaryButtonDisabled(); const blockClass = `${pkg.prefix}--remove-modal`; useEffect(() => { if (!open && previousState?.open) setUserInput(""); }, [open, previousState?.open]); return renderPortalUse(/* @__PURE__ */ React.createElement(ComposedModal, { ...rest, className: (0, import_classnames.default)(blockClass, className), size: "sm", "aria-label": title, open, ref, preventCloseOnClickOutside, onClose, ...getDevtoolsProps(componentName) }, /* @__PURE__ */ React.createElement(ModalHeader, { title, label, iconDescription }), /* @__PURE__ */ React.createElement(ModalBody, null, typeof body === "string" ? /* @__PURE__ */ React.createElement("p", { className: `${blockClass}__body` }, body) : body, textConfirmation && /* @__PURE__ */ React.createElement(TextInput, { id: `${idRef.current}-confirmation-input`, className: `${blockClass}__input`, invalidText: inputInvalidText, labelText: inputLabelText, placeholder: inputPlaceholderText, onChange: onChangeHandler, value: userInput, "data-modal-primary-focus": true })), /* @__PURE__ */ React.createElement(ModalFooter, null, /* @__PURE__ */ React.createElement(Button, { type: "button", kind: "secondary", onClick: onClose, "data-modal-primary-focus": !textConfirmation }, secondaryButtonText), /* @__PURE__ */ React.createElement(Button, { type: "submit", kind: "danger", dangerDescription: primaryDangerDescription, onClick: onRequestSubmit, disabled: primaryButtonStatus }, primaryButtonText)))); }); RemoveModal.propTypes = { /** * The content to be displayed in the body of the modal */ body: PropTypes.node.isRequired, /** * Optional classname */ className: PropTypes.string, /** * Provide a description for "close" icon that can be read by screen readers */ iconDescription: PropTypes.string.isRequired, /** * Message showed when user input fails validation */ inputInvalidText: PropTypes.string, /** * Label for text box */ inputLabelText: PropTypes.node, /** * Placeholder for text box */ inputPlaceholderText: PropTypes.string, /** * Specify the modal label texts */ label: PropTypes.string, /** * Callback function that runs when user closes the modal */ onClose: PropTypes.func, /** * Callback function that runs when user submits the modal */ onRequestSubmit: PropTypes.func, /** * Specify whether the Modal is currently open */ open: PropTypes.bool.isRequired, /** * The DOM node the tearsheet should be rendered within. Defaults to document.body. */ portalTarget: PropTypes.node, /** * Prevent closing on click outside of modal */ preventCloseOnClickOutside: PropTypes.bool, /** * Specify whether the primary button should be disabled. This value will override textConfirmation */ primaryButtonDisabled: PropTypes.bool, /** * Specify the text for the primary button */ primaryButtonText: PropTypes.string, /** * Specify the danger description on the primary button */ primaryDangerDescription: PropTypes.string, /** * The name of the resource being acted upon */ resourceName: PropTypes.string.isRequired, /** * Specify the text for the secondary button */ secondaryButtonText: PropTypes.string, /** * Specify whether or not to show the text confirmation input */ textConfirmation: PropTypes.bool, /** * The text displayed at the top of the modal */ title: PropTypes.string.isRequired }; RemoveModal.displayName = componentName; //#endregion export { RemoveModal };