aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Question.tsx
diff options
context:
space:
mode:
authorGravatar ks129 <[email protected]>2021-05-07 19:46:28 +0300
committerGravatar ks129 <[email protected]>2021-05-07 19:46:28 +0300
commit39704af82ccd7772ef47c7ec889f3db74efc5523 (patch)
tree1f97bf4da0e021ce7e3102186e88724339a6bd30 /src/components/Question.tsx
parentInstall CodeMirror packages (diff)
Implement code field
Diffstat (limited to 'src/components/Question.tsx')
-rw-r--r--src/components/Question.tsx20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/components/Question.tsx b/src/components/Question.tsx
index 0af745e..e6bb845 100644
--- a/src/components/Question.tsx
+++ b/src/components/Question.tsx
@@ -28,6 +28,8 @@ class RenderedQuestion extends React.Component<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 {
this.handler = this.normal_handler.bind(this);
}
@@ -47,7 +49,7 @@ class RenderedQuestion extends React.Component<QuestionProp> {
}
// This is here to allow dynamic selection between the general handler, and the textarea handler.
- handler(_: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>): void {} // eslint-disable-line
+ handler(_: ChangeEvent<HTMLInputElement | HTMLTextAreaElement> | string): void {} // eslint-disable-line
blurHandler(): void {
if (this.props.question.required) {
@@ -132,6 +134,21 @@ class RenderedQuestion extends React.Component<QuestionProp> {
this.setPublicState("value", event.target.value);
}
+ 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.props.public_state.get("value")) {
+ validate = true;
+ }
+
+ this.setPublicState("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;
@@ -142,6 +159,7 @@ class RenderedQuestion extends React.Component<QuestionProp> {
switch (this.props.question.type) {
case QuestionType.TextArea:
case QuestionType.ShortText:
+ case QuestionType.Code:
if (this.props.public_state.get("value") === "") {
invalid = true;
}