import React, { useState } from "react"; import { ICheckbox, IAnswerProps } from "../types"; export const ACheckbox = ({ element, answer: answerQuestion, }: IAnswerProps) => { const [content, setContent] = useState([]); const handleChange = (event: React.ChangeEvent) => { const { value, checked } = event.currentTarget; // console.log("value:", value, "checked:", checked); if (checked) { content.push(value); } else { const index = content.indexOf(value); if (index !== -1) { content.splice(index, 1); } } // if (answerQuestion.content) { // if (answerQuestion.content.find((a: any) => a === value)) { // const newList = answerQuestion.content.filter((a: any) => a !== value); // answerQuestion.content = newList; // if (answerQuestion.content.length) { // answerQuestion.requiredCheck = true; // } else { // answerQuestion.requiredCheck = false; // } // } else { // answerQuestion.content.push(value); // answerQuestion.requiredCheck = true; // } // } else { // answerQuestion.content = []; // answerQuestion.content.push(value); // answerQuestion.requiredCheck = true; // } if (content.length > 0) { answerQuestion.requiredCheck = true; } else { answerQuestion.requiredCheck = false; } answerQuestion.content = [...content]; console.log("answer content", answerQuestion); setContent([...content]); }; // console.log("content:", content); return (
{element.content.choices.map((choice) => (
))}
); };