@carbon/ibm-products
Version:
Carbon for IBM Products
277 lines (275 loc) • 10.7 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.
*/
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../node_modules/classnames/index.js");
const require_settings = require("../../settings.js");
const require_useCreateComponentFocus = require("../../global/js/hooks/useCreateComponentFocus.js");
const require_useCreateComponentStepChange = require("../../global/js/hooks/useCreateComponentStepChange.js");
const require_usePreviousValue = require("../../global/js/hooks/usePreviousValue.js");
const require_getNumberOfHiddenSteps = require("../../global/js/utils/getNumberOfHiddenSteps.js");
const require_useResetCreateComponent = require("../../global/js/hooks/useResetCreateComponent.js");
const require_useValidCreateStepCount = require("../../global/js/hooks/useValidCreateStepCount.js");
const require_devtools = require("../../global/js/utils/devtools.js");
const require_props_helper = require("../../global/js/utils/props-helper.js");
const require_CreateInfluencer = require("../CreateInfluencer/CreateInfluencer.js");
const require_lastIndexInArray = require("../../global/js/utils/lastIndexInArray.js");
const require_TearsheetShell = require("../Tearsheet/TearsheetShell.js");
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(prop_types);
let _carbon_react = require("@carbon/react");
//#region src/components/CreateTearsheet/CreateTearsheet.tsx
/**
* Copyright IBM Corp. 2021, 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.
*/
var import_classnames = /* @__PURE__ */ require_runtime.__toESM(require_index.default);
const componentName = "CreateTearsheet";
const blockClass = `${require_settings.pkg.prefix}--tearsheet-create`;
const StepsContext = (0, react.createContext)(null);
const StepNumberContext = (0, react.createContext)(-1);
const CreateTearsheet = (0, react.forwardRef)(({ backButtonText, cancelButtonText, children, className, experimentalSecondarySubmitText, firstFocusElement, description, hasError, influencerWidth = "narrow", initialStep, label, nextButtonText, onClose, onRequestSubmit, open, selectorPrimaryFocus, slug: deprecated_slug, decorator, submitButtonText, title, verticalPosition = "normal", ...rest }, ref) => {
const [createTearsheetActions, setCreateTearsheetActions] = (0, react.useState)([]);
const [shouldViewAll, setShouldViewAll] = (0, react.useState)(false);
const [currentStep, setCurrentStep] = (0, react.useState)(0);
const [isSubmitting, setIsSubmitting] = (0, react.useState)(false);
const [isDisabled, setIsDisabled] = (0, react.useState)(false);
const [loadingPrevious, setLoadingPrevious] = (0, react.useState)(false);
const [onPrevious, setOnPrevious] = (0, react.useState)();
const [onNext, setOnNext] = (0, react.useState)();
const [onMount, setOnMount] = (0, react.useState)();
const [secondaryButtonDisabled, setSecondaryButtonDisabled] = (0, react.useState)(false);
const [stepData, setStepData] = (0, react.useState)([]);
const [firstIncludedStep, setFirstIncludedStep] = (0, react.useState)(1);
const [lastIncludedStep, setLastIncludedStep] = (0, react.useState)();
const [experimentalSecondarySubmit, setExperimentalSecondarySubmit] = (0, react.useState)();
const previousState = require_usePreviousValue.usePreviousValue({
currentStep,
open
});
const contentRef = (0, react.useRef)(null);
(0, react.useEffect)(() => {
const firstItem = stepData.findIndex((item) => item?.shouldIncludeStep) + 1;
const lastItem = require_lastIndexInArray.lastIndexInArray(stepData, "shouldIncludeStep", true);
if (firstItem !== firstIncludedStep) {
setCurrentStep(firstItem);
setFirstIncludedStep(firstItem);
}
if (lastItem !== lastIncludedStep) setLastIncludedStep(lastItem);
if (open && initialStep) {
const numberOfHiddenSteps = require_getNumberOfHiddenSteps.getNumberOfHiddenSteps(stepData, initialStep);
setCurrentStep(Number(initialStep + numberOfHiddenSteps));
}
}, [
firstIncludedStep,
initialStep,
lastIncludedStep,
open,
stepData
]);
require_useCreateComponentFocus.useCreateComponentFocus({
previousState,
currentStep,
blockClass: `.${blockClass} .${require_settings.pkg.prefix}--tearsheet-create__step`,
onMount,
firstFocusElement: firstFocusElement || selectorPrimaryFocus
});
require_useValidCreateStepCount.useValidCreateStepCount(stepData.length, componentName);
require_useResetCreateComponent.useResetCreateComponent({
firstIncludedStep,
previousState,
/**@ts-ignore */
open,
setCurrentStep,
stepData,
/**@ts-ignore */
initialStep,
totalSteps: stepData?.length,
componentName
});
require_useCreateComponentStepChange.useCreateComponentStepChange({
firstIncludedStep,
lastIncludedStep,
stepData,
onPrevious,
onNext,
isSubmitDisabled: isDisabled,
setCurrentStep,
setIsSubmitting,
setShouldViewAll,
/**@ts-ignore */
setLoadingPrevious,
loadingPrevious,
onClose,
onRequestSubmit,
componentName,
currentStep,
shouldViewAll,
backButtonText,
cancelButtonText,
submitButtonText,
nextButtonText,
isSubmitting,
secondaryButtonDisabled,
componentBlockClass: blockClass,
experimentalSecondarySubmit,
experimentalSecondarySubmitText: experimentalSecondarySubmit?.labelText ? experimentalSecondarySubmit.labelText : experimentalSecondarySubmitText,
setCreateComponentActions: setCreateTearsheetActions
});
return /* @__PURE__ */ react.default.createElement(require_TearsheetShell.TearsheetShell, {
...require_devtools.getDevtoolsProps(componentName),
...rest,
ref,
actions: createTearsheetActions,
className: (0, import_classnames.default)(blockClass, className),
description,
influencer: /* @__PURE__ */ react.default.createElement(require_CreateInfluencer.CreateInfluencer, {
currentStep,
stepData
}),
influencerWidth,
label,
onClose,
open,
size: "wide",
decorator: decorator || deprecated_slug,
title,
verticalPosition,
closeIconDescription: "",
currentStep,
hasError,
selectorPrimaryFocus
}, /* @__PURE__ */ react.default.createElement("div", {
className: `${blockClass}__content`,
ref: contentRef
}, /* @__PURE__ */ react.default.createElement(_carbon_react.Form, {
"aria-label": title,
onSubmit: (e) => e.preventDefault()
}, /* @__PURE__ */ react.default.createElement(StepsContext.Provider, { value: {
currentStep,
setExperimentalSecondarySubmit,
setIsDisabled,
setOnPrevious: (fn) => setOnPrevious(() => fn),
setOnNext: (fn) => setOnNext(() => fn),
setOnMount: (fn) => setOnMount(() => fn),
setSecondaryButtonDisabled,
setStepData,
stepData
} }, react.default.Children.toArray(children).filter(Boolean).map((child, index) => /* @__PURE__ */ react.default.createElement(StepNumberContext.Provider, {
value: index + 1,
key: index
}, child))))));
});
CreateTearsheet.displayName = componentName;
const deprecatedProps = {
/**
* @deprecated Property replaced by `decorator`
*/
slug: require_props_helper.deprecateProp(prop_types.default.node, "Property replaced by `decorator`") };
CreateTearsheet.propTypes = {
/**
* The back button text
*/
backButtonText: prop_types.default.string.isRequired,
/**
* The cancel button text
*/
cancelButtonText: prop_types.default.string.isRequired,
/**
* The main content of the tearsheet
*/
children: prop_types.default.node,
/**
* An optional class or classes to be added to the outermost element.
*/
className: prop_types.default.string,
/**
* Optional prop that allows you to pass any component.
*/
decorator: prop_types.default.node,
/**
* A description of the flow, displayed in the header area of the tearsheet.
*/
description: prop_types.default.node,
/**
* The experimentalSecondary submit button text
*/
experimentalSecondarySubmitText: prop_types.default.string,
/**
* Specifies elements to focus on first on render.
*/
firstFocusElement: prop_types.default.string,
/**
* To indicate an error occurred in the Tearsheet step
* Used to pass this value to TearsheetShell
*/
hasError: prop_types.default.bool,
/**
* Used to set the size of the influencer
*/
influencerWidth: prop_types.default.oneOf(["narrow", "wide"]),
/**
* This can be used to open the component to a step other than the first step.
* For example, a create flow was previously in progress, data was saved, and
* is now being completed.
*/
initialStep: prop_types.default.number,
/**
* A label for the tearsheet, displayed in the header area of the tearsheet
* to maintain context for the tearsheet (e.g. as the title changes from page
* to page of a multi-page task).
*/
label: prop_types.default.node,
/**
* The next button text
*/
nextButtonText: prop_types.default.string.isRequired,
/**
* An optional handler that is called when the user closes the tearsheet (by
* clicking the close button, if enabled, or clicking outside, if enabled).
* Returning `false` here prevents the modal from closing.
*/
onClose: prop_types.default.func,
/**
* Specify a handler for submitting the multi step tearsheet (final step).
* This function can _optionally_ return a promise that is either resolved or rejected and the CreateTearsheet will handle the submitting state of the create button.
*/
onRequestSubmit: prop_types.default.func.isRequired,
/**
* Specifies whether the tearsheet is currently open.
*/
open: prop_types.default.bool,
/**
* Specify a CSS selector that matches the DOM element that should be
* focused when the Modal opens.
*/
selectorPrimaryFocus: prop_types.default.string,
/**
* The submit button text
*/
submitButtonText: prop_types.default.string.isRequired,
/**
* The main title of the tearsheet, displayed in the header area.
*/
title: prop_types.default.node,
/**
* The position of the top of tearsheet in the viewport. The 'normal'
* position (the default) is a short distance down from the top of the
* viewport, leaving room at the top for a global header bar to show through
* from below. The 'lower' position provides a little extra room at the top
* to allow an action bar navigation or breadcrumbs to also show through.
*/
verticalPosition: prop_types.default.oneOf(["normal", "lower"]),
...deprecatedProps
};
//#endregion
exports.CreateTearsheet = CreateTearsheet;
exports.StepNumberContext = StepNumberContext;
exports.StepsContext = StepsContext;