UNPKG

@carbon/ibm-products

Version:
51 lines (49 loc) 2.14 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 { getNumberOfHiddenSteps } from "../utils/getNumberOfHiddenSteps.js"; import { useEffect } from "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 }) => { useEffect(() => { if (!previousState?.open && open) { if (initialStep && totalSteps && Number(initialStep) <= Number(totalSteps) && Number(initialStep) > 0) { const numberOfHiddenSteps = 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 export { useResetCreateComponent };