I want to create a stepper in a form that when you click the next button it's controlled by a useState that sets the current step to true or false. I know I could do this without a map function or by repeating myself with a series of map functions but I don't want that. I want to be able to add as many steps as I want without needing to hard code it in the frontend. any ideas how to connect the index in the map with the current useState inside the if-statement ?
Thanks!
export default function Steps() {
const [stepsDone, setStepsDone] = useState({
step1: false,
step2: false,
step3: false,
step4: false,
step5: false,
step6: false,
});
return (
<>
<Flex flexDir="column">
{steps.map((data, i, arr) => {
if (arr.length - 1 === i) {
console.log(arr[5]);
if (stepsDone.step6) {
return (
<Flex flexDir="column">
<Flex
alignItems="center"
justifyContent="center"
backgroundColor="white"
border="2px solid #31C3AC"
w="52px"
h="52px"
borderRadius="50%"
marginBottom="11px"
>
<Image h="19px" w="20px" alt="check-mark" src={check} />
</Flex>
</Flex>
);
}
return (
<Flex flexDir="column">
<Flex
alignItems="center"
justifyContent="center"
backgroundColor="white"
border="2px solid #CECECE"
w="52px"
h="52px"
borderRadius="50%"
// isActive={stepsDone === index ? true : false}
_active={{}}
>
{data.number}
</Flex>
</Flex>
);
} else if (stepsDone.step5) {
return (
<Flex flexDir="column">
<Flex
alignItems="center"
justifyContent="center"
backgroundColor="white"
border="2px solid #31C3AC"
w="52px"
h="52px"
borderRadius="50%"
marginBottom="11px"
>
<Image h="19px" w="20px" alt="check-mark" src={check} />
</Flex>
<Center marginBottom="11px" h="45px">
<Divider
borderWidth={"1px"}
borderColor={"#31C3AC"}
orientation="vertical"
/>
</Center>
</Flex>
);
} else {
return (
<Flex flexDir="column">
<Flex
flexDir="column"
alignItems="center"
justifyContent="center"
backgroundColor="white"
border="2px solid #CECECE"
w="52px"
h="52px"
borderRadius="50%"
marginBottom="11px"
>
{data.number}
</Flex>
<Center marginBottom="11px" h="45px">
<Divider
borderWidth={"1px"}
borderColor={"#CECECE"}
orientation="vertical"
/>
</Center>
</Flex>
);
}
})}
</Flex>
<Button
onClick={() => {
// steps[5].isComplete = true;
setStepsDone({ ...stepsDone, step5: false });
}}
>
Click meeeeeee
</Button>
</>
);
}