{"version":3,"file":"SmileyQuestion.cjs","sources":["../../../../../src/components/feedback/questions/SmileyQuestion.tsx"],"sourcesContent":["import React, {\n    type ChangeEventHandler,\n    Fragment,\n    useId,\n    useMemo,\n} from \"react\";\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\";\nimport { defaultOptions, getSmiley } from \"./smileyUtils.js\";\n\nconst isNotInScale = (option: FeedbackOption) =>\n    typeof option.value !== \"number\" || ![1, 2, 3, 4, 5].includes(option.value);\n\nexport const SmileyQuestion: React.FC<QuestionProps> = ({\n    label,\n    name = \"smiley\",\n    helpLabel,\n    options = defaultOptions,\n}) => {\n    const followupContext = useFollowUpContext();\n    const feedbackContext = useMainQuestionContext();\n    const context = followupContext || feedbackContext;\n    const id = useId();\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\n                : context?.currentValue?.value,\n        [context?.currentValue],\n    );\n\n    if (options.some(isNotInScale)) {\n        console.error(\n            \"SmileyQuestion må ha tallene 1 til 5 som verdier for alternativene sine\",\n        );\n        return null;\n    }\n\n    if (!context || !options) {\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            <div className=\"jkl-feedback-smileys\">\n                {options.map((option) => (\n                    <Fragment key={option.value}>\n                        <input\n                            className=\"jkl-sr-only\"\n                            id={`${id}-${name}-${option.value}`}\n                            name={`${id}-${name}`}\n                            type=\"radio\"\n                            value={option.value}\n                            onChange={handleChange}\n                            checked={selectedValue === option.value}\n                        />\n                        <label\n                            className=\"jkl-feedback-smiley-option\"\n                            htmlFor={`${id}-${name}-${option.value}`}\n                        >\n                            <span className=\"jkl-sr-only\">{option.label}</span>\n                            {getSmiley(Number(option.value))}\n                        </label>\n                    </Fragment>\n                ))}\n            </div>\n        </FieldGroup>\n    );\n};\n"],"names":["isNotInScale","option","value","includes","label","name","helpLabel","options","defaultOptions","followupContext","useFollowUpContext","feedbackContext","useMainQuestionContext","context","id","useId","handleChange","e","find","toString","target","setCurrentValue","selectedValue","useMemo","Array","isArray","currentValue","some","console","error","jsx","FieldGroup","labelProps","variant","legend","children","className","map","jsxs","Fragment","type","onChange","checked","htmlFor","getSmiley","Number"],"mappings":"wTAYMA,EAAgBC,GACM,iBAAjBA,EAAOC,QAAuB,CAAC,EAAG,EAAG,EAAG,EAAG,GAAGC,SAASF,EAAOC,8BAElB,EACnDE,MAAAA,EACAC,KAAAA,EAAO,SACPC,UAAAA,EACAC,QAAAA,EAAUC,EAAAA,mBAEV,MAAMC,EAAkBC,EAAAA,qBAClBC,EAAkBC,EAAAA,yBAClBC,EAAUJ,GAAmBE,EAC7BG,EAAKC,EAAAA,QAELC,EAAsDC,IACxD,MAAMhB,EAASM,GAASW,KACnBjB,GAAWA,EAAOC,MAAMiB,aAAeF,EAAEG,OAAOlB,OAErDW,GAASQ,gBAAgBpB,IAGvBqB,EAAgBC,EAAAA,QAClB,IACIC,MAAMC,QAAQZ,GAASa,cACjBb,GAASa,aAAa,GAAGxB,MACzBW,GAASa,cAAcxB,MACjC,CAACW,GAASa,eAGd,OAAInB,EAAQoB,KAAK3B,IACb4B,QAAQC,MACJ,2EAEG,MAGNhB,GAAYN,EAQbuB,EAAAA,IAACC,EAAAA,WAAA,CACGC,WAAY,CAAEC,QAAS,SACvBC,OAAQ9B,EACRE,UAAAA,EAEA6B,SAAAL,EAAAA,IAAC,OAAIM,UAAU,uBACVD,WAAQE,IAAKpC,GACVqC,EAAAA,KAACC,EAAAA,SAAA,CACGJ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