UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

55 lines (49 loc) 2.24 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. */ 'use strict'; var React = require('react'); var getNumberOfHiddenSteps = require('../utils/getNumberOfHiddenSteps.js'); /** * 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 = _ref => { let { firstIncludedStep, previousState, open, setCurrentStep, stepData, initialStep, totalSteps, componentName } = _ref; React.useEffect(() => { if (!previousState?.open && open) { if (initialStep && totalSteps && Number(initialStep) <= Number(totalSteps) && Number(initialStep) > 0) { const numberOfHiddenSteps = getNumberOfHiddenSteps.getNumberOfHiddenSteps(stepData, initialStep); setCurrentStep(Number(initialStep + numberOfHiddenSteps)); } else { // default should be fist includedStep instead of just 1 setCurrentStep(firstIncludedStep); } // An invalid initialStep value was provided, we'll default to rendering the first step in this scenario 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]); }; exports.useResetCreateComponent = useResetCreateComponent;