@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
99 lines (98 loc) • 3.04 kB
JavaScript
"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)) {
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.apply(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
} = 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