-
Notifications
You must be signed in to change notification settings - Fork 4
/
choice.js
106 lines (101 loc) · 3.55 KB
/
choice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React, { useState, useCallback } from 'react'
import classNames from 'classnames'
import { Button } from './button'
import classes from '../styles/choice.module.sass'
const Choice = ({ id = '0', children = [] }) => {
//var selected_buffer
const [asel, setAsel] = useState([])
function updateSelection (asel_tmp, i) {
if (asel_tmp.includes(i)) {
// Remove i from selected items and set selection to undefined:
for (var k = 0; k < asel_tmp.length; k++) {
if (asel_tmp[k] === i) {
asel_tmp.splice(k, 1)
setSelected(undefined)
}
}
} else {
asel_tmp.push(i)
setSelected(i)
}
if (selected === undefined) {
setSelected(i)
}
return asel_tmp
}
function changeUpdate (i) { }
var rr_corr = []
const [selected, setSelected] = useState(null)
const [answer, setAnswer] = useState(null)
const handleAnswer = useCallback(() => setAnswer(selected), [selected])
const options = children.filter(child => child !== '\n')
// Required to prevent the "Submit" button from not reacting due to undefined selected:
{options.map(({ key, props }, i) => {
const isCorrect = props.correct
if (isCorrect) { rr_corr.push(i) }
})}
const [all_correct, setCorrect] = useState(null)
function updateAnswer () {
setAnswer(rr_corr[0])
var has_equal_length = asel.length === rr_corr.length
var tmp = true
if (has_equal_length) {
for (var k = 0; k < asel.length; k++) {
if (!rr_corr.includes(asel[k])) {
tmp = false
}
}
} else {
tmp = false
}
setCorrect(tmp)
}
return (
<>
{options.map(({ key, props }, i) => (
<form key={key} className={classes.option}>
<input
className={classes.input}
name={`choice-${id}`}
id={`choice-${id}-${i}`}
value={i}
type="radio"
checked={asel.includes(i)}
onChange={() => changeUpdate(i)}
onClick={() => setAsel(updateSelection(asel, i))}
/>
<label
className={classes.label}
htmlFor={`choice-${id}-${i}`}
dangerouslySetInnerHTML={{ __html: `<span>${props.text}</span>` }}
/>
</form>
))}
<Button variant="primary" onClick={updateAnswer}>
Submit
</Button>
{options.map(({ key, props }, i) => {
const isCorrect = !!props.correct
return answer === i ? (
<div
key={key}
className={classNames(classes.answer, { [classes.correct]: isCorrect && all_correct })}
>
<strong
className={classNames(classes.answerLabel, {
[classes.answerLabelCorrect]: isCorrect && all_correct,
})}
>
{isCorrect && all_correct ? "That's correct! " : 'Incorrect. '}
</strong>
{props.children}
</div>
) : null
})}
</>
)
}
export const Option = ({ children }) => {
return children
}
export default Choice