Skip to content

Instantly share code, notes, and snippets.

@robinheinze
Last active April 8, 2021 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinheinze/3240641bdb52405b52a174f0efdeee23 to your computer and use it in GitHub Desktop.
Save robinheinze/3240641bdb52405b52a174f0efdeee23 to your computer and use it in GitHub Desktop.
Question Screen with radio buttons
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