11import React from 'react'
22import { Grid , Fab , Paper } from '@material-ui/core'
3+ import { HotKeys , Handlers } from 'react-keyboard'
4+
5+ const keyMap = {
6+ selectOne : "1" ,
7+ selectTwo : "2" ,
8+ selectThree : "3" ,
9+ selectFour : "4"
10+ }
311
412// colour setup to dynamically draw the colour of an item
513const colors = {
614 standard : "#ffffff" ,
7- selected : "#548BDF"
815}
916
1017/**
@@ -13,44 +20,46 @@ const colors = {
1320* @return {JSX } - the JSX of the component
1421*/
1522function MultiAnswer ( props ) {
16- const [ selected , setSelected ] = React . useState ( null ) // state for storing the selected item
1723
18- const submit = ( e ) => { // function that handles when an answer is submitted
19- let s = selected
20- setSelected ( null )
24+ const submit = ( index ) => { // function that handles when an answer is submitted
25+ let s = index
2126 let correct = ( s === props . question [ "CORRECT" ] )
2227 props . correctAnswer ( correct )
2328 }
2429
2530 const handleSelect = ( index ) => { // function that handles when an answer is selected
26- if ( selected === index ) { // if it is already selected, deselect it
27- setSelected ( null )
28- } else { // otherwise change the highlighted item to the clicked one
29- setSelected ( index )
31+ if ( props . question [ "ANSWERS" ] . length > index ) {
32+ submit ( index )
3033 }
3134 }
3235
36+ const handlers = {
37+ selectOne : ( ) => handleSelect ( 0 ) ,
38+ selectTwo : ( ) => handleSelect ( 1 ) ,
39+ selectThree : ( ) => handleSelect ( 2 ) ,
40+ selectFour : ( ) => handleSelect ( 3 ) ,
41+ }
42+
3343 return (
34- < Grid container style = { { position : "absolute" , bottom : "15%" } } >
35- < Grid item xs = { 4 } > </ Grid >
36- < Grid item container spacing = { 3 } xs = { 4 } >
37- { props . question [ "ANSWERS" ] . map ( ( answer , i ) => {
38- return (
39- < Grid item xs = { 6 } >
40- < Paper onClick = { ( ) => ( handleSelect ( i ) ) } elevation = { 3 } style = { { width : "100%" , height : "5vh" , borderRadius : "10px" , backgroundColor : selected === i ? colors . selected : colors . standard } } >
41- < p style = { { fontSize : 20 , paddingTop : "1.4vh" } } >
42- { answer }
43- </ p >
44- </ Paper >
45- </ Grid >
46- )
47- } ) }
48- </ Grid >
49- < Grid item xs = { 1 } > </ Grid >
50- < Grid item xs = { 1 } >
51- < div style = { { paddingTop : "2vh" } } > < Fab variant = "extended" onClick = { submit } style = { { backgroundColor : "white" } } > Submit</ Fab > </ div >
44+ < HotKeys keyMap = { keyMap } handlers = { handlers } >
45+ < Grid container style = { { position : "absolute" , bottom : "15%" } } >
46+ < Grid item xs = { 4 } > </ Grid >
47+ < Grid item container spacing = { 3 } xs = { 4 } >
48+ { props . question [ "ANSWERS" ] . map ( ( answer , i ) => {
49+ return (
50+ < Grid item xs = { 6 } >
51+ < Paper onClick = { ( ) => ( handleSelect ( i ) ) } elevation = { 3 } style = { { width : "100%" , height : "5vh" , borderRadius : "10px" , backgroundColor : colors . standard } } >
52+ < p style = { { position : "absolute" , paddingLeft : "1vw" , opacity : 0.7 } } > { i + 1 } </ p >
53+ < p style = { { fontSize : 20 , paddingTop : "1vh" } } >
54+ { answer }
55+ </ p >
56+ </ Paper >
57+ </ Grid >
58+ )
59+ } ) }
60+ </ Grid >
5261 </ Grid >
53- </ Grid >
62+ </ HotKeys >
5463 )
5564}
5665
0 commit comments