Created
March 11, 2019 10:21
-
-
Save jwjames/3a6d1a9b12380697544e728e837f0ed7 to your computer and use it in GitHub Desktop.
StreamCreate component. Renders twice.
This file contains hidden or 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 from 'react'; | |
import { Field, reduxForm } from 'redux-form'; | |
class StreamCreate extends React.Component { | |
renderError({ error, touched }) { | |
if (touched && error) { | |
return ( | |
<div className="ui error message"> | |
<div className="header">{error}</div> | |
</div> | |
); | |
} | |
} | |
renderInput = ({ input, label, meta }) => { | |
const className = `field ${meta.error && meta.touched ? 'error' : ''}`; | |
return ( | |
<div className={className}> | |
<label>{label}</label> | |
<input {...input} autoComplete="off" /> | |
<div>{this.renderError(meta)}</div> | |
</div> | |
); | |
}; | |
onSubmit(formValues) { | |
console.log(formValues); | |
} | |
render() { | |
console.log('StreamCreate rendered'); | |
return ( | |
<form | |
onSubmit={this.props.handleSubmit(this.onSubmit)} | |
className="ui form error" | |
> | |
<Field name="title" component={this.renderInput} label="Enter Title" /> | |
<Field | |
name="description" | |
component={this.renderInput} | |
label="Enter Description" | |
/> | |
<button className="ui button primary">Submit</button> | |
</form> | |
); | |
} | |
} | |
const validate = formValues => { | |
const errors = {}; | |
if (!formValues.title) { | |
errors.title = 'You must enter a title'; | |
} | |
if (!formValues.description) { | |
errors.description = 'You must enter a description'; | |
} | |
return errors; | |
}; | |
export default reduxForm({ | |
form: 'streamCreate', | |
validate, | |
})(StreamCreate); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment