@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
107 lines (106 loc) • 3.38 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.IterateOverSteps = IterateOverSteps;
var _react = require("react");
var _WizardContext = _interopRequireDefault(require("../Context/WizardContext.js"));
var _StepContext = _interopRequireDefault(require("../Step/StepContext.js"));
var _Step = _interopRequireDefault(require("../Step/Step.js"));
var _useCollectStepsData = require("./useCollectStepsData.js");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function IterateOverSteps({
children
}) {
const {
check,
stepsRef,
activeIndexRef,
totalStepsRef,
stepIndexRef,
prerenderFieldProps,
prerenderFieldPropsRef,
hasErrorInOtherStepRef,
mapOverChildrenRef
} = (0, _react.useContext)(_WizardContext.default);
const {
collectStepsData
} = (0, _useCollectStepsData.useCollectStepsData)();
stepsRef.current = new Map();
hasErrorInOtherStepRef.current = false;
stepIndexRef.current = -1;
totalStepsRef.current = 0;
const childrenArray = _react.Children.map(children, child => {
if ((0, _react.isValidElement)(child)) {
var _child, _child2;
let step = child;
if (((_child = child) === null || _child === void 0 ? void 0 : _child.type) !== _Step.default && typeof child.type === 'function') {
var _step;
step = child.type(child.props);
if (((_step = step) === null || _step === void 0 ? void 0 : _step.type) === _Step.default) {
child = step;
}
}
if (((_child2 = child) === null || _child2 === void 0 ? void 0 : _child2.type) === _Step.default) {
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: () => (0, _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 (0, _jsxRuntime.jsx)(_StepContext.default, {
value: {
index
},
children: child
}, index);
});
}
return children;
}
//# sourceMappingURL=IterateOverSteps.js.map