'use client' import type { QuestionOption } from '@/lib/types' interface CheckboxGroupProps { name: string legend: string options: QuestionOption[] value: string[] maxSelect: number | null required: boolean onChange: (value: string[]) => void error?: string } export function CheckboxGroup({ name, legend, options, value, maxSelect, required, onChange, error, }: CheckboxGroupProps) { const atLimit = maxSelect !== null && value.length >= maxSelect function toggle(optValue: string) { if (value.includes(optValue)) { onChange(value.filter((v) => v !== optValue)) } else if (!atLimit) { onChange([...value, optValue]) } } return (
{legend} {required && } {required && (obligatorio)} {maxSelect !== null && (

Elegí hasta {maxSelect} opciones {value.length > 0 && ` · ${value.length}/${maxSelect} seleccionadas`}

)}
{options.map((opt) => { const id = `${name}_${opt.value.replace(/\s+/g, '_')}` const checked = value.includes(opt.value) const disabled = atLimit && !checked return (
) })}
{error && ( )}
) }