Last active
August 30, 2018 15:56
-
-
Save mishelen/bdae351515c238d7fe67607d752dc11b 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, { Fragment } from 'react'; | |
import PropTypes from 'prop-types'; | |
import { Input, FormFeedback, FormText } from 'reactstrap'; | |
export const renderTextField = ({ input, meta: { touched, error, warning }, ...custom }) => ( | |
<Fragment> | |
<Input {...(touched ? { valid: !error } : {})} {...input} {...custom} /> | |
{error && <FormFeedback>{error}</FormFeedback>} | |
{!error && warning && <FormText>{warning}</FormText>} | |
</Fragment> | |
); | |
renderTextField.propTypes = { | |
input: PropTypes.shape().isRequired, | |
meta: PropTypes.shape().isRequired, | |
type: PropTypes.string.isRequired | |
}; | |
export const renderCheckbox = ({ input: { value, onChange } }) => ( | |
<Input type="checkbox" checked={!!value} onChange={onChange} /> | |
); | |
renderCheckbox.propTypes = { | |
input: PropTypes.shape().isRequired | |
}; | |
export const renderSelectField = ({ input, meta: { touched, error }, children, ...custom }) => ( | |
<Input type="select" {...(touched ? { valid: !error } : {})} {...input} {...custom}> | |
{children} | |
</Input> | |
); | |
renderSelectField.propTypes = { | |
input: PropTypes.shape().isRequired, | |
meta: PropTypes.shape().isRequired, | |
children: PropTypes.node.isRequired | |
}; | |
export const renderRadioField = ({ value, input, ...custom }) => ( | |
<Input type="radio" checked={value === input.value} {...input} {...custom} /> | |
); | |
renderRadioField.propTypes = { | |
input: PropTypes.shape().isRequired, | |
meta: PropTypes.shape().isRequired, | |
value: PropTypes.string.isRequired | |
}; | |
export const renderField = props => { | |
switch (props.type) { | |
case 'text': | |
case 'textarea': | |
return renderTextField(props); | |
case 'select': | |
return renderSelectField(props); | |
case 'radio': | |
return renderRadioField(props); | |
case 'checkbox': | |
return renderCheckbox(props); | |
default: | |
throw new TypeError('You need to provide simple type for Field component/'); | |
} | |
}; |
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
// .... | |
<FormGroup> | |
<Label for="author_id">Select Author</Label> | |
<Field id="author_id" name="authorId" type="select" component={renderField}> | |
<option value="">Select Author</option> | |
{authors.map(author => ( | |
<option key={author.id} value={author.id}> | |
{`${author.firstName} ${author.lastName}`} | |
</option> | |
))} | |
</Field> | |
</FormGroup> | |
// .... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment