-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathindex.jsx
119 lines (110 loc) · 2.83 KB
/
index.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
import PropTypes from 'prop-types';
import React from 'react';
import GenericTask from '../generic';
import GenericTaskEditor from '../generic-editor';
import SingleChoiceSummary from './summary';
import TaskInputField from '../components/TaskInputField';
const NOOP = Function.prototype;
export default class SingleChoiceTask extends React.Component {
constructor(props) {
super(props);
}
handleChange(index, e) {
if (e.target.checked) {
const newAnnotation = Object.assign({}, this.props.annotation, { value: index });
this.props.onChange(newAnnotation);
}
}
render() {
const { annotation, task, translation } = this.props;
if (!task._key) {
task._key = Math.random();
}
const answers = [];
for (const [i, answer] of task.answers.entries()) {
if (!answer._key) {
answer._key = Math.random();
}
const checked = i === annotation.value;
answers.push(
<TaskInputField
autoFocus={checked}
checked={checked}
className={checked ? 'active' : ''}
index={i}
key={answer._key}
label={translation.answers[i].label}
name={`${task._key}`}
onChange={this.handleChange.bind(this, i)}
type="radio"
/>
);
}
return (
<GenericTask
autoFocus={annotation.value === null}
question={translation.question}
help={translation.help}
answers={answers}
required={task.required}
showRequiredNotice={this.props.showRequiredNotice}
/>
);
}
}
// Define the static methods and values
SingleChoiceTask.Editor = GenericTaskEditor;
SingleChoiceTask.Summary = SingleChoiceSummary;
SingleChoiceTask.getDefaultTask = () => {
return {
type: 'single',
question: 'Enter a question.',
help: '',
answers: []
};
};
SingleChoiceTask.getTaskText = (task) => {
return task.question;
};
SingleChoiceTask.getDefaultAnnotation = () => {
return { value: null };
};
SingleChoiceTask.isAnnotationComplete = (task, annotation) => {
return (!task.required || annotation.value !== null);
};
SingleChoiceTask.propTypes = {
task: PropTypes.shape(
{
answers: PropTypes.array,
question: PropTypes.string,
help: PropTypes.string,
required: PropTypes.bool
}
),
translation: PropTypes.shape({
characteristics: PropTypes.object,
choices: PropTypes.object,
questions: PropTypes.object
}).isRequired,
annotation: PropTypes.shape(
{ value: PropTypes.number }
),
onChange: PropTypes.func,
showRequiredNotice: PropTypes.bool
};
SingleChoiceTask.defaultProps = {
task: {
answers: [],
question: '',
help: '',
required: false
},
translation: {
answers: [],
question: '',
help: ''
},
annotation: { value: null },
onChange: NOOP,
showRequiredNotice: false
};