@@ -86,7 +86,7 @@ const questions: QuestionType[] = [
8686
8787const Questionnaire = ( ) => {
8888 const [ currentQuestion , setCurrentQuestion ] = useState ( 0 ) ;
89- const [ answers , setAnswers ] = useState < Record < string , any > > ( { } ) ;
89+ const [ answers , setAnswers ] = useState < Record < string , string | number | string [ ] > > ( { } ) ;
9090 const [ submitted , setSubmitted ] = useState ( false ) ;
9191 const sectionRef = useRef < HTMLElement > ( null ) ;
9292 const { toast } = useToast ( ) ;
@@ -144,7 +144,7 @@ const Questionnaire = () => {
144144 } ;
145145
146146 // Handle answer updates
147- const handleAnswer = ( value : any ) => {
147+ const handleAnswer = ( value : string | number | string [ ] ) => {
148148 const question = questions [ currentQuestion ] ;
149149 setAnswers ( {
150150 ...answers ,
@@ -213,13 +213,14 @@ const Questionnaire = () => {
213213 { threshold : 0.1 , rootMargin : "0px 0px -100px 0px" }
214214 ) ;
215215
216- if ( sectionRef . current ) {
217- observer . observe ( sectionRef . current ) ;
216+ const currentSection = sectionRef . current ;
217+ if ( currentSection ) {
218+ observer . observe ( currentSection ) ;
218219 }
219220
220221 return ( ) => {
221- if ( sectionRef . current ) {
222- observer . unobserve ( sectionRef . current ) ;
222+ if ( currentSection ) {
223+ observer . unobserve ( currentSection ) ;
223224 }
224225 } ;
225226 } , [ ] ) ;
@@ -290,36 +291,39 @@ const Questionnaire = () => {
290291 case 'checkbox' :
291292 return (
292293 < div className = "space-y-3" >
293- { question . options ?. map ( ( option ) => (
294- < label
295- key = { option }
296- className = { cn (
297- "flex items-center p-4 border rounded-lg cursor-pointer transition-all" ,
298- answers [ question . id ] ?. includes ( option )
299- ? "border-wellness-500 bg-wellness-50 ring-2 ring-wellness-200"
300- : "border-border hover:border-wellness-300"
301- ) }
302- >
303- < input
304- type = "checkbox"
305- name = { question . id }
306- checked = { answers [ question . id ] ?. includes ( option ) || false }
307- onChange = { ( ) => handleCheckboxChange ( option ) }
308- className = "sr-only"
309- />
310- < div className = { cn (
311- "w-5 h-5 rounded border-2 flex items-center justify-center mr-3 transition-all" ,
312- answers [ question . id ] ?. includes ( option )
313- ? "border-wellness-500 bg-wellness-500"
314- : "border-border"
315- ) } >
316- { answers [ question . id ] ?. includes ( option ) && (
317- < svg xmlns = "http://www.w3.org/2000/svg" width = "14" height = "14" viewBox = "0 0 24 24" fill = "none" stroke = "white" strokeWidth = "3" strokeLinecap = "round" strokeLinejoin = "round" > < polyline points = "20 6 9 17 4 12" > </ polyline > </ svg >
294+ { question . options ?. map ( ( option ) => {
295+ const answerArray = Array . isArray ( answers [ question . id ] ) ? answers [ question . id ] as string [ ] : [ ] ;
296+ return (
297+ < label
298+ key = { option }
299+ className = { cn (
300+ "flex items-center p-4 border rounded-lg cursor-pointer transition-all" ,
301+ answerArray . includes ( option )
302+ ? "border-wellness-500 bg-wellness-50 ring-2 ring-wellness-200"
303+ : "border-border hover:border-wellness-300"
318304 ) }
319- </ div >
320- < span > { option } </ span >
321- </ label >
322- ) ) }
305+ >
306+ < input
307+ type = "checkbox"
308+ name = { question . id }
309+ checked = { answerArray . includes ( option ) }
310+ onChange = { ( ) => handleCheckboxChange ( option ) }
311+ className = "sr-only"
312+ />
313+ < div className = { cn (
314+ "w-5 h-5 rounded border-2 flex items-center justify-center mr-3 transition-all" ,
315+ answerArray . includes ( option )
316+ ? "border-wellness-500 bg-wellness-500"
317+ : "border-border"
318+ ) } >
319+ { answerArray . includes ( option ) && (
320+ < svg xmlns = "http://www.w3.org/2000/svg" width = "14" height = "14" viewBox = "0 0 24 24" fill = "none" stroke = "white" strokeWidth = "3" strokeLinecap = "round" strokeLinejoin = "round" > < polyline points = "20 6 9 17 4 12" > </ polyline > </ svg >
321+ ) }
322+ </ div >
323+ < span > { option } </ span >
324+ </ label >
325+ ) ;
326+ } ) }
323327 </ div >
324328 ) ;
325329
0 commit comments