{"version":3,"file":"CheckboxQuestion.cjs","sources":["../../../../../src/components/feedback/questions/CheckboxQuestion.tsx"],"sourcesContent":["import React, { type ChangeEventHandler, useEffect, useRef } from \"react\";\nimport { Checkbox } from \"../../checkbox/Checkbox.js\";\nimport { FieldGroup } from \"../../input-group/FieldGroup.js\";\nimport { useFollowUpContext } from \"../followup/followupContext.js\";\nimport { useMainQuestionContext } from \"../main-question/mainQuestionContext.js\";\nimport type { FeedbackOption, QuestionProps } from \"../types.js\";\n\nexport const CheckboxQuestion: React.FC<QuestionProps> = ({\n    label,\n    name,\n    options,\n    helpLabel,\n    autoFocus = false,\n}) => {\n    const followupContext = useFollowUpContext();\n    const feedbackContext = useMainQuestionContext();\n    const context = followupContext || feedbackContext;\n    const ref = useRef<HTMLInputElement>(null);\n\n    useEffect(() => {\n        if (autoFocus && ref.current) {\n            ref.current.focus();\n        }\n    }, [autoFocus]);\n\n    const handleChange: ChangeEventHandler<HTMLInputElement> = (event) => {\n        const { value } = event.target;\n        const matchingOption = options?.find(\n            (option) => option.value.toString() === value,\n        );\n        if (!matchingOption) return;\n\n        if (!context?.currentValue) {\n            context?.setCurrentValue([matchingOption]);\n            return;\n        }\n\n        if (Array.isArray(context?.currentValue)) {\n            const found = context.currentValue.find(\n                (option) => option === matchingOption,\n            );\n            if (found) {\n                context.setCurrentValue((oldValues) =>\n                    (oldValues as FeedbackOption[]).filter(\n                        (option) => option !== found,\n                    ),\n                );\n            } else {\n                context.setCurrentValue((oldValues) => [\n                    ...(oldValues as FeedbackOption[]),\n                    matchingOption,\n                ]);\n            }\n        }\n    };\n\n    if (!context) {\n        console.error(\n            \"Questions must be used inside a Followup or Feedback context provider\",\n        );\n        return null;\n    }\n\n    return (\n        <FieldGroup\n            labelProps={{ variant: \"large\" }}\n            legend={label}\n            helpLabel={helpLabel}\n        >\n            {options?.map((option, i) => (\n                <Checkbox\n                    key={`${label}-${option.value}`}\n                    name={name || label}\n                    value={option.value.toString()}\n                    onChange={handleChange}\n                    ref={i === 0 ? ref : undefined}\n                >\n                    {option.label}\n                </Checkbox>\n            ))}\n        </FieldGroup>\n    );\n};\n"],"names":["label","name","options","helpLabel","autoFocus","followupContext","useFollowUpContext","feedbackContext","useMainQuestionContext","context","ref","useRef","useEffect","current","focus","handleChange","event","value","target","matchingOption","find","option","toString","currentValue","setCurrentValue","Array","isArray","found","oldValues","filter","jsx","FieldGroup","labelProps","variant","legend","children","map","i","Checkbox","onChange","console","error"],"mappings":"2VAOyD,EACrDA,MAAAA,EACAC,KAAAA,EACAC,QAAAA,EACAC,UAAAA,EACAC,UAAAA,GAAY,MAEZ,MAAMC,EAAkBC,EAAAA,qBAClBC,EAAkBC,EAAAA,yBAClBC,EAAUJ,GAAmBE,EAC7BG,EAAMC,EAAAA,OAAyB,MAErCC,EAAAA,UAAU,KACFR,GAAaM,EAAIG,SACjBH,EAAIG,QAAQC,SAEjB,CAACV,IAEJ,MAAMW,EAAsDC,IACxD,MAAQC,MAAAA,GAAUD,EAAME,OAClBC,EAAiBjB,GAASkB,KAC3BC,GAAWA,EAAOJ,MAAMK,aAAeL,GAE5C,GAAKE,EAEL,CAAA,IAAKV,GAASc,aAEV,YADAd,GAASe,gBAAgB,CAACL,IAI9B,GAAIM,MAAMC,QAAQjB,GAASc,cAAe,CACtC,MAAMI,EAAQlB,EAAQc,aAAaH,KAC9BC,GAAWA,IAAWF,GAEvBQ,EACAlB,EAAQe,gBAAiBI,GACpBA,EAA+BC,OAC3BR,GAAWA,IAAWM,IAI/BlB,EAAQe,gBAAiBI,GAAc,IAC/BA,EACJT,GAGZ,CAAA,GAGJ,OAAKV,EAQDqB,EAAAA,IAACC,EAAAA,WAAA,CACGC,WAAY,CAAEC,QAAS,SACvBC,OAAQlC,EACRG,UAAAA,EAECgC,SAAAjC,GAASkC,IAAI,CAACf,EAAQgB,IACnBP,EAAAA,IAACQ,EAAAA,SAAA,CAEGrC,KAAMA,GAAQD,EACdiB,MAAOI,EAAOJ,MAAMK,WACpBiB,SAAUxB,EACVL,IAAW,IAAN2B,EAAU3B,OAAM,EAEpByB,SAAAd,EAAOrB,OANH,GAAGA,KAASqB,EAAOJ,aAdpCuB,QAAQC,MACJ,yEAEG"}