This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
QuestionFields.jsx
426 lines (401 loc) · 15.5 KB
/
QuestionFields.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import React from 'react';
import _ from 'lodash';
import * as helpers from './HelperFields.jsx';
// A base Higher-Order Component providing common behaviour for all Question
// Fields.
const baseQuestionField = QuestionField => {
const BaseQuestionField = React.createClass({
_isSub() {
/* Return a boolean to determine if the question should be considered a 'sub-
question', based on the value of `position`.
e.g.
`1` would return False
`1.1` would return True
*/
return (this.props.position % 1 !== 0);
},
getClassValues() {
var classValue = '';
if (!this.props.visibleProps.enabled) classValue += 'disabled ';
if (!this.props.visibleProps.visible) classValue += 'hide ';
if (this.props.visibleProps.required) classValue += 'required ';
if (this.props.position === 1) classValue += 'first ';
if (this._isSub()) classValue += 'sub ';
return _.trim(classValue);
},
render() {
return <QuestionField getClassValues={this.getClassValues}
{...this.props}
{...this.state} />;
}
});
return BaseQuestionField;
};
let QuestionFieldText = React.createClass({
render() {
return (<div className={'text question ' + this.props.getClassValues()}>
<div className="main">
<div>
<helpers.QuestionInstructions instructionText={this.props.instructions}
id={this.props.id} />
<helpers.QuestionHeader label={this.props.label}>
{this.props.children.toString()}
</helpers.QuestionHeader>
</div>
<div>
<helpers.CurrentEntry currentValue={this.props.currentValue} />
<div className="answer-wrapper">
<div className="answer">
<input type="text"
value={this.props.value}
name={this.props.id}
onChange={this.handler}
disabled={!this.props.visibleProps.enabled} />
</div>
</div>
</div>
</div>
<helpers.QuestionComments id={this.props.id}
placeholder={this.props.placeholder}
commentValue={this.props.commentValue}
onCommentChange={this.props.onCommentChange}
disabled={!this.props.visibleProps.enabled} />
</div>);
},
handler(e) {
this.props.onChange(this, e.target.value);
}
});
QuestionFieldText = baseQuestionField(QuestionFieldText);
let QuestionFieldYesNo = React.createClass({
render() {
return (<div className={'yes-no question ' + this.props.getClassValues()}>
<div className="main">
<div>
<helpers.QuestionInstructions instructionText={this.props.instructions}
id={this.props.id} />
<helpers.QuestionHeader label={this.props.label}>
{this.props.children.toString()}
</helpers.QuestionHeader>
</div>
<div>
<helpers.CurrentEntry currentValue={this.props.currentValue} />
<div className="answer-wrapper">
<div className="answer">
<input type="radio"
name={this.props.id}
id={this.props.id + '1'}
value="No"
checked={(this.props.value === 'No')}
disabled={!this.props.visibleProps.enabled}
onChange={this.handler} />
<label htmlFor={this.props.id + '1'}>
<span>No</span>
</label>
<input type="radio"
name={this.props.id}
id={this.props.id + '2'}
value="Yes"
checked={(this.props.value === 'Yes')}
disabled={!this.props.visibleProps.enabled}
onChange={this.handler} />
<label htmlFor={this.props.id + '2'}>
<span>Yes</span>
</label>
</div>
</div>
</div>
</div>
<helpers.QuestionComments id={this.props.id}
placeholder={this.props.placeholder}
commentValue={this.props.commentValue}
onCommentChange={this.props.onCommentChange}
disabled={!this.props.visibleProps.enabled} />
</div>);
},
handler(e) {
this.props.onChange(this, e.target.value);
}
});
QuestionFieldYesNo = baseQuestionField(QuestionFieldYesNo);
const QuestionFieldLikertOption = props => {
return (
<span>
<input type="radio"
name={props.id}
id={props.id + props.value}
value={props.value}
onChange={props.handler}
checked={props.checked}
disabled={props.disabled} />
<label htmlFor={props.id + props.value}>
<span>{props.value}</span> <em className="description">{props.description}</em>
</label>
</span>
);
};
let QuestionFieldLikert = React.createClass({
render() {
let options = _.get(this.props.config, 'options', []);
let scaleOptionNodes = _.map(options, option => {
return <QuestionFieldLikertOption id={this.props.id}
value={option.value}
description={option.description}
key={this.props.id + option.value}
handler={this.handler}
checked={this.props.value === option.value}
disabled={!this.props.visibleProps.enabled} />;
});
return (<div className={'scale question ' + this.props.getClassValues()}>
<div className="main">
<div>
<helpers.QuestionInstructions instructionText={this.props.instructions}
id={this.props.id} />
<helpers.QuestionHeader label={this.props.label}>
{this.props.children.toString()}
</helpers.QuestionHeader>
</div>
<div>
<helpers.CurrentEntry currentValue={this.props.currentValue} />
<div className="answer-wrapper">
<div className="answer">
{scaleOptionNodes}
</div>
</div>
</div>
</div>
<helpers.QuestionComments id={this.props.id}
placeholder={this.props.placeholder}
commentValue={this.props.commentValue}
onCommentChange={this.props.onCommentChange}
disabled={!this.props.visibleProps.enabled} />
</div>);
},
handler(e) {
this.props.onChange(this, e.target.value);
}
});
QuestionFieldLikert = baseQuestionField(QuestionFieldLikert);
const QuestionFieldSourceLine = props => {
return (
<ul>
<li>
<label htmlFor={props.id + '_url'}>Source URL</label>
<input id={props.id + '_url'}
name={props.id + '_url'}
type="url"
data-key={'urlValue'}
placeholder="http://"
value={props.urlValue}
onChange={props.onChange}
disabled={props.disabled} />
</li>
<li>
<label htmlFor={props.id + '_desc'}>Source description</label>
<input id={props.id + '_desc'}
name={props.id + '_desc'}
type="text"
data-key={'descValue'}
value={props.descValue}
onChange={props.onChange}
disabled={props.disabled} />
</li>
</ul>
);
};
let QuestionFieldSource = React.createClass({
emptySource: {urlValue: '', descValue: ''},
_getSourceValues() {
let sourceValues = (_.isArray(this.props.value)) ? this.props.value : [];
if (!_.isEqual(_.last(sourceValues), this.emptySource))
sourceValues.push(_.clone(this.emptySource));
return sourceValues;
},
componentWillMount() {
// Split the current value object up into a list for display by
// helpers.CurrentEntry.
this.currentValue = _.filter(this.props.currentValue, val =>
(val.urlValue || val.descValue));
this.currentValue = _.map(this.currentValue, (val, i) =>
<ul key={i}><li>{val.urlValue}</li><li>{val.descValue}</li></ul>);
},
render() {
let sourceLines = [];
let sourceValues = this._getSourceValues();
for (var i = 0; i < sourceValues.length; i++) {
let sourceValue = sourceValues[i];
let node = <QuestionFieldSourceLine key={this.props.id + i}
id={this.props.id + i}
urlValue={sourceValue.urlValue}
descValue={sourceValue.descValue}
onChange={this.handler.bind(this, i)}
disabled={!this.props.visibleProps.enabled}
/>;
sourceLines.push(node);
}
return (<div className={'source question ' + this.props.getClassValues()}>
<div className="main">
<div>
<helpers.QuestionInstructions instructionText={this.props.instructions}
id={this.props.id} />
<helpers.QuestionHeader label={this.props.label}>
{this.props.children.toString()}
</helpers.QuestionHeader>
</div>
<div>
<helpers.CurrentEntry currentValue={this.currentValue} />
<div className="answer-wrapper">
<div className="answer">
{sourceLines}
</div>
</div>
</div>
</div>
<helpers.QuestionComments id={this.props.id}
placeholder={this.props.placeholder}
commentValue={this.props.commentValue}
onCommentChange={this.props.onCommentChange}
disabled={!this.props.visibleProps.enabled}
disabled={!this.props.visibleProps.enabled} />
</div>);
},
handler(i, e) {
let newSourceValues = this._getSourceValues();
newSourceValues[i] = _.assign(newSourceValues[i],
{[e.target.dataset.key]: e.target.value});
newSourceValues = _.reject(newSourceValues, this.emptySource);
this.props.onChange(this, newSourceValues);
}
});
QuestionFieldSource = baseQuestionField(QuestionFieldSource);
const QuestionFieldMultipleChoiceOption = props => {
return (
<li>
<input type="checkbox"
name={props.id}
id={props.id}
value="1"
checked={props.checked}
onChange={props.handler}
disabled={props.disabled} />
<label htmlFor={props.id}>
<span className="letter">{props.label}</span> <span className="description">{props.children.toString()}</span>
</label>
</li>
);
};
const QuestionFieldMultipleChoiceOther = props => {
if (props.includeOther) {
return (
<div className="other text sub">
<h3>Other</h3>
<div className="answer">
<input name={props.id}
value={props.value}
type="text"
disabled={props.disabled} />
</div>
</div>
);
}
return null;
};
let QuestionFieldMultipleChoice = React.createClass({
getDefaultOptionValuesForOptionList(optionList) {
return _.map(optionList, option => {
return {description: option, checked: false};
});
},
componentWillMount() {
// Set the defaultOptions collection, either from a list of
// `options` in the Question's config, or from the context using a key
// defined in the Question's config (`optionsContextKey`).
let defaultOptions = [];
if (_.has(this.props.config, 'optionsContextKey')) {
// Config directs to get the value from the context for the key set in
// `optionsContextKey`.
let contextOptions =
this.props.context[this.props.config.optionsContextKey];
defaultOptions =
this.getDefaultOptionValuesForOptionList(contextOptions);
}
if (_.has(this.props.config, 'options')) {
// Config has a list of options to use directly.
defaultOptions =
this.getDefaultOptionValuesForOptionList(this.props.config.options);
}
// Merge the defaultOptions with those from the value in props to
// get the value store we'll use for the render.
this.optionValues = _.assign(defaultOptions, this.props.value);
this.orderOptions = _.get(this.props.config, 'orderOptions', false);
},
render() {
if (this.orderOptions) {
this.optionValues = _.sortBy(this.optionValues, 'description');
}
let choices = _.map(this.optionValues, (option, i) => {
// i ==> letter, good for the first 26 options!
let label = String.fromCharCode(97 + i).toUpperCase();
return <QuestionFieldMultipleChoiceOption key={i}
id={this.props.id + i}
checked={option.checked}
label={label}
handler={this.handler.bind(this, i)}
disabled={!this.props.visibleProps.enabled}>
{option.description}
</QuestionFieldMultipleChoiceOption>;
});
let currentValue = _.filter(this.props.currentValue, option => option.checked);
currentValue = _.map(currentValue, option => option.description);
if (this.orderOptions) currentValue = _.sortBy(currentValue);
return (<div className={'multiple question ' + this.props.getClassValues()}>
<div className="main">
<div>
<helpers.QuestionInstructions instructionText={this.props.instructions}
id={this.props.id} />
<helpers.QuestionHeader label={this.props.label}>
{this.props.children.toString()}
</helpers.QuestionHeader>
</div>
<div>
<helpers.CurrentEntry currentValue={currentValue} />
<div className="answer-wrapper">
<div className={this._getAnswerClassNames()}>
<ul>
{choices}
</ul>
</div>
<QuestionFieldMultipleChoiceOther includeOther={this.props.config.includeOther}
id={this.props.id + '_other'}
disabled={!this.props.visibleProps.enabled} />
</div>
</div>
</div>
<helpers.QuestionComments id={this.props.id}
placeholder={this.props.placeholder}
commentValue={this.props.commentValue}
onCommentChange={this.props.onCommentChange}
disabled={!this.props.visibleProps.enabled} />
</div>);
},
_getAnswerClassNames() {
let classNames = 'answer';
if (_.get(this.props.config, 'short', false)) {
classNames += ' short';
}
return classNames;
},
handler(i, e) {
let newOptionValues = this.optionValues;
newOptionValues[i].checked = e.target.checked;
this.props.onChange(this, newOptionValues);
}
});
QuestionFieldMultipleChoice = baseQuestionField(QuestionFieldMultipleChoice);
export {
QuestionFieldText,
QuestionFieldYesNo,
QuestionFieldLikert,
QuestionFieldSource,
QuestionFieldMultipleChoice
};