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
|
/** @jsx jsx */
import { jsx, css } from "@emotion/react";
import React, { ChangeEvent } from "react";
import { Question, QuestionType } from "../api/question";
import { selectable } from "../commonStyles";
import create_input from "./InputTypes";
import ErrorMessage from "./ErrorMessage";
const skip_normal_state: Array<QuestionType> = [
QuestionType.Radio,
QuestionType.Checkbox,
QuestionType.Select,
QuestionType.TimeZone,
QuestionType.Section,
QuestionType.Range,
QuestionType.Vote
];
export interface QuestionState {
// Common keys
value: string | null | Map<string, boolean> | Record<string, number | null>
// Validation
valid: boolean
error: string
// Unittest-specific validation
unittestsFailed: boolean // This indicates a failure in testing when submitting (i.e not from common validation)
testFailure: boolean // Whether we had failed unittests, or other failures, such as code loading
}
export type QuestionProp = {
question: Question,
scroll_ref: React.RefObject<HTMLDivElement>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
focus_ref: React.RefObject<any>,
selfRef: React.RefObject<RenderedQuestion>,
}
class RenderedQuestion extends React.Component<QuestionProp> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: TS2610
state: QuestionState;
/** The current state of the question components, which may or may not match the state rendered. */
public realState: QuestionState;
setState(state: Partial<QuestionState>): void {
this.realState = {...this.realState, ...state};
super.setState(state);
}
constructor(props: QuestionProp) {
super(props);
if (props.question.type === QuestionType.TextArea) {
this.handler = this.text_area_handler.bind(this);
} else if (props.question.type === QuestionType.Code) {
this.handler = this.code_field_handler.bind(this);
} else if (props.question.type === QuestionType.Vote) {
this.handler = this.vote_handler.bind(this);
} else {
this.handler = this.normal_handler.bind(this);
}
this.blurHandler = this.blurHandler.bind(this);
this.state = {
value: skip_normal_state.includes(props.question.type) ? null : "",
valid: true,
error: "",
unittestsFailed: false,
testFailure: false,
};
this.realState = this.state;
}
// This is here to allow dynamic selection between the general handler, textarea, and code field handlers.
handler(_: ChangeEvent<HTMLInputElement | HTMLTextAreaElement> | string | Record<string, number | null>): void {} // eslint-disable-line
blurHandler(): void {
if (this.props.question.required) {
if (!this.realState.value) {
this.setState({
error: "Field must be filled.",
valid: false
});
} else {
this.setState({
error: "",
valid: true,
unittestsFailed: false
});
}
}
}
normal_handler(event: ChangeEvent<HTMLInputElement>): void {
switch (event.target.type) {
case "checkbox":
if (!(this.realState.value instanceof Map)) return;
this.realState.value.set(event.target.name, event.target.checked);
break;
case "radio": {
// This handles radios and ranges, as they are both based on the same fundamental input type
let value;
if (event.target.parentElement) {
value = event.target.parentElement.innerText.trimEnd();
} else {
value = event.target.value;
}
this.setState({value: value});
break;
}
default:
this.setState({value: event.target.value});
}
// Toggle checkbox class
if (event.target.type === "checkbox" && event.target.parentElement !== null) {
event.target.parentElement.classList.toggle("unselected");
event.target.parentElement.classList.toggle("selected");
}
const options: string | string[] = this.props.question.data["options"];
switch (event.target.type) {
case "text":
this.setState({valid: true});
break;
case "checkbox":
// We need to check this here, because checkbox doesn't have onBlur
if (this.props.question.required && typeof options !== "string") {
if (!(this.realState.value instanceof Map)) return;
const valid = Array.from(this.realState.value.values()).includes(true);
this.setState({
error: valid ? "" : "Field must be filled.",
valid: valid
});
}
break;
case "radio":
this.setState({
valid: true,
error: ""
});
break;
}
}
vote_handler(event: Record<string, number | null>) {
this.setState({
value: event,
valid: true,
error: ""
});
}
text_area_handler(event: ChangeEvent<HTMLTextAreaElement>): void {
// We will validate again when focusing out.
this.setState({
value: event.target.value,
valid: true,
error: ""
});
}
code_field_handler(newContent: string): void {
// If content stays same (what means that user have just zoomed in), then don't validate.
let validate = false;
if (newContent != this.realState.value) {
validate = true;
}
this.setState({value: newContent});
// CodeMirror don't provide onBlur event, so we have to run validation here.
if (validate) {
this.blurHandler();
}
}
validateField(): void {
if (!this.props.question.required) {
return;
}
let valid = true;
const options: string | string[] = this.props.question.data["options"];
switch (this.props.question.type) {
case QuestionType.TextArea:
case QuestionType.ShortText:
case QuestionType.Code:
if (this.realState.value === "") {
valid = false;
}
break;
case QuestionType.Select:
case QuestionType.Range:
case QuestionType.TimeZone:
case QuestionType.Vote:
case QuestionType.Radio:
if (!this.realState.value) {
valid = false;
}
break;
case QuestionType.Checkbox:
if (typeof options !== "string") {
if (!(this.realState.value instanceof Map)) return;
valid = Array.from(this.realState.value.values()).includes(true);
}
break;
}
this.setState({
error: valid ? "" : "Field must be filled",
valid: valid
});
}
componentDidMount(): void {
// Initialize defaults for complex and nested fields
const options: string | string[] = this.props.question.data["options"];
switch (this.props.question.type) {
case QuestionType.Checkbox:
if (typeof options === "string") return;
this.setState({
value: new Map(options.map((option, index) =>
[`${("000" + index).slice(-4)}. ${option}`, false]
))
});
break;
}
}
generateUnitTestErrorMessage(): JSX.Element {
let inner;
if (this.realState.testFailure) {
inner = <div>
{"Unittest Failure:\n"}
<ul css={css`font-size: 1rem;`}>
{this.realState.error.split(";").map(testName =>
<li css={css`letter-spacing: 0.5px;`} key={testName}>{testName}</li>
)}
</ul>
</div>;
} else {
inner = `Unittest Failure:\n\n${this.realState.error}`;
}
const element = <div css={css`white-space: pre-wrap; word-wrap: break-word;`}>{inner}</div>;
return <ErrorMessage show={!this.realState.valid} content={element}/>;
}
render(): JSX.Element {
const question = this.props.question;
const name = question.name.split("\n").map((line, index) => <span key={index}>{line}<br/></span>);
name.push(<span key={name.length - 1}>{name.pop()?.props.children[0]}</span>);
if (question.type === QuestionType.Section) {
const styles = css`
h1 {
margin-bottom: 0;
}
h3 {
margin-top: 0;
}
h1, h3 {
text-align: center;
padding: 0 2rem;
}
@media (max-width: 500px) {
h1, h3 {
padding: 0;
}
}
`;
const data = question.data["text"];
let text;
if (data && typeof(data) === "string") {
text = data.split("\n").map((line, index) => <h3 css={selectable} key={index}>{line}<br/></h3>);
text.push(<h3 css={selectable} key={data.length - 1}>{text.pop()?.props.children[0]}</h3>);
} else {
text = "";
}
return <div css={styles}>
<h1 css={[selectable, css`line-height: 2.5rem;`]}>{name}</h1>
{ text }
<hr css={css`color: gray; margin: 3rem 0;`}/>
</div>;
} else {
const requiredStarStyles = css`
.required {
display: inline-block;
position: relative;
color: red;
top: -0.2rem;
margin-left: 0.2rem;
}
`;
let error;
if (this.props.question.type === QuestionType.Code && this.realState.unittestsFailed) {
error = this.generateUnitTestErrorMessage();
} else {
error = <ErrorMessage show={!this.realState.valid} content={this.realState.error}/>;
}
return <div ref={this.props.scroll_ref}>
<h2 css={[selectable, requiredStarStyles]}>
{name}<span css={css`display: none;`} className={question.required ? "required" : ""}>*</span>
</h2>
{ create_input(this, this.handler, this.blurHandler, this.props.focus_ref) }
{error}
<hr css={css`color: gray; margin: 3rem 0;`}/>
</div>;
}
}
}
export default RenderedQuestion;
|