Working answer key form (saving to state at least)

This commit is contained in:
Ben Ramey
2018-09-17 06:14:09 -05:00
parent c15cbbf5ae
commit 0a8421fad3
3 changed files with 16 additions and 10 deletions

View File

@@ -4,9 +4,8 @@ export default function(props) {
const cells = [...Array(props.numQuestions).keys()].map((question, index) => ( const cells = [...Array(props.numQuestions).keys()].map((question, index) => (
<td key={index}> <td key={index}>
<input <input
name={'answer_' + props.testNum + ',' + question}
type="text" type="text"
onChange={props.handleInputChange} onChange={event => props.onChange(event.target, question)}
/> />
</td> </td>
)); ));

View File

@@ -2,10 +2,12 @@
} }
.answerkeyform__table input { .answerkeyform__table input {
width: 4rem; width: 3rem;
height: 3rem;
margin: 0; margin: 0;
padding: 1rem;
} }
.answerkeyform__table__questionrow td { .answerkeyform__table__questionrow td {
padding: 1rem; padding: 0.5rem;
} }

View File

@@ -8,23 +8,25 @@ class AnswerKeyForm extends React.Component {
this.state = { this.state = {
courseName: '', courseName: '',
answers: Array(props.numberOfTests).fill( answers: Array(props.numberOfTests * props.numberOfQuestionsPerTest).fill(
Array(props.numberOfQuestionsPerTest) null
), ),
}; };
this.handleInputChange = this.handleInputChange.bind(this); this.handleInputChange = this.handleInputChange.bind(this);
this.handleAnswerChange = this.handleAnswerChange.bind(this);
console.log(this.state.answers); console.log(this.state.answers);
} }
handleAnswerChange(test, question) { handleAnswerChange(target, answerIndex) {
const answers = this.state.answers.slice(); const answers = this.state.answers.slice();
const testAnswers = answers[test]; answers[answerIndex] = target.value;
testAnswers[question] = value;
this.setState({ this.setState({
answers: answers, answers: answers,
}); });
console.log(answers);
} }
handleInputChange(event) { handleInputChange(event) {
@@ -46,7 +48,9 @@ class AnswerKeyForm extends React.Component {
key={index} key={index}
testNum={test + 1} testNum={test + 1}
numQuestions={this.props.numberOfQuestionsPerTest} numQuestions={this.props.numberOfQuestionsPerTest}
handleInputChange={() => this.handleAnswerChange(testNum)} onChange={(target, question) =>
this.handleAnswerChange(target, (test + 1) * question)
}
/> />
) )
); );
@@ -63,6 +67,7 @@ class AnswerKeyForm extends React.Component {
onChange={this.handleInputChange} onChange={this.handleInputChange}
/> />
<h2>Test Answers</h2>
<table className="u-full-width answerkeyform__table"> <table className="u-full-width answerkeyform__table">
<thead> <thead>
<tr> <tr>