UNPKG

@carbon/ibm-products

Version:
273 lines (271 loc) 9.61 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 { useCreateComponentFocus } from "../../global/js/hooks/useCreateComponentFocus.js"; import { useCreateComponentStepChange } from "../../global/js/hooks/useCreateComponentStepChange.js"; import { usePreviousValue } from "../../global/js/hooks/usePreviousValue.js"; import { getNumberOfHiddenSteps } from "../../global/js/utils/getNumberOfHiddenSteps.js"; import { useResetCreateComponent } from "../../global/js/hooks/useResetCreateComponent.js"; import { useValidCreateStepCount } from "../../global/js/hooks/useValidCreateStepCount.js"; import { getDevtoolsProps } from "../../global/js/utils/devtools.js"; import { deprecateProp } from "../../global/js/utils/props-helper.js"; import { CreateInfluencer } from "../CreateInfluencer/CreateInfluencer.js"; import { lastIndexInArray } from "../../global/js/utils/lastIndexInArray.js"; import { TearsheetShell } from "../Tearsheet/TearsheetShell.js"; import React, { createContext, forwardRef, useEffect, useRef, useState } from "react"; import PropTypes from "prop-types"; import { Form } from "@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__ */ __toESM(require_classnames()); const componentName = "CreateTearsheet"; const blockClass = `${pkg.prefix}--tearsheet-create`; const StepsContext = createContext(null); const StepNumberContext = createContext(-1); const CreateTearsheet = 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] = useState([]); const [shouldViewAll, setShouldViewAll] = useState(false); const [currentStep, setCurrentStep] = useState(0); const [isSubmitting, setIsSubmitting] = useState(false); const [isDisabled, setIsDisabled] = useState(false); const [loadingPrevious, setLoadingPrevious] = useState(false); const [onPrevious, setOnPrevious] = useState(); const [onNext, setOnNext] = useState(); const [onMount, setOnMount] = useState(); const [secondaryButtonDisabled, setSecondaryButtonDisabled] = useState(false); const [stepData, setStepData] = useState([]); const [firstIncludedStep, setFirstIncludedStep] = useState(1); const [lastIncludedStep, setLastIncludedStep] = useState(); const [experimentalSecondarySubmit, setExperimentalSecondarySubmit] = useState(); const previousState = usePreviousValue({ currentStep, open }); const contentRef = useRef(null); useEffect(() => { const firstItem = stepData.findIndex((item) => item?.shouldIncludeStep) + 1; const lastItem = lastIndexInArray(stepData, "shouldIncludeStep", true); if (firstItem !== firstIncludedStep) { setCurrentStep(firstItem); setFirstIncludedStep(firstItem); } if (lastItem !== lastIncludedStep) setLastIncludedStep(lastItem); if (open && initialStep) { const numberOfHiddenSteps = getNumberOfHiddenSteps(stepData, initialStep); setCurrentStep(Number(initialStep + numberOfHiddenSteps)); } }, [ firstIncludedStep, initialStep, lastIncludedStep, open, stepData ]); useCreateComponentFocus({ previousState, currentStep, blockClass: `.${blockClass} .${pkg.prefix}--tearsheet-create__step`, onMount, firstFocusElement: firstFocusElement || selectorPrimaryFocus }); useValidCreateStepCount(stepData.length, componentName); useResetCreateComponent({ firstIncludedStep, previousState, /**@ts-ignore */ open, setCurrentStep, stepData, /**@ts-ignore */ initialStep, totalSteps: stepData?.length, componentName }); 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.createElement(TearsheetShell, { ...getDevtoolsProps(componentName), ...rest, ref, actions: createTearsheetActions, className: (0, import_classnames.default)(blockClass, className), description, influencer: /* @__PURE__ */ React.createElement(CreateInfluencer, { currentStep, stepData }), influencerWidth, label, onClose, open, size: "wide", decorator: decorator || deprecated_slug, title, verticalPosition, closeIconDescription: "", currentStep, hasError, selectorPrimaryFocus }, /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__content`, ref: contentRef }, /* @__PURE__ */ React.createElement(Form, { "aria-label": title, onSubmit: (e) => e.preventDefault() }, /* @__PURE__ */ React.createElement(StepsContext.Provider, { value: { currentStep, setExperimentalSecondarySubmit, setIsDisabled, setOnPrevious: (fn) => setOnPrevious(() => fn), setOnNext: (fn) => setOnNext(() => fn), setOnMount: (fn) => setOnMount(() => fn), setSecondaryButtonDisabled, setStepData, stepData } }, React.Children.toArray(children).filter(Boolean).map((child, index) => /* @__PURE__ */ React.createElement(StepNumberContext.Provider, { value: index + 1, key: index }, child)))))); }); CreateTearsheet.displayName = componentName; const deprecatedProps = { /** * @deprecated Property replaced by `decorator` */ slug: deprecateProp(PropTypes.node, "Property replaced by `decorator`") }; CreateTearsheet.propTypes = { /** * The back button text */ backButtonText: PropTypes.string.isRequired, /** * The cancel button text */ cancelButtonText: PropTypes.string.isRequired, /** * The main content of the tearsheet */ children: PropTypes.node, /** * An optional class or classes to be added to the outermost element. */ className: PropTypes.string, /** * Optional prop that allows you to pass any component. */ decorator: PropTypes.node, /** * A description of the flow, displayed in the header area of the tearsheet. */ description: PropTypes.node, /** * The experimentalSecondary submit button text */ experimentalSecondarySubmitText: PropTypes.string, /** * Specifies elements to focus on first on render. */ firstFocusElement: PropTypes.string, /** * To indicate an error occurred in the Tearsheet step * Used to pass this value to TearsheetShell */ hasError: PropTypes.bool, /** * Used to set the size of the influencer */ influencerWidth: PropTypes.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: PropTypes.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: PropTypes.node, /** * The next button text */ nextButtonText: PropTypes.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: PropTypes.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: PropTypes.func.isRequired, /** * Specifies whether the tearsheet is currently open. */ open: PropTypes.bool, /** * Specify a CSS selector that matches the DOM element that should be * focused when the Modal opens. */ selectorPrimaryFocus: PropTypes.string, /** * The submit button text */ submitButtonText: PropTypes.string.isRequired, /** * The main title of the tearsheet, displayed in the header area. */ title: PropTypes.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: PropTypes.oneOf(["normal", "lower"]), ...deprecatedProps }; //#endregion export { CreateTearsheet, StepNumberContext, StepsContext };