UNPKG

@carbon/ibm-products

Version:
160 lines (158 loc) 4.97 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. */ require("../../../_virtual/_rolldown/runtime.js"); let react = require("react"); //#region src/global/js/hooks/useCreateComponentStepChange.js /** * Copyright IBM Corp. 2021, 2023 * * 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 useCreateComponentStepChange = ({ firstIncludedStep, lastIncludedStep, stepData, onPrevious, onNext, isSubmitDisabled, setCurrentStep, setIsSubmitting, setLoadingPrevious = null, loadingPrevious = false, onClose, onRequestSubmit, componentName, currentStep, backButtonText, cancelButtonText, submitButtonText, nextButtonText, isSubmitting, secondaryButtonDisabled = false, componentBlockClass, setCreateComponentActions, setModalIsOpen, experimentalSecondarySubmit, experimentalSecondarySubmitText }) => { const continueToNextStep = (0, react.useCallback)(() => { setIsSubmitting(false); setCurrentStep((prev) => { do prev++; while (!stepData[prev - 1]?.shouldIncludeStep); return prev; }); }, [ setCurrentStep, setIsSubmitting, stepData ]); const moveToPreviousStep = (0, react.useCallback)(() => { if (componentName !== "CreateFullPage") setLoadingPrevious(false); setCurrentStep((prev) => { do prev--; while (!stepData[prev - 1]?.shouldIncludeStep); return prev; }); }, [ setCurrentStep, stepData, setLoadingPrevious, componentName ]); (0, react.useEffect)(() => { const onUnmount = () => { if (componentName !== "CreateFullPage") setCurrentStep(1); setIsSubmitting(false); onClose(); }; const handleOnRequestSubmit = async () => { try { if (!(await onRequestSubmit())?.preventClose) onUnmount(); } catch (error) { setIsSubmitting(false); console.warn(`${componentName} submit error: ${error}`); } }; const handlePrevious = async () => { setLoadingPrevious?.(true); if (typeof onPrevious === "function") try { await onPrevious(); moveToPreviousStep(); } catch (error) { console.warn(`${componentName} onBack error: ${error}`); } else moveToPreviousStep(); }; const handleNext = async () => { setIsSubmitting(true); if (typeof onNext === "function") try { await onNext(); continueToNextStep(); } catch (error) { setIsSubmitting(false); console.warn(`${componentName} onNext error: ${error}`); } else continueToNextStep(); }; const handleSubmit = async () => { setIsSubmitting(true); if (typeof onNext === "function") try { await onNext(); await handleOnRequestSubmit(); } catch (error) { setIsSubmitting(false); console.warn(`${componentName} onNext error: ${error}`); } else await handleOnRequestSubmit(); }; const handleExperimentalSecondarySubmit = () => { if (typeof experimentalSecondarySubmit?.onClick === "function") experimentalSecondarySubmit.onClick(); }; if (stepData?.length > 0) { const buttons = []; if (stepData?.length > 1) buttons.push({ key: "create-action-button-back", label: backButtonText, onClick: handlePrevious, kind: "secondary", disabled: currentStep === firstIncludedStep || secondaryButtonDisabled, loading: loadingPrevious }); buttons.push({ key: "create-action-button-cancel", label: cancelButtonText, onClick: componentName === "CreateFullPage" ? () => setModalIsOpen(true) : onUnmount, kind: "ghost" }); if (experimentalSecondarySubmitText && !experimentalSecondarySubmit?.hideSecondarySubmit) buttons.push({ key: "create-action-button-experimentalSecondarySubmit", label: experimentalSecondarySubmitText, onClick: handleExperimentalSecondarySubmit, kind: "secondary", disabled: experimentalSecondarySubmit?.disabled }); buttons.push({ key: "create-action-button-submit", label: currentStep < lastIncludedStep ? nextButtonText : submitButtonText, onClick: currentStep < lastIncludedStep ? handleNext : handleSubmit, disabled: isSubmitDisabled, kind: "primary", loading: isSubmitting, className: `${componentBlockClass}__create-button` }); setCreateComponentActions(buttons); } }, [ firstIncludedStep, lastIncludedStep, stepData, onNext, isSubmitDisabled, backButtonText, cancelButtonText, currentStep, onClose, nextButtonText, submitButtonText, onRequestSubmit, isSubmitting, secondaryButtonDisabled, componentBlockClass, componentName, continueToNextStep, setCurrentStep, setCreateComponentActions, setIsSubmitting, setModalIsOpen, moveToPreviousStep, onPrevious, setLoadingPrevious, loadingPrevious, experimentalSecondarySubmit, experimentalSecondarySubmitText ]); }; //#endregion exports.useCreateComponentStepChange = useCreateComponentStepChange;