UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

101 lines (100 loc) 3.01 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)) { var _child, _child2; let step = child; if (((_child = child) === null || _child === void 0 ? void 0 : _child.type) !== Step && typeof child.type === 'function') { var _step; step = child.type(child.props); if (((_step = step) === null || _step === void 0 ? void 0 : _step.type) === Step) { child = step; } } if (((_child2 = child) === null || _child2 === void 0 ? void 0 : _child2.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