-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from bigrobsf/multiple_choice
Multiple choice
- Loading branch information
Showing
9 changed files
with
224 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React, { Component } from "react"; | ||
import PropTypes from "prop-types"; | ||
import "./MultipleChoiceQuestionForm.css"; | ||
|
||
class MultipleChoiceQuestionForm extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
title: "", | ||
choices: [] | ||
}; | ||
this.handleAdd = this.handleAdd.bind(this); | ||
this.handleChange = this.handleChange.bind(this); | ||
this.handleClick = this.handleClick.bind(this); | ||
this.handleRemove = this.handleRemove.bind(this); | ||
this.handleChoiceChange = this.handleChoiceChange.bind(this); | ||
} | ||
|
||
handleChange(e) { | ||
this.setState({ | ||
[e.target.name]: e.target.value | ||
}); | ||
} | ||
|
||
handleAdd(e) { | ||
e.preventDefault(); | ||
if (this.state.choices.length === 0) return; | ||
this.props.addQuestion({ | ||
title: this.state.title, | ||
choices: this.state.choices | ||
}); | ||
this.setState({ title: "", choices: [] }); | ||
} | ||
|
||
handleClick() { | ||
this.setState({ choices: [...this.state.choices, ""] }); | ||
} | ||
|
||
handleRemove(i) { | ||
let choices = this.state.choices.filter((e, index) => index !== i); | ||
this.setState({ choices }); | ||
} | ||
|
||
handleChoiceChange(e) { | ||
let choices = [...this.state.choices]; | ||
choices[e.target.name] = e.target.value; | ||
this.setState({ choices }); | ||
} | ||
|
||
render() { | ||
let inputs = this.state.choices.map((e, i) => ( | ||
<span className="choice" key={"span" + i}> | ||
<input | ||
type="text" | ||
name={i} | ||
placeholder="answer" | ||
key={i} | ||
value={this.state.choices[i]} | ||
onChange={this.handleChoiceChange} | ||
required | ||
/> | ||
<button className="remove button" key={"butt-" + i} onClick={() => this.handleRemove(i)}> | ||
X | ||
</button> | ||
</span> | ||
)); | ||
return ( | ||
<div className="forms"> | ||
<form className="addQuestion" onSubmit={this.handleAdd}> | ||
<h4>Add Question</h4> | ||
<input | ||
className="quest" | ||
type="text" | ||
onChange={this.handleChange} | ||
name="title" | ||
placeholder="Add A Question" | ||
value={this.state.title} | ||
required | ||
/> | ||
<button className="button ac" type="button" onClick={this.handleClick}> | ||
Add choice | ||
</button> | ||
{inputs} | ||
<button type="submit" className="button" value="Add"> | ||
+ | ||
</button> | ||
</form> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
MultipleChoiceQuestionForm.propTypes = { | ||
addQuestion: PropTypes.func.isRequired | ||
}; | ||
|
||
export default MultipleChoiceQuestionForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.choice { | ||
padding: 0px; | ||
} | ||
|
||
.choice input { | ||
margin: 0px; | ||
} | ||
|
||
button.remove { | ||
margin: 0px; | ||
} | ||
|
||
button.ac { | ||
margin-bottom: 10px; | ||
} | ||
|
||
input.quest { | ||
margin-bottom: 2px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.