-
Notifications
You must be signed in to change notification settings - Fork 479
/
single_choice_responses.jsx
73 lines (69 loc) · 2.24 KB
/
single_choice_responses.jsx
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
import React, {PropTypes} from 'react';
import _ from 'lodash';
import {Panel} from 'react-bootstrap';
export default class SingleChoiceResponses extends React.Component {
static propTypes = {
question: PropTypes.string.isRequired,
answers: PropTypes.object.isRequired,
answerType: PropTypes.string.isRequired,
possibleAnswers: PropTypes.array.isRequired,
otherAnswers: PropTypes.array //Free response "other" answers
};
render() {
let totalAnswers = Object.values(this.props.answers).reduce((sum, x) => sum + x, 0);
return (
<Panel>
{this.props.question}
<table>
<tbody>
{
this.props.possibleAnswers.map((possibleAnswer, i) => {
let answerIndex;
if (this.props.answerType === 'selectValue') {
// For selectValue questions, the value is not the text of the response
// but a numeric value in some range. The hash contains a map of the
// numeric values to counts, not texts to counts.
answerIndex = i + 1;
} else {
answerIndex = possibleAnswer;
}
let count = this.props.answers[answerIndex] || 0;
return (
<tr key={i}>
<td>
{
(count / totalAnswers)
.toLocaleString('en-US', {
style: 'percent',
minimumFractionDigits: 2,
maximumFractionDigits: 2
})
}
</td>
<td style={{paddingLeft: '20px'}}>
{count}
</td>
<td style={{paddingLeft: '20px'}}>
{possibleAnswer}
</td>
</tr>
);
})
}
</tbody>
</table>
{
this.props.otherAnswers && (
<div>
<br/>
Other:
<ul>
{_.compact(this.props.otherAnswers).map((answer, i) => (<li key={i}>{answer}</li>))}
</ul>
</div>
)
}
</Panel>
);
}
}