Last active
May 31, 2018 16:52
-
-
Save codeAdrian/e84e4ab7412d7c1ef48ebbd6090d8656 to your computer and use it in GitHub Desktop.
React HOC for forms that keeps track of input values and stores it in state dynamically, handles submit events
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"; | |
const WithForm = (FormFields, action) => { | |
return class Form extends Component { | |
handleOnChange = event => { | |
const inputTarget = event.currentTarget || event.srcElement; | |
const { title, value } = inputTarget; | |
this.setState( | |
{ | |
[title]: value | |
} | |
); | |
}; | |
handleOnSubmit = event => { | |
event.preventDefault(); | |
action({ ...this.state }); | |
}; | |
render() { | |
return ( | |
<form className="formMain" onSubmit={this.props.submitHandler}> | |
<FormFields | |
handleOnChange={this.handleOnChange} | |
handleOnSubmit={this.handleOnSubmit} | |
/> | |
</form> | |
); | |
} | |
}; | |
}; | |
export default WithForm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment