import React, { useState, useEffect } from 'react'; const Step = ({ number, title, isActive, isValid, isCompleted, onClick }) => { return (
{isCompleted ? ( ) : ( number )}
{title}
); }; const SpacerStep = ({ isCompleted }) => { return (
); }; const Dots = () => { return (
...
...
); }; const ProgressStep = ({ steps, stepTitles, currentStep, setStep, isStepValid, }) => { const [windowWidth, setWindowWidth] = useState(window.innerWidth); const [visibleSteps, setVisibleSteps] = useState(steps); useEffect(() => { const handleResize = () => setWindowWidth(window.innerWidth); window.addEventListener('resize', handleResize); return () => window.removeEventListener('resize', handleResize); }, []); useEffect(() => { const calculateVisibleSteps = () => { const minWidth = 150; // Largeur minimale estimée par étape const maxVisibleSteps = Math.floor(windowWidth / minWidth); if (maxVisibleSteps >= steps.length) { setVisibleSteps(steps); return; } if (maxVisibleSteps < 4) { // Garder seulement première, dernière et courante let filtered = [steps[0]]; if (currentStep > 1 && currentStep < steps.length) { filtered.push('...'); filtered.push(steps[currentStep - 1]); } if (currentStep < steps.length) { filtered.push('...'); } filtered.push(steps[steps.length - 1]); setVisibleSteps(filtered); } else { // Garder première, dernière, courante et quelques étapes adjacentes let filtered = [steps[0]]; if (currentStep > 2) filtered.push('...'); if (currentStep > 1 && currentStep < steps.length) { filtered.push(steps[currentStep - 1]); } if (currentStep < steps.length - 1) filtered.push('...'); filtered.push(steps[steps.length - 1]); setVisibleSteps(filtered); } }; calculateVisibleSteps(); }, [windowWidth, currentStep, steps]); const handleStepClick = (stepIndex) => { // Vérifie si on peut naviguer vers l'étape (toutes les étapes précédentes doivent être valides) const canNavigate = Array.from( { length: stepIndex }, (_, i) => i + 1 ).every((step) => isStepValid(step)); if (canNavigate) { setStep(stepIndex + 1); } }; return (
{visibleSteps.map((step, index) => { if (step === '...') { return (
{index !== visibleSteps.length - 1 && ( )}
); } const originalIndex = steps.indexOf(step); return (
i + 1).every((s) => isStepValid(s)) ? 'cursor-pointer' : 'cursor-not-allowed'} `} onClick={() => handleStepClick(originalIndex)} >
originalIndex + 1} isValid={isStepValid(originalIndex + 1)} /> {index !== visibleSteps.length - 1 && ( originalIndex + 1} /> )}
); })}
); }; export default ProgressStep;