// NumericalQuestion.tsx import React, { useState } from 'react'; import '../questionStyle.css'; import { Button, TextField } from '@mui/material'; import { FormattedTextTemplate } from '../../GiftTemplate/templates/TextTypeTemplate'; import { NumericalQuestion, SimpleNumericalAnswer, RangeNumericalAnswer, HighLowNumericalAnswer } from 'gift-pegjs'; import { isSimpleNumericalAnswer, isRangeNumericalAnswer, isHighLowNumericalAnswer, isMultipleNumericalAnswer } from 'gift-pegjs/typeGuards'; interface Props { question: NumericalQuestion; handleOnSubmitAnswer?: (answer: number) => void; showAnswer?: boolean; } const NumericalQuestionDisplay: React.FC = (props) => { const { question, showAnswer, handleOnSubmitAnswer } = props; const [answer, setAnswer] = useState(); const correctAnswers = question.choices; let correctAnswer = ''; //const isSingleAnswer = correctAnswers.length === 1; if (isSimpleNumericalAnswer(correctAnswers[0])) { correctAnswer = `${(correctAnswers[0] as SimpleNumericalAnswer).number}`; } else if (isRangeNumericalAnswer(correctAnswers[0])) { const choice = correctAnswers[0] as RangeNumericalAnswer; correctAnswer = `Entre ${choice.number - choice.range} et ${choice.number + choice.range}`; } else if (isHighLowNumericalAnswer(correctAnswers[0])) { const choice = correctAnswers[0] as HighLowNumericalAnswer; correctAnswer = `Entre ${choice.numberLow} et ${choice.numberHigh}`; } else if (isMultipleNumericalAnswer(correctAnswers[0])) { correctAnswer = `MultipleNumericalAnswer is not supported yet`; } else { throw new Error('Unknown numerical answer type'); } return (
{showAnswer ? ( <>
{correctAnswer}
{question.formattedGlobalFeedback &&
} ) : ( <>
) => { setAnswer(e.target.valueAsNumber); }} inputProps={{ 'data-testid': 'number-input' }} />
{question.formattedGlobalFeedback && showAnswer && (
)} {handleOnSubmitAnswer && ( )} )}
); }; export default NumericalQuestionDisplay;