Last active
April 8, 2021 17:26
-
-
Save robinheinze/3240641bdb52405b52a174f0efdeee23 to your computer and use it in GitHub Desktop.
Question Screen with radio buttons
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
const CHECK_ANSWER: ViewStyle = { | |
paddingVertical: spacing.smaller, | |
backgroundColor: color.palette.angry, | |
marginTop: spacing.smaller, | |
} | |
export const QuestionScreen = observer(function QuestionScreen() { | |
... | |
const onPressAnswer = (question: Question, guess: string) => { | |
question.setGuess(guess) | |
} | |
const checkAnswer = (question: Question) => { | |
if (question.isCorrect) { | |
Alert.alert("That is correct!") | |
} else { | |
Alert.alert(`Wrong! The correct answer is: ${question.correctAnswer}`) | |
} | |
} | |
const renderAnswer = (answer: string, selected: boolean, onSelect: () => void, index) => { | |
const style: TextStyle = selected ? { fontWeight: "bold", fontSize: 14 } : {} | |
return ( | |
<TouchableOpacity key={index} onPress={onSelect} style={ANSWER_WRAPPER}> | |
<Text style={{ ...ANSWER, ...style }} text={decodeHTMLEntities(answer)} /> | |
</TouchableOpacity> | |
) | |
} | |
const renderQuestion = ({ item }) => { | |
const question: Question = item | |
return ( | |
<View style={QUESTION_WRAPPER}> | |
<Text style={QUESTION} text={decodeHTMLEntities(question.question)} /> | |
<RadioButtons | |
options={question.allAnswers} | |
onSelection={(guess) => onPressAnswer(question, guess)} | |
selectedOption={question.guess} | |
renderOption={renderAnswer} | |
/> | |
<Button style={CHECK_ANSWER} onPress={() => checkAnswer(question)} text={"Check Answer!"} /> | |
</View> | |
) | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment