-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple form with Redux Form (including validation).
- Loading branch information
Showing
6 changed files
with
101 additions
and
7 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,57 @@ | ||
import React from "react"; | ||
import { Field, reduxForm } from "redux-form"; | ||
|
||
class StreamCreate extends React.Component { | ||
renderError(error, touched) { | ||
if (error && touched) { | ||
return <div className='ui error message'>{error}</div>; | ||
} | ||
} | ||
|
||
renderInput = (fieldProps) => { | ||
// fieldProps.input & fieldProps.meta are provided by the redux-forms framrwork. | ||
// Custom Field attributes (like label) are also made available on the fieldProps object. | ||
const error = fieldProps.meta.error; | ||
const touched = fieldProps.meta.touched; | ||
const cssClass = error && touched ? "field error" : "field"; | ||
return ( | ||
<div className={cssClass}> | ||
<label>{fieldProps.label}</label> | ||
<input {...fieldProps.input} autoComplete='off' /> | ||
{this.renderError(error, touched)} | ||
</div> | ||
); | ||
}; | ||
|
||
onSubmit(formValues) { | ||
// Form is already stored in Redux store. At this point we can submit to backend server etc. | ||
} | ||
|
||
render() { | ||
return <div>StreamCreate</div>; | ||
return ( | ||
<form onSubmit={this.props.handleSubmit(this.onSubmit)} className='ui form error'> | ||
<Field name='title' component={this.renderInput} label='Title' /> | ||
<Field name='description' component={this.renderInput} label='Description' /> | ||
<button className='ui button'>Submit</button> | ||
</form> | ||
); | ||
} | ||
} | ||
|
||
export default StreamCreate; | ||
const validate = (formValues) => { | ||
const errors = {}; | ||
if (!formValues.title) { | ||
errors.title = "Invalid title"; | ||
} | ||
if (!formValues.description) { | ||
errors.description = "Invalid description"; | ||
} | ||
return errors; | ||
}; | ||
|
||
export default reduxForm({ | ||
// Name to identify this component's form in the Redux store | ||
form: "createStream", | ||
// Reference to the form validation method | ||
validate: validate | ||
})(StreamCreate); |
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