Last active
March 19, 2017 21:31
-
-
Save helloncanella/857a6dd375be777b18c96fd1f5b05bea to your computer and use it in GitHub Desktop.
New form version
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
import React, { Component } from 'react' | |
class Form extends Component { | |
constructor() { | |
super() | |
this.onSubmit = this.onSubmit.bind(this) | |
} | |
//public method | |
getData() { | |
return this.data | |
} | |
fetchData() { | |
let data = [] | |
const fieldsets = this.form.querySelectorAll("fieldset") | |
fieldsets.forEach(fieldset => { | |
const { name } = fieldset | |
data.push({ | |
name, | |
inputs: this.fetchInputsData(fieldset) | |
}) | |
}) | |
return data | |
} | |
fetchInputsData(fieldset) { | |
var data = {} | |
var inputs = fieldset.querySelectorAll('input:not(*[type="submit"])') | |
inputs.forEach(({ name, value }) => data[name] = value) | |
return data | |
} | |
renderFieldsets() { | |
const { fieldsets } = this.props | |
return fieldsets.map(({ name, inputs }, index) => { | |
return <fieldset name={name} key={index}>{this.renderInputs(inputs)}</fieldset> | |
}) | |
} | |
renderInputs(inputs) { | |
return inputs.map((inputProps, index) => { | |
return <input {...inputProps} key={index} /> | |
}) | |
} | |
onSubmit(e) { | |
e.preventDefault() | |
this.data = this.fetchData() | |
this.props.onSubmit() | |
} | |
render() { | |
return ( | |
<form onSubmit={this.onSubmit} ref={e => this.form = e}> | |
<h1>A beautiful form</h1> | |
<div className="content"> | |
<h4 className="error">{this.props.validationError}</h4> | |
{this.renderFieldsets()} | |
<input type="submit" /> | |
</div> | |
</form> | |
) | |
} | |
} | |
export default Form |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment