resizable answers table

This commit is contained in:
Ben Ramey
2018-09-17 06:59:04 -05:00
parent ae0cf1c2b4
commit e52b0b2a34
2 changed files with 66 additions and 12 deletions

View File

@@ -8,9 +8,11 @@ class AnswerKeyForm extends React.Component {
this.state = { this.state = {
courseName: '', courseName: '',
answers: Array(props.numberOfTests * props.numberOfQuestionsPerTest).fill( numTests: 10,
null numQuestionsPerTest: 10,
), answers: Array(
props.defaultNumTests * props.defaultNumQuestionsPerTest
).fill(null),
}; };
this.handleInputChange = this.handleInputChange.bind(this); this.handleInputChange = this.handleInputChange.bind(this);
@@ -18,6 +20,17 @@ class AnswerKeyForm extends React.Component {
console.log(this.state.answers); console.log(this.state.answers);
} }
componentDidUpdate(prevProps, prevState) {
// resize the answers array (and clear it) if
// the size parameters were changed
if (
this.state.numTests !== prevState.numTests ||
this.state.numQuestionsPerTest !== prevState.numQuestionsPerTest
) {
this.updateAnswersSize();
}
}
handleAnswerChange(target, answerIndex) { handleAnswerChange(target, answerIndex) {
const answers = this.state.answers.slice(); const answers = this.state.answers.slice();
answers[answerIndex] = target.value; answers[answerIndex] = target.value;
@@ -29,27 +42,47 @@ class AnswerKeyForm extends React.Component {
console.log(answers); console.log(answers);
} }
updateAnswersSize() {
const newSize = this.state.numTests * this.state.numQuestionsPerTest;
const resizedAnswers = Array(newSize).fill(null);
const prevAnswers = this.state.answers.slice();
for (
let i = 0;
i < Math.min(resizedAnswers.length, prevAnswers.length);
i++
) {
resizedAnswers[i] = prevAnswers[i];
}
this.setState({
answers: resizedAnswers,
});
}
handleInputChange(event) { handleInputChange(event) {
const target = event.target; const target = event.target;
const value = target.value; const value =
target.type === 'number' ? Number(target.value) : target.value;
const name = target.name; const name = target.name;
this.setState({ this.setState({
[name]: value, [name]: value,
}); });
console.log(this.state);
} }
render() { render() {
const testRows = [...Array(this.props.numberOfTests).keys()].map( const testRows = [...Array(this.state.numTests).keys()].map(
(test, index) => ( (test, index) => (
<QuestionsRow <QuestionsRow
key={index} key={index}
testNum={test + 1} testNum={test + 1}
numQuestions={this.props.numberOfQuestionsPerTest} numQuestions={this.state.numQuestionsPerTest}
onChange={(target, question) => onChange={(target, question) =>
this.handleAnswerChange(target, (test + 1) * question) this.handleAnswerChange(
target,
test * this.state.numQuestionsPerTest + question
)
} }
/> />
) )
@@ -67,17 +100,38 @@ class AnswerKeyForm extends React.Component {
onChange={this.handleInputChange} onChange={this.handleInputChange}
/> />
<div className="row">
<div className="six columns">
<label>Num. tests</label>
<input
type="number"
name="numTests"
value={this.state.numTests}
onChange={this.handleInputChange}
/>
</div>
<div className="six columns">
<label>Num. questions per tests</label>
<input
type="number"
name="numQuestionsPerTest"
value={this.state.numQuestionsPerTest}
onChange={this.handleInputChange}
/>
</div>
</div>
<table className="u-full-width answerkeyform__table"> <table className="u-full-width answerkeyform__table">
<thead> <thead>
<tr> <tr>
<th colSpan={this.props.numberOfQuestionsPerTest + 1}> <th colSpan={this.state.numQuestionsPerTest + 1}>
Test answers Test answers
</th> </th>
</tr> </tr>
</thead> </thead>
<tbody>{testRows}</tbody> <tbody>{testRows}</tbody>
</table> </table>
<input class="button-primary" type="submit" value="Save" /> <input className="button-primary" type="submit" value="Save" />
</form> </form>
</div> </div>
); );

View File

@@ -8,7 +8,7 @@ class App extends Component {
<div className="App"> <div className="App">
<NavBar /> <NavBar />
<div className="container"> <div className="container">
<AnswerKeyForm numberOfTests={10} numberOfQuestionsPerTest={10} /> <AnswerKeyForm defaultNumTests={10} defaultNumQuestionsPerTest={10} />
</div> </div>
</div> </div>
); );