{"version":3,"file":"RadioQuestion.cjs","sources":["../../../../../src/components/feedback/questions/RadioQuestion.tsx"],"sourcesContent":["import React, {\n    type ChangeEventHandler,\n    useEffect,\n    useId,\n    useMemo,\n    useRef,\n} from \"react\";\nimport { RadioButton } from \"../../radio-button/RadioButton.js\";\nimport { RadioButtonGroup } from \"../../radio-button/RadioButtonGroup.js\";\nimport { useFollowUpContext } from \"../followup/followupContext.js\";\nimport { useMainQuestionContext } from \"../main-question/mainQuestionContext.js\";\nimport type { QuestionProps } from \"../types.js\";\n\nexport const RadioQuestion: 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 id = useId();\n\n    const numOptions = options?.length || 0;\n\n    const ref = useRef<HTMLInputElement>(null);\n    useEffect(() => {\n        if (autoFocus && ref.current) {\n            ref.current.focus();\n        }\n    }, [autoFocus]);\n\n    const handleChange: ChangeEventHandler<HTMLInputElement> = (e) => {\n        const option = options?.find(\n            (option) => option.value.toString() === e.target.value,\n        );\n        context?.setCurrentValue(option);\n    };\n\n    const selectedValue = useMemo(\n        () =>\n            Array.isArray(context?.currentValue)\n                ? context?.currentValue[0].value.toString()\n                : context?.currentValue?.value.toString(),\n        [context?.currentValue],\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        <RadioButtonGroup\n            legend={label}\n            labelProps={{ variant: \"large\" }}\n            name={`${id}-${name || label}`}\n            inline={numOptions < 3}\n            value={selectedValue || \"\"}\n            onChange={handleChange}\n            helpLabel={helpLabel}\n        >\n            {options?.map((option, i) => (\n                <RadioButton\n                    ref={i === 0 ? ref : undefined}\n                    key={`${id}-${name || label}-${option.value}`}\n                    label={option.label}\n                    value={String(option.value)}\n                />\n            ))}\n        </RadioButtonGroup>\n    );\n};\n"],"names":["label","name","options","helpLabel","autoFocus","followupContext","useFollowUpContext","feedbackContext","useMainQuestionContext","context","id","useId","numOptions","length","ref","useRef","useEffect","current","focus","selectedValue","useMemo","Array","isArray","currentValue","value","toString","jsx","RadioButtonGroup","legend","labelProps","variant","inline","onChange","e","option","find","target","setCurrentValue","children","map","i","RadioButton","String","console","error"],"mappings":"sWAasD,EAClDA,MAAAA,EACAC,KAAAA,EACAC,QAAAA,EACAC,UAAAA,EACAC,UAAAA,GAAY,MAEZ,MAAMC,EAAkBC,EAAAA,qBAClBC,EAAkBC,EAAAA,yBAClBC,EAAUJ,GAAmBE,EAC7BG,EAAKC,EAAAA,QAELC,EAAaV,GAASW,QAAU,EAEhCC,EAAMC,EAAAA,OAAyB,MACrCC,EAAAA,UAAU,KACFZ,GAAaU,EAAIG,SACjBH,EAAIG,QAAQC,SAEjB,CAACd,IAEJ,MAOMe,EAAgBC,EAAAA,QAClB,IACIC,MAAMC,QAAQb,GAASc,cACjBd,GAASc,aAAa,GAAGC,MAAMC,WAC/BhB,GAASc,cAAcC,MAAMC,WACvC,CAAChB,GAASc,eAGd,OAAKd,EAQDiB,EAAAA,IAACC,EAAAA,iBAAA,CACGC,OAAQ5B,EACR6B,WAAY,CAAEC,QAAS,SACvB7B,KAAM,GAAGS,KAAMT,GAAQD,IACvB+B,OAAQnB,EAAa,EACrBY,MAAOL,GAAiB,GACxBa,SA7BoDC,IACxD,MAAMC,EAAShC,GAASiC,KACnBD,GAAWA,EAAOV,MAAMC,aAAeQ,EAAEG,OAAOZ,OAErDf,GAAS4B,gBAAgBH,IA0BrB/B,UAAAA,EAECmC,SAAApC,GAASqC,IAAI,CAACL,EAAQM,IACnBd,EAAAA,IAACe,EAAAA,YAAA,CACG3B,IAAW,IAAN0B,EAAU1B,OAAM,EAErBd,MAAOkC,EAAOlC,MACdwB,MAAOkB,OAAOR,EAAOV,QAFhB,GAAGd,KAAMT,GAAQD,KAASkC,EAAOV,aAnBlDmB,QAAQC,MACJ,yEAEG"}