Denis Thiessen
6 months ago
6 changed files with 162 additions and 0 deletions
-
13package-lock.json
-
1package.json
-
63src/components/questionnaire/QuestionComponent.jsx
-
36src/components/questionnaire/QuestionData.jsx
-
27src/components/questionnaire/QuestionnaireData.jsx
-
22src/pages/TestQuestionnaire.jsx
@ -0,0 +1,63 @@ |
|||||
|
// QuestionComponent.js |
||||
|
import React, { useEffect } from "react"; |
||||
|
import { Slider, Input, Radio, Checkbox, Link } from '@geist-ui/core'; |
||||
|
|
||||
|
export default function QuestionComponent(props) { |
||||
|
const { questionData, onUpdateAnswerData } = props; |
||||
|
|
||||
|
useEffect(() => { |
||||
|
// Update questionData when component mounts or props change |
||||
|
questionData.sData({ "id": props.id, "answerValue": questionData.answerValue }); |
||||
|
}, [props.id, questionData.answerValue]); |
||||
|
|
||||
|
// Define a function to handle answer data change |
||||
|
const handleAnswerChange = (val) => { |
||||
|
questionData.sAnswerData(val); |
||||
|
onUpdateAnswerData(questionData.gAnswerData()); // Update answerData in parent component |
||||
|
}; |
||||
|
|
||||
|
// Determine and render answer element based on props |
||||
|
var element; |
||||
|
if (props.answerType === "slider") { |
||||
|
element = ( |
||||
|
<Slider |
||||
|
onChange={handleAnswerChange} |
||||
|
max={props.maxElement} |
||||
|
initialValue={questionData.gAnswerData()} |
||||
|
showMarkers |
||||
|
width="75%" |
||||
|
/> |
||||
|
); |
||||
|
} else if (props.answerType === "radio") { |
||||
|
const btnIndexes = Array.from({ length: props.maxElement }, (_, i) => i + 1); |
||||
|
element = ( |
||||
|
<Radio.Group onChange={handleAnswerChange}> |
||||
|
{btnIndexes.map((x) => <Radio key={x} value={x}>{x}</Radio>)} |
||||
|
</Radio.Group> |
||||
|
); |
||||
|
} else if (props.answerType === "checkbox") { |
||||
|
const btnIndexes = Array.from({ length: props.maxElement }, (_, i) => i + 1); |
||||
|
element = ( |
||||
|
<Checkbox.Group onChange={handleAnswerChange}> |
||||
|
{btnIndexes.map((x) => <Checkbox key={x} value={x}>{x}</Checkbox>)} |
||||
|
</Checkbox.Group> |
||||
|
); |
||||
|
} else { |
||||
|
element = ( |
||||
|
<Input |
||||
|
onChange={e => handleAnswerChange(e.target.value)} |
||||
|
clearable |
||||
|
/> |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
// Render the component |
||||
|
return ( |
||||
|
<div> |
||||
|
<p>{props.text}</p> |
||||
|
{element} |
||||
|
{props.referBack && <Link href={props.referBack} block>←</Link>} |
||||
|
<Link href={props.referTo} block>→</Link> |
||||
|
</div> |
||||
|
); |
||||
|
} |
@ -0,0 +1,36 @@ |
|||||
|
import {useState} from "react"; |
||||
|
|
||||
|
export function useQuestionData() { |
||||
|
const [data, setData] = useState({}); |
||||
|
|
||||
|
function setQuestionData(questionData) { |
||||
|
setData(questionData); |
||||
|
}; |
||||
|
|
||||
|
function getQuestionData() { |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
function setAnswerValue(val) { |
||||
|
data.answerValue = val; |
||||
|
setData(data); |
||||
|
} |
||||
|
|
||||
|
function getAnswerValue() { |
||||
|
return data.answerValue; |
||||
|
} |
||||
|
|
||||
|
function reset() { |
||||
|
setData({}); |
||||
|
}; |
||||
|
|
||||
|
const inputProps = { |
||||
|
sData: setQuestionData, |
||||
|
gData: getQuestionData, |
||||
|
sAnswerData: setAnswerValue, |
||||
|
gAnswerData: getAnswerValue, |
||||
|
r: reset |
||||
|
}; |
||||
|
|
||||
|
return inputProps; |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
// useQuestionComponent.js |
||||
|
import { useState, useEffect } from "react"; |
||||
|
import { useQuestionData } from "./QuestionData"; |
||||
|
|
||||
|
export function useQuestionComponent(answerType, maxElement, text, referBack, referTo) { |
||||
|
const [answerData, setAnswerData] = useState(null); |
||||
|
const questionData = useQuestionData({}); |
||||
|
|
||||
|
useEffect(() => { |
||||
|
setAnswerData(questionData.gAnswerData()); |
||||
|
}, [questionData]); |
||||
|
|
||||
|
const handleUpdateAnswerData = (data) => { |
||||
|
setAnswerData(data); |
||||
|
}; |
||||
|
|
||||
|
return { |
||||
|
questionData, |
||||
|
answerData, |
||||
|
handleUpdateAnswerData, // Pass the function here |
||||
|
answerType, |
||||
|
maxElement, |
||||
|
text, |
||||
|
referBack, |
||||
|
referTo |
||||
|
}; |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
import React from "react"; |
||||
|
import { useParams } from 'react-router-dom'; |
||||
|
import QuestionComponent from "../components/questionnaire/QuestionComponent"; |
||||
|
import { useQuestionComponent } from "../components/questionnaire/QuestionnaireData"; |
||||
|
|
||||
|
|
||||
|
export default function TestQuestionnaire() { |
||||
|
const { id } = useParams() |
||||
|
|
||||
|
const q1Props = useQuestionComponent("checkbox", 4, "Lorem Ipsum", "./info", "./q2"); |
||||
|
const q2Props = useQuestionComponent("slider", 8, "Lorem Ipsum Ye", "./q1", "./info"); |
||||
|
|
||||
|
const questionProperties = {"q1": q1Props, "q2": q2Props}; |
||||
|
const questionProperty = questionProperties[id]; |
||||
|
|
||||
|
return ( |
||||
|
<div> |
||||
|
<QuestionComponent {...questionProperty} onUpdateAnswerData={questionProperty.handleUpdateAnswerData} /> |
||||
|
<p>Current Answer Data: {JSON.stringify(questionProperty.answerData)}</p> |
||||
|
</div> |
||||
|
); |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue