UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

300 lines (290 loc) 10.5 kB
/** * 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 React__default, { forwardRef, useState, useRef, useEffect, createContext } from 'react'; import { deprecateProp } from '../../global/js/utils/props-helper.js'; import { Form } from '@carbon/react'; import PropTypes from '../../_virtual/index.js'; import { TearsheetShell } from '../Tearsheet/TearsheetShell.js'; import cx from 'classnames'; import { getDevtoolsProps } from '../../global/js/utils/devtools.js'; import { getNumberOfHiddenSteps } from '../../global/js/utils/getNumberOfHiddenSteps.js'; import { lastIndexInArray } from '../../global/js/utils/lastIndexInArray.js'; import { pkg } from '../../settings.js'; import { usePreviousValue } from '../../global/js/hooks/usePreviousValue.js'; import { useCreateComponentFocus } from '../../global/js/hooks/useCreateComponentFocus.js'; import { useValidCreateStepCount } from '../../global/js/hooks/useValidCreateStepCount.js'; import { useResetCreateComponent } from '../../global/js/hooks/useResetCreateComponent.js'; import { useCreateComponentStepChange } from '../../global/js/hooks/useCreateComponentStepChange.js'; import { CreateInfluencer } from '../CreateInfluencer/CreateInfluencer.js'; const componentName = 'CreateTearsheet'; const blockClass = `${pkg.prefix}--tearsheet-create`; // This is a general context for the steps container // containing information about the state of the container // and providing some callback methods for steps to use const StepsContext = /*#__PURE__*/createContext(null); // This is a context supplied separately to each step in the container // to let it know what number it is in the sequence of steps const StepNumberContext = /*#__PURE__*/createContext(-1); let CreateTearsheet = /*#__PURE__*/forwardRef((_ref, ref) => { let { // The component props, in alphabetical order (for consistency). 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', // Collect any other property values passed in. ...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 [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 === true) + 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, onMount, firstFocusElement }); 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, /**@ts-ignore */ setLoadingPrevious, loadingPrevious, onClose, onRequestSubmit, componentName, currentStep, backButtonText, cancelButtonText, submitButtonText, nextButtonText, isSubmitting, componentBlockClass: blockClass, experimentalSecondarySubmit, experimentalSecondarySubmitText: experimentalSecondarySubmit?.labelText ? experimentalSecondarySubmit.labelText : experimentalSecondarySubmitText, setCreateComponentActions: setCreateTearsheetActions }); return /*#__PURE__*/React__default.createElement(TearsheetShell, _extends({}, getDevtoolsProps(componentName), rest, { ref, actions: createTearsheetActions, className: cx(blockClass, className), description, influencer: /*#__PURE__*/React__default.createElement(CreateInfluencer, { currentStep: currentStep, stepData: stepData }), influencerWidth, label, onClose, open, size: 'wide', decorator: decorator || deprecated_slug, title, verticalPosition, closeIconDescription: '', currentStep: currentStep, hasError: hasError, selectorPrimaryFocus: selectorPrimaryFocus }), /*#__PURE__*/React__default.createElement("div", { className: `${blockClass}__content`, ref: contentRef }, /*#__PURE__*/React__default.createElement(Form, { "aria-label": title }, /*#__PURE__*/React__default.createElement(StepsContext.Provider, { value: { currentStep, setExperimentalSecondarySubmit, setIsDisabled, setOnPrevious: fn => setOnPrevious(() => fn), setOnNext: fn => setOnNext(() => fn), setOnMount: fn => setOnMount(() => fn), setStepData, stepData } }, React__default.Children.toArray(children).filter(Boolean).map((child, index) => /*#__PURE__*/React__default.createElement(StepNumberContext.Provider, { value: index + 1, key: index }, child)))))); }); // Return a placeholder if not released and not enabled by feature flag CreateTearsheet = pkg.checkComponentEnabled(CreateTearsheet, componentName); // The display name of the component, used by React. Note that displayName // is used in preference to relying on function.name. CreateTearsheet.displayName = componentName; const deprecatedProps = { /** * @deprecated Property replaced by `decorator` */ slug: deprecateProp(PropTypes.node, 'Property replaced by `decorator`') }; // Note that the descriptions here should be kept in sync with those for the // corresponding props for TearsheetNarrow and TearsheetShell components. 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 }; export { CreateTearsheet, StepNumberContext, StepsContext };