"use client"; import type { QuestionField } from "@/lib/schema-adapter"; import { useQuestionAnswers } from "./question-answer-storage"; import QuestionTitle from "./question-title"; type QuestionCheckboxProps = { question: QuestionField; disabled?: boolean; }; export function QuestionCheckbox({ question, disabled, }: QuestionCheckboxProps) { const options = question.options || []; const { getAnswerValue, setAnswerValue } = useQuestionAnswers(); const rawValue = getAnswerValue(question); const value = Array.isArray(rawValue) ? rawValue : rawValue ? [String(rawValue)] : []; const isSingleCheckbox = options.length === 0; if (isSingleCheckbox) { const isChecked = Boolean( rawValue === true || rawValue === "true" || rawValue === "confirmed" || (Array.isArray(rawValue) && rawValue.length > 0), ); const toggleSingle = () => { setAnswerValue(question, isChecked ? null : ["confirmed"]); }; const labelText = question.description || question.title || "تأیید می‌کنم که نام، سن، تصویر و اطلاعات هویتی من با مدارک بارگذاری‌شده مطابقت دارد."; return (
); } const toggleOption = (optionId: string) => { let nextValue: string[]; if (value.includes(optionId)) { nextValue = value.filter((v) => v !== optionId); } else { nextValue = [...value, optionId]; } setAnswerValue( question, nextValue.length > 0 ? nextValue : null, ); }; // Render horizontally only for 2-option binary choices const isShortOptions = options.length <= 2 && options.every((opt) => String(opt.label || opt.value || "").length <= 10); return (
{options.map((option) => { const optionId = `checkbox-${question.id}-${option.id}`; const isSelected = value.includes(option.id); return ( ); })}
); } export default QuestionCheckbox;