2018-04-13 15:33:03 +01:00
|
|
|
import React from 'react';
|
2018-05-18 14:54:21 +01:00
|
|
|
import { kebabCase, startCase } from 'lodash';
|
2018-04-13 15:33:03 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import {
|
|
|
|
Alert,
|
|
|
|
Col,
|
|
|
|
ControlLabel,
|
|
|
|
FormControl,
|
2019-02-15 17:52:45 +03:00
|
|
|
FormGroup,
|
2018-05-18 14:54:21 +01:00
|
|
|
HelpBlock
|
2018-09-30 11:37:19 +01:00
|
|
|
} from '@freecodecamp/react-bootstrap';
|
2019-09-04 15:48:58 +03:00
|
|
|
import { Field } from 'react-final-form';
|
2018-04-13 15:33:03 +01:00
|
|
|
|
|
|
|
const propTypes = {
|
2019-09-04 15:48:58 +03:00
|
|
|
fields: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
|
2018-04-13 15:33:03 +01:00
|
|
|
options: PropTypes.shape({
|
|
|
|
ignored: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
placeholder: PropTypes.bool,
|
|
|
|
required: PropTypes.arrayOf(PropTypes.string),
|
|
|
|
types: PropTypes.objectOf(PropTypes.string)
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
function FormFields(props) {
|
2019-09-04 15:48:58 +03:00
|
|
|
const { fields, options = {} } = props;
|
2018-04-13 15:33:03 +01:00
|
|
|
const {
|
|
|
|
ignored = [],
|
|
|
|
placeholder = true,
|
|
|
|
required = [],
|
|
|
|
types = {}
|
|
|
|
} = options;
|
|
|
|
return (
|
|
|
|
<div>
|
2019-09-04 15:48:58 +03:00
|
|
|
{fields
|
2018-04-13 15:33:03 +01:00
|
|
|
.filter(field => !ignored.includes(field))
|
2019-09-04 15:48:58 +03:00
|
|
|
.map(name => (
|
|
|
|
<Field key={`${name}-field`} name={name}>
|
|
|
|
{({ input: { value, onChange }, meta: { pristine, error } }) => {
|
|
|
|
const key = kebabCase(name);
|
|
|
|
const type = name in types ? types[name] : 'text';
|
|
|
|
return (
|
|
|
|
<Col key={key} xs={12}>
|
|
|
|
<FormGroup>
|
|
|
|
{type === 'hidden' ? null : (
|
|
|
|
<ControlLabel htmlFor={key}>
|
|
|
|
{startCase(name)}
|
|
|
|
</ControlLabel>
|
|
|
|
)}
|
|
|
|
<FormControl
|
|
|
|
componentClass={type === 'textarea' ? type : 'input'}
|
|
|
|
id={key}
|
|
|
|
name={name}
|
|
|
|
onChange={onChange}
|
|
|
|
placeholder={placeholder ? name : ''}
|
|
|
|
required={required.includes(name)}
|
|
|
|
rows={4}
|
|
|
|
type={type}
|
|
|
|
value={value}
|
|
|
|
/>
|
|
|
|
{error && !pristine ? (
|
|
|
|
<HelpBlock>
|
|
|
|
<Alert bsStyle='danger'>{error}</Alert>
|
|
|
|
</HelpBlock>
|
|
|
|
) : null}
|
|
|
|
</FormGroup>
|
|
|
|
</Col>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</Field>
|
|
|
|
))}
|
2018-04-13 15:33:03 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
FormFields.displayName = 'FormFields';
|
|
|
|
FormFields.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default FormFields;
|