UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

99 lines (98 loc) 2.78 kB
"use client"; import React, { useContext } from 'react'; import WizardContext from "../Context/WizardContext.js"; import WizardStepContext from "../Step/StepContext.js"; import Step from "../Step/Step.js"; import { useCollectStepsData } from "./useCollectStepsData.js"; import { jsx as _jsx } from "react/jsx-runtime"; 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(child.props); if (step?.type === Step) { child = step; } } if (child?.type === Step) { const { title, inactive, keepInDOM, include, id, includeWhen } = 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.createElement(child.type, { ...child.props, 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 _jsx(WizardStepContext, { value: { index }, children: child }, index); }); } return children; } //# sourceMappingURL=IterateOverSteps.js.map