import React from 'react'; import _ from 'lodash'; import PropTypes from 'prop-types'; import { Alert, Col, ControlLabel, FormControl, HelpBlock, Row } from 'react-bootstrap'; const propTypes = { errors: PropTypes.objectOf(PropTypes.string), fields: PropTypes.objectOf( PropTypes.shape({ name: PropTypes.string.isRequired, onChange: PropTypes.func.isRequired, value: PropTypes.string.isRequired }) ).isRequired, options: PropTypes.shape({ errors: PropTypes.objectOf( PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(null)]) ), ignored: PropTypes.arrayOf(PropTypes.string), placeholder: PropTypes.bool, required: PropTypes.arrayOf(PropTypes.string), types: PropTypes.objectOf(PropTypes.string) }) }; function FormFields(props) { const { errors = {}, fields, options = {} } = props; const { ignored = [], placeholder = true, required = [], types = {} } = options; return (
{Object.keys(fields) .filter(field => !ignored.includes(field)) .map(key => fields[key]) .map(({ name, onChange, value, pristine }) => { const key = _.kebabCase(name); const type = name in types ? types[name] : 'text'; return ( {type === 'hidden' ? null : ( {_.startCase(name)} )} {name in errors && !pristine ? ( {errors[name]} ) : null} ); })}
); } FormFields.displayName = 'FormFields'; FormFields.propTypes = propTypes; export default FormFields;