@carbon/ibm-products
Version:
Carbon for IBM Products
156 lines (149 loc) • 5.48 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.
*/
'use strict';
var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var react = require('@carbon/react');
var React = require('react');
var index = require('../../_virtual/index.js');
var cx = require('classnames');
var devtools = require('../../global/js/utils/devtools.js');
var settings = require('../../settings.js');
var usePortalTarget = require('../../global/js/hooks/usePortalTarget.js');
const componentName = 'CreateModal';
const blockClass = `${settings.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.
*/
exports.CreateModal = /*#__PURE__*/React.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.usePortalTarget(portalTargetIn);
return renderPortalUse(/*#__PURE__*/React.createElement(react.ComposedModal, _rollupPluginBabelHelpers.extends({}, rest, {
selectorPrimaryFocus: selectorPrimaryFocus,
className: cx(blockClass, className),
open,
ref,
"aria-label": title,
size: "sm",
preventCloseOnClickOutside: true,
onClose: () => {
onRequestClose?.();
return false;
}
}, devtools.getDevtoolsProps(componentName)), /*#__PURE__*/React.createElement(react.ModalHeader, {
title: title,
titleClassName: `${blockClass}__title`
}, subtitle && /*#__PURE__*/React.createElement("p", {
className: `${blockClass}__subtitle`
}, subtitle)), /*#__PURE__*/React.createElement(react.ModalBody, {
hasForm: true
}, description && /*#__PURE__*/React.createElement("p", {
className: `${blockClass}__description`
}, description), /*#__PURE__*/React.createElement(react.Form, {
className: `${blockClass}__form`,
"aria-label": title
}, children)), /*#__PURE__*/React.createElement(react.ModalFooter, null, /*#__PURE__*/React.createElement(react.Button, {
type: "button",
kind: "secondary",
onClick: onRequestClose
}, secondaryButtonText), /*#__PURE__*/React.createElement(react.Button, {
type: "submit",
kind: "primary",
onClick: onRequestSubmit,
disabled: disableSubmit
}, primaryButtonText))));
});
// Return a placeholder if not released and not enabled by feature flag
exports.CreateModal = settings.pkg.checkComponentEnabled(exports.CreateModal, componentName);
exports.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: index.default.string,
/**
* The description of the CreateModal serves to provide more information about the modal.
*/
description: index.default.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: index.default.bool,
/**
* Specifies an optional handler which is called when the CreateModal
* is closed.
*/
onRequestClose: index.default.func,
/**
* Specifies an optional handler which is called when the CreateModal
* primary button is pressed.
*/
onRequestSubmit: index.default.func,
/**
* Specifies whether the CreateModal is open or not.
*/
open: index.default.bool,
/**
* The DOM node the tearsheet should be rendered within. Defaults to document.body.
*/
portalTarget: index.default.node,
/**
* Specifies the primary button's text in the modal.
*/
primaryButtonText: index.default.string.isRequired,
/**
* Specifies the secondary button's text in the modal.
*/
secondaryButtonText: index.default.string.isRequired,
/**
* Specifies which DOM element in the form should be focused.
*/
selectorPrimaryFocus: index.default.string.isRequired,
/**
* The subtitle of the CreateModal is optional and serves to provide more information about the modal.
*/
subtitle: index.default.node,
/**
* The title of the CreateModal is usually the product or service name.
*/
title: index.default.node.isRequired
};
exports.CreateModal.displayName = componentName;