forked from eyra/feldspar
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from d3i-infra/questionnaire-module
added questionnaire-module back
- Loading branch information
Showing
9 changed files
with
438 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/framework/visualisation/react/ui/elements/question_multiple_choice.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from 'react' | ||
import { PropsUIQuestionMultipleChoice } from '../../../../types/elements' | ||
import { Translator } from '../../../../translator' | ||
import { ReactFactoryContext } from '../../factory' | ||
import { Title3 } from './text' | ||
|
||
interface parentSetter { | ||
parentSetter: (arg: any) => any | ||
} | ||
|
||
type Props = PropsUIQuestionMultipleChoice & parentSetter & ReactFactoryContext | ||
|
||
export const MultipleChoiceQuestion = (props: Props): JSX.Element => { | ||
const { question, choices, id, parentSetter, locale } = props | ||
const [selectedChoice, setSelectedChoice] = React.useState<string>(""); | ||
const [checkedArray, setCheckedArray] = React.useState(Array(choices.length).fill(false)); | ||
|
||
const copy = prepareCopy(locale) | ||
|
||
const handleChoiceSelect = (choice: string, index: number) => { | ||
setSelectedChoice(choice) | ||
setCheckedArray(Array.from({ length: choices.length }, (_, i) => i === index)) | ||
}; | ||
|
||
const setParentState = () => { | ||
parentSetter((prevState: any) => { | ||
prevState[id] = selectedChoice | ||
return prevState | ||
}) | ||
} | ||
|
||
React.useEffect(() => { | ||
setParentState() | ||
}) | ||
|
||
return ( | ||
<div className="p-4"> | ||
<Title3 text={copy.question}/> | ||
<ul className="mt-4 space-y-1"> | ||
{copy.choices.map((choice, index) => ( | ||
<li key={index}> | ||
<label className="inline-flex items-center"> | ||
<input | ||
type="radio" | ||
name={`${index}-${id}`} | ||
value={choice} | ||
checked={checkedArray.at(index)} | ||
onChange={() => handleChoiceSelect(choice, index)} | ||
className="mr-1 form-radio" | ||
/> | ||
</label> | ||
{choice} | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
|
||
function prepareCopy (locale: string): Copy { | ||
return { | ||
choices: choices.map((choice) => Translator.translate(choice, locale)), | ||
question: Translator.translate(question, locale) | ||
} | ||
} | ||
} | ||
|
||
interface Copy { | ||
choices: string[] | ||
question: string | ||
} |
79 changes: 79 additions & 0 deletions
79
src/framework/visualisation/react/ui/elements/question_multiple_choice_checkbox.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React from 'react' | ||
import { PropsUIQuestionMultipleChoiceCheckbox } from '../../../../types/elements' | ||
import { Translator } from '../../../../translator' | ||
import { ReactFactoryContext } from '../../factory' | ||
import {Title3 } from './text' | ||
|
||
interface parentSetter { | ||
parentSetter: (arg: any) => any | ||
} | ||
|
||
type Props = PropsUIQuestionMultipleChoiceCheckbox & parentSetter & ReactFactoryContext | ||
|
||
export const MultipleChoiceQuestionCheckbox = (props: Props): JSX.Element => { | ||
const { question, choices, id, parentSetter, locale } = props | ||
const [selectedChoices, setSelectedChoices] = React.useState<string[]>([]); | ||
|
||
const copy = prepareCopy(locale) | ||
|
||
const setParentState = () => { | ||
parentSetter((prevState: any) => { | ||
prevState[id] = selectedChoices | ||
return prevState | ||
}) | ||
} | ||
|
||
React.useEffect(() => { | ||
setParentState() | ||
}) | ||
|
||
|
||
const handleChoiceSelect = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const { value, checked } = event.target; | ||
if (checked) { | ||
setSelectedChoices((prevSelectedChoices) => [ | ||
...prevSelectedChoices, | ||
value, | ||
]); | ||
} else { | ||
setSelectedChoices((prevSelectedChoices) => | ||
prevSelectedChoices.filter((choice) => choice !== value) | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="p-4"> | ||
<Title3 text={copy.question} /> | ||
<ul className="mt-4 space-y-1"> | ||
{copy.choices.map((choice, index) => ( | ||
<li key={index}> | ||
<label className="flex items-center"> | ||
<input | ||
type="checkbox" | ||
name="choice" | ||
value={choice} | ||
checked={selectedChoices.includes(choice)} | ||
onChange={handleChoiceSelect} | ||
className="mr-1 form-checkbox" | ||
/> | ||
{choice} | ||
</label> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
|
||
function prepareCopy (locale: string): Copy { | ||
return { | ||
choices: choices.map((choice) => Translator.translate(choice, locale)), | ||
question: Translator.translate(question, locale) | ||
} | ||
} | ||
} | ||
|
||
interface Copy { | ||
choices: string[] | ||
question: string | ||
} |
56 changes: 56 additions & 0 deletions
56
src/framework/visualisation/react/ui/elements/question_open.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React from 'react' | ||
import { PropsUIQuestionOpen } from '../../../../types/elements' | ||
import { Translator } from '../../../../translator' | ||
import { ReactFactoryContext } from '../../factory' | ||
|
||
import { Title3 } from './text' | ||
|
||
interface parentSetter { | ||
parentSetter: (arg: any) => any | ||
} | ||
|
||
type Props = PropsUIQuestionOpen & parentSetter & ReactFactoryContext | ||
|
||
export const OpenQuestion = (props: Props): JSX.Element => { | ||
|
||
const { question, id, parentSetter, locale } = props | ||
const [userAnswer, setUserAnswer] = React.useState<string>(""); | ||
const copy = prepareCopy(locale) | ||
|
||
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setUserAnswer(event.target.value); | ||
}; | ||
|
||
const setParentState = () => { | ||
parentSetter((prevState: any) => { | ||
prevState[id] = userAnswer | ||
return prevState | ||
}) | ||
} | ||
|
||
React.useEffect(() => { | ||
setParentState() | ||
}) | ||
|
||
return ( | ||
<div className="p-4"> | ||
<Title3 text={copy.question} /> | ||
<input | ||
type="text" | ||
value={userAnswer} | ||
onChange={handleInputChange} | ||
className="w-full px-4 py-2 text-gray-700 bg-gray-100 border border-gray-300 rounded-md resize-none h-16" | ||
/> | ||
</div> | ||
); | ||
|
||
function prepareCopy (locale: string): Copy { | ||
return { | ||
question: Translator.translate(question, locale) | ||
} | ||
} | ||
} | ||
|
||
interface Copy { | ||
question: string | ||
} |
Oops, something went wrong.