@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
38 lines (37 loc) • 1.32 kB
JavaScript
"use client";
import React, { useCallback, useContext, useRef } from 'react';
import WizardContext from '../Context/WizardContext';
import { createReferenceKey, useSharedState } from '../../../../shared/helpers/useSharedState';
const useLayoutEffect = typeof window === 'undefined' ? React.useEffect : React.useLayoutEffect;
export default function useStep(id = null, {
onStepChange
} = {}) {
const setFormError = useCallback(() => null, []);
const wizardContext = useContext(WizardContext) || {
setFormError
};
if (onStepChange && !id && wizardContext.id) {
id = wizardContext.id;
}
const sharedDataRef = useRef(null);
sharedDataRef.current = useSharedState(id ? createReferenceKey(id, 'wizard') : undefined);
useLayoutEffect(() => {
sharedDataRef.current.extend({
onStepChange
});
}, [onStepChange]);
const data = sharedDataRef.current.data;
if (data && !data.setFormError) {
data.setFormError = setFormError;
}
const context = data || wizardContext;
const {
totalStepsRef
} = context || {};
const totalSteps = (totalStepsRef === null || totalStepsRef === void 0 ? void 0 : totalStepsRef.current) || 0;
if (context && context.totalSteps !== totalSteps) {
context.totalSteps = totalSteps;
}
return context;
}
//# sourceMappingURL=useStep.js.map