UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

97 lines (96 loc) 2.81 kB
"use client"; import React, { useContext } from 'react'; import WizardContext from "../Context/WizardContext.js"; import WizardStepContext from "../Step/StepContext.js"; import Step, { handleDeprecatedProps as handleDeprecatedStepProps } from "../Step/Step.js"; import { useCollectStepsData } from "./useCollectStepsData.js"; export function IterateOverSteps({ children }) { const { check, stepsRef, activeIndexRef, totalStepsRef, stepIndexRef, prerenderFieldProps, prerenderFieldPropsRef, hasErrorInOtherStepRef, mapOverChildrenRef } = useContext(WizardContext); const { collectStepsData } = useCollectStepsData(); stepsRef.current = new Map(); hasErrorInOtherStepRef.current = false; stepIndexRef.current = -1; totalStepsRef.current = 0; const childrenArray = React.Children.map(children, child => { if (React.isValidElement(child)) { let step = child; if (child?.type !== Step && typeof child.type === 'function') { step = child.type.apply(child.type, [child.props]); if (step?.type === Step) { child = step; } } if (child?.type === Step) { const { title, inactive, keepInDOM, include, id, includeWhen } = handleDeprecatedStepProps(child.props); if (include === false) { return null; } if (includeWhen && !check({ visibleWhen: includeWhen })) { return null; } const index = totalStepsRef.current; totalStepsRef.current = totalStepsRef.current + 1; collectStepsData({ id, index, title, inactive, keepInDOM }); if (prerenderFieldProps && typeof document !== 'undefined' && index !== activeIndexRef.current && typeof prerenderFieldPropsRef.current['step-' + index] === 'undefined') { const key = `${index}-${activeIndexRef.current}`; prerenderFieldPropsRef.current['step-' + index] = { index, fn: () => React.cloneElement(child, { key, index, prerenderFieldProps: true }) }; } return child; } } return child; }); if (totalStepsRef.current === 0) { activeIndexRef.current = 0; } else if (totalStepsRef.current < activeIndexRef.current + 1) { activeIndexRef.current = totalStepsRef.current - 1; } if (mapOverChildrenRef.current) { return childrenArray.map((child, index) => { return React.createElement(WizardStepContext.Provider, { key: index, value: { index } }, child); }); } return children; } //# sourceMappingURL=IterateOverSteps.js.map