Skip to content

Commit 533b43d

Browse files
authored
Merge pull request #1 from xaiki/master
Children support, internal state to store values & provide values of previous questions
2 parents b5cf917 + dbe341a commit 533b43d

1 file changed

Lines changed: 51 additions & 23 deletions

File tree

src/index.js

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class EurekaForm extends React.Component {
4444
this.state = {
4545
current: 0,
4646
questions: [],
47+
values: {},
4748
wasSubmitted: false
4849
};
4950
}
@@ -159,7 +160,7 @@ class EurekaForm extends React.Component {
159160
if (!input.checkValidity()) {
160161
// Optionally, set a custom HTML5 valiation message
161162
// comment or remove this line to use the browser default message
162-
input.setCustomValidity('Whoops, that\'s not an email address!');
163+
//input.setCustomValidity('Whoops, that\'s not an email address!');
163164

164165
// display the HTML5 error message
165166
this._showError(input.validationMessage);
@@ -180,6 +181,7 @@ class EurekaForm extends React.Component {
180181
// current question
181182
const currentQuestion = this.state.questions[this.state.current];
182183
currentQuestion.querySelector('input, textarea, select').blur();
184+
this._setValue(currentQuestion)
183185

184186
this.setState({
185187
...this.state,
@@ -296,27 +298,32 @@ class EurekaForm extends React.Component {
296298
this.error.classList.remove('show');
297299
}
298300

301+
_setValue(question) {
302+
const questionInput = question.querySelector('input, textarea, select');
303+
questionInput.setAttribute("disabled", true);
304+
const key = questionInput.getAttribute("id")
305+
const newState = {
306+
...this.state,
307+
values: {
308+
...this.state.values,
309+
[key]: questionInput.value
310+
}
311+
}
312+
this.setState(newState)
313+
this.props.onUpdate(newState)
314+
}
315+
299316
_submit() {
300317
if (!this.state.wasSubmitted) {
301318
this.setState({
302319
...this.state,
303320
wasSubmitted: true
304321
}, () => {
305-
const values = [];
306-
307-
// Disable all inputs
308-
this.state.questions.forEach(question => {
309-
const questionInput = question.querySelector('input, textarea, select');
310-
questionInput.setAttribute("disabled", true);
311-
312-
values.push(questionInput.value);
313-
});
314-
315322
// Remove next button
316323
this.ctrlNext.style.display = "none";
317324

318325
// Call the custom onSubmit function
319-
this.props.onSubmit(this.formRef, values);
326+
this.props.onSubmit(this.formRef, this.state.values);
320327
});
321328
}
322329
}
@@ -332,17 +339,34 @@ class EurekaForm extends React.Component {
332339
<form id={this.props.id} className={customClass + "simform"} ref={formRef => this.formRef = formRef}>
333340
<div className="simform-inner">
334341
<ol className="questions">
335-
{this.props.questions.map((question, i) =>
336-
<li key={`eureka-question-${i}`}>
337-
<span>
338-
<label htmlFor={`eureka-question-${i}`}>
339-
{question.title}
340-
</label>
341-
</span>
342-
343-
<input id={`eureka-question-${i}`} name={`eureka-question-${i}`} type={question.inputType || "text"} />
344-
</li>
345-
)}
342+
{this.props.questions && this.props.questions.map((question, i) => {
343+
const key = question.key || `eureka-question-${i}`
344+
return (
345+
<li key={key}>
346+
<span>
347+
<label htmlFor={key}>
348+
{question.title}
349+
</label>
350+
</span>
351+
352+
<input id={key} name={key} type={question.inputType || "text"} />
353+
</li>
354+
)
355+
})}
356+
{this.props.children && React.Children.map(this.props.children, (child, i) => {
357+
const key = child.props.type || `eureka-question-${i}`
358+
return (
359+
<li key={key}>
360+
<span>
361+
<label htmlFor={key}>
362+
{child}
363+
</label>
364+
</span>
365+
366+
<input id={key} name={key} type={child.props.type || "text"} />
367+
</li>
368+
)
369+
})}
346370
</ol>
347371

348372
<button className="submit" type="submit">Send answers</button>
@@ -369,4 +393,8 @@ class EurekaForm extends React.Component {
369393
}
370394
}
371395

396+
EurekaForm.defaultProps = {
397+
onUpdate: function () {}
398+
}
399+
372400
module.exports = { EurekaForm };

0 commit comments

Comments
 (0)