UNPKG

@carbon/ibm-products

Version:
52 lines (50 loc) 2.26 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"); const require_getNumberOfHiddenSteps = require("../utils/getNumberOfHiddenSteps.js"); let react = require("react"); //#region src/global/js/hooks/useResetCreateComponent.js /** * Copyright IBM Corp. 2021, 2022 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ /** * Resets the current step of the create component if it has been closed. * @param {object} useResetCreateComponent - Create component that uses this custom hook * @param {object} useResetCreateComponent.firstIncludedStep * @param {object} useResetCreateComponent.previousState * @param {boolean} useResetCreateComponent.open * @param {Function} useResetCreateComponent.setCurrentStep * @param {object} useResetCreateComponent.stepData * @param {number} useResetCreateComponent.initialStep * @param {number} useResetCreateComponent.totalSteps * @param {string} useResetCreateComponent.componentName */ const useResetCreateComponent = ({ firstIncludedStep, previousState, open, setCurrentStep, stepData, initialStep, totalSteps, componentName }) => { (0, react.useEffect)(() => { if (!previousState?.open && open) { if (initialStep && totalSteps && Number(initialStep) <= Number(totalSteps) && Number(initialStep) > 0) { const numberOfHiddenSteps = require_getNumberOfHiddenSteps.getNumberOfHiddenSteps(stepData, initialStep); setCurrentStep(Number(initialStep + numberOfHiddenSteps)); } else setCurrentStep(firstIncludedStep); if (initialStep && totalSteps && Number(initialStep) > Number(totalSteps) || Number(initialStep) <= 0) console.warn(`${componentName}: An invalid \`initialStep\` prop was supplied. The \`initialStep\` prop should be a number that is greater than 0 or less than or equal to the number of steps your ${componentName} has.`); } }, [ firstIncludedStep, open, previousState, setCurrentStep, stepData, initialStep, totalSteps, componentName ]); }; //#endregion exports.useResetCreateComponent = useResetCreateComponent;