Created
June 24, 2018 13:00
-
-
Save santosh-btc/89943da49b31326e86dee904446cc713 to your computer and use it in GitHub Desktop.
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 from 'react'; | |
import { Field, reduxForm } from 'redux-form'; | |
function validate(values) { | |
const errors = {} | |
if (!values.firstName) { | |
errors.firstName = 'Required' | |
} | |
if (!values.lastName) { | |
errors.lastName = 'Required' | |
} | |
if (!values.email) { | |
errors.email = 'Required' | |
} | |
return errors | |
} | |
const renderField = ({ | |
input, | |
label, | |
type, | |
meta: { touched, error } | |
}) => ( | |
<div> | |
<label>{label}</label> | |
<div> | |
<input {...input} placeholder={label} type={type} /> | |
{touched && error && <span>{error}</span>} | |
</div> | |
</div> | |
) | |
const SimpleForm = props => { | |
const { handleSubmit, pristine, reset, submitting } = props; | |
return ( | |
<form onSubmit={handleSubmit}> | |
<div> | |
<label>First Name</label> | |
<div> | |
<Field | |
name="firstName" | |
component={renderField} | |
type="text" | |
/> | |
</div> | |
</div> | |
<div> | |
<label>Last Name</label> | |
<div> | |
<Field | |
name="lastName" | |
component={renderField} | |
type="text" | |
/> | |
</div> | |
</div> | |
<div> | |
<label>Email</label> | |
<div> | |
<Field | |
name="email" | |
component={renderField} | |
type="email" | |
/> | |
</div> | |
</div> | |
<div> | |
<label>Sex</label> | |
<div> | |
<label> | |
<Field name="sex" component="input" type="radio" value="male" /> | |
{' '} | |
Male | |
</label> | |
<label> | |
<Field name="sex" component="input" type="radio" value="female" /> | |
{' '} | |
Female | |
</label> | |
</div> | |
</div> | |
<div> | |
<label>Favorite Color</label> | |
<div> | |
<Field name="favoriteColor" component="select"> | |
<option /> | |
<option value="ff0000">Red</option> | |
<option value="00ff00">Green</option> | |
<option value="0000ff">Blue</option> | |
</Field> | |
</div> | |
</div> | |
<div> | |
<label htmlFor="employed">Employed</label> | |
<div> | |
<Field | |
name="employed" | |
id="employed" | |
component="input" | |
type="checkbox" | |
/> | |
</div> | |
</div> | |
<div> | |
<label>Notes</label> | |
<div> | |
<Field name="notes" component="textarea" /> | |
</div> | |
</div> | |
<div> | |
<button type="submit" disabled={pristine || submitting}>Submit</button> | |
<button type="button" disabled={pristine || submitting} onClick={reset}> | |
Clear Values | |
</button> | |
</div> | |
</form> | |
); | |
}; | |
export default reduxForm({ | |
form: 'simple', // a unique identifier for this form | |
validate | |
})(SimpleForm); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment