@carbon/ibm-products
Version:
Carbon for IBM Products
156 lines (149 loc) • 5.45 kB
JavaScript
/**
* 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 { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import { ComposedModal, ModalHeader, ModalBody, Form, ModalFooter, Button } from '@carbon/react';
import React__default from 'react';
import PropTypes from '../../_virtual/index.js';
import cx from 'classnames';
import { getDevtoolsProps } from '../../global/js/utils/devtools.js';
import { pkg } from '../../settings.js';
import { usePortalTarget } from '../../global/js/hooks/usePortalTarget.js';
const componentName = 'CreateModal';
const blockClass = `${pkg.prefix}--create-modal`;
// Custom PropType validator which checks and ensures that the children property has no more than 4 nodes
const isValidChildren = () => _ref => {
let {
children
} = _ref;
if (children && children.length > 4) {
throw new Error('The `CreateModal` component does not take more than 4 nodes as children. This is to ensure that the modal does not overflow. Please remove 1 or more nodes.');
}
return;
};
/**
* The `CreateModal` component provides a way for a user to quickly generate a new
resource. It is triggered by a user’s action, appears on top of the main page
content, and is persistent until dismissed. The purpose of this modal should be
immediately apparent to the user, with a clear and obvious path to completion.
*/
let CreateModal = /*#__PURE__*/React__default.forwardRef((_ref2, ref) => {
let {
// The component props, in alphabetical order (for consistency).
className,
children,
onRequestClose,
onRequestSubmit,
open,
title,
subtitle,
description,
secondaryButtonText,
portalTarget: portalTargetIn,
primaryButtonText,
disableSubmit,
selectorPrimaryFocus,
// Collect any other property values passed in.
...rest
} = _ref2;
const renderPortalUse = usePortalTarget(portalTargetIn);
return renderPortalUse(/*#__PURE__*/React__default.createElement(ComposedModal, _extends({}, rest, {
selectorPrimaryFocus: selectorPrimaryFocus,
className: cx(blockClass, className),
open,
ref,
"aria-label": title,
size: "sm",
preventCloseOnClickOutside: true,
onClose: () => {
onRequestClose?.();
return false;
}
}, getDevtoolsProps(componentName)), /*#__PURE__*/React__default.createElement(ModalHeader, {
title: title,
titleClassName: `${blockClass}__title`
}, subtitle && /*#__PURE__*/React__default.createElement("p", {
className: `${blockClass}__subtitle`
}, subtitle)), /*#__PURE__*/React__default.createElement(ModalBody, {
hasForm: true
}, description && /*#__PURE__*/React__default.createElement("p", {
className: `${blockClass}__description`
}, description), /*#__PURE__*/React__default.createElement(Form, {
className: `${blockClass}__form`,
"aria-label": title
}, children)), /*#__PURE__*/React__default.createElement(ModalFooter, null, /*#__PURE__*/React__default.createElement(Button, {
type: "button",
kind: "secondary",
onClick: onRequestClose
}, secondaryButtonText), /*#__PURE__*/React__default.createElement(Button, {
type: "submit",
kind: "primary",
onClick: onRequestSubmit,
disabled: disableSubmit
}, primaryButtonText))));
});
// Return a placeholder if not released and not enabled by feature flag
CreateModal = pkg.checkComponentEnabled(CreateModal, componentName);
CreateModal.propTypes = {
/**
* Children refers to all form items within a form inside of the modal's body.
*/
/**@ts-ignore*/
children: isValidChildren(),
/**
* Specify an optional className to be applied to the modal root node
*/
className: PropTypes.string,
/**
* The description of the CreateModal serves to provide more information about the modal.
*/
description: PropTypes.node.isRequired,
/**
* Specifies a boolean for disabling or enabling the primary button. This is important for form validation
* Returning `true` prevents the primary button from being clicked until required fields are completed.
*/
disableSubmit: PropTypes.bool,
/**
* Specifies an optional handler which is called when the CreateModal
* is closed.
*/
onRequestClose: PropTypes.func,
/**
* Specifies an optional handler which is called when the CreateModal
* primary button is pressed.
*/
onRequestSubmit: PropTypes.func,
/**
* Specifies whether the CreateModal is open or not.
*/
open: PropTypes.bool,
/**
* The DOM node the tearsheet should be rendered within. Defaults to document.body.
*/
portalTarget: PropTypes.node,
/**
* Specifies the primary button's text in the modal.
*/
primaryButtonText: PropTypes.string.isRequired,
/**
* Specifies the secondary button's text in the modal.
*/
secondaryButtonText: PropTypes.string.isRequired,
/**
* Specifies which DOM element in the form should be focused.
*/
selectorPrimaryFocus: PropTypes.string.isRequired,
/**
* The subtitle of the CreateModal is optional and serves to provide more information about the modal.
*/
subtitle: PropTypes.node,
/**
* The title of the CreateModal is usually the product or service name.
*/
title: PropTypes.node.isRequired
};
CreateModal.displayName = componentName;
export { CreateModal };