chore: refactor
This commit is contained in:
@@ -57,8 +57,8 @@ export const Stepper: FC<StepperProps> = ({
|
|||||||
onStepChanged && onStepChanged(currentStep);
|
onStepChanged && onStepChanged(currentStep);
|
||||||
}, [currentStep, onStepChanged]);
|
}, [currentStep, onStepChanged]);
|
||||||
|
|
||||||
const useStep = (stepIndex: number) => ({
|
const useStep = () => ({
|
||||||
stepIndex,
|
stepIndex: currentStep - 1,
|
||||||
currentStep,
|
currentStep,
|
||||||
totalSteps,
|
totalSteps,
|
||||||
isFirst: currentStep === 1,
|
isFirst: currentStep === 1,
|
||||||
@@ -67,14 +67,13 @@ export const Stepper: FC<StepperProps> = ({
|
|||||||
previousStep,
|
previousStep,
|
||||||
});
|
});
|
||||||
|
|
||||||
const renderStep = (child: React.ReactNode, index: number) => {
|
// empty stepper
|
||||||
if (!React.isValidElement<StepProps>(child)) return null;
|
if (totalSteps === 0) return null;
|
||||||
return index + 1 === currentStep
|
|
||||||
? React.cloneElement(child, {
|
|
||||||
useStep: () => useStep(index),
|
|
||||||
})
|
|
||||||
: null;
|
|
||||||
};
|
|
||||||
|
|
||||||
return <>{React.Children.toArray(children).map(renderStep)}</>;
|
const currentChild = React.Children.toArray(children)[currentStep - 1];
|
||||||
|
|
||||||
|
// type validation
|
||||||
|
if (!React.isValidElement<StepProps>(currentChild)) return null;
|
||||||
|
|
||||||
|
return <>{React.cloneElement(currentChild, { useStep })}</>;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user