"use client"; import type { QuestionField } from "@/data/question-data"; import { useQuestionAnswers } from "./question-answer-storage"; import QuestionTitle from "./question-title"; type QuestionCheckboxProps = { question: QuestionField; questionIndex: number; disabled?: boolean; }; export function QuestionCheckbox({ question, questionIndex, disabled, }: QuestionCheckboxProps) { const options = question.extras.options || []; const { getAnswerValue, setAnswerValue } = useQuestionAnswers(); const rawValue = getAnswerValue(question, questionIndex); const value = Array.isArray(rawValue) ? rawValue : rawValue ? [String(rawValue)] : []; if (options.length === 0) { return null; } const toggleOption = (option: string) => { let nextValue: string[]; if (value.includes(option)) { nextValue = value.filter((v) => v !== option); } else { nextValue = [...value, option]; } const exclusiveOption = options.find( (o) => o.includes("مهم نیست") || o.includes("Doesn't matter") || o.includes("No children") || o.includes("فرزندی ندارم.") || o.includes("مسئولیت مستمری ندارم") || o.includes("do not have any ongoing responsibility"), ); if (exclusiveOption) { if (option === exclusiveOption) { nextValue = [exclusiveOption]; } else if (nextValue.includes(exclusiveOption)) { nextValue = nextValue.filter((v) => v !== exclusiveOption); } } setAnswerValue( question, questionIndex, nextValue.length > 0 ? nextValue : null, ); }; const isShortOptions = options.length <= 4 && options.every((opt) => opt.length <= 15); return (
{options.map((option) => { const optionId = `checkbox-${questionIndex}-${option}`; const isSelected = value.includes(option); return ( ); })}
); } export default QuestionCheckbox;