fleet/frontend/components/forms/FormField/FormField.jsx

65 lines
1.4 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
2016-12-16 15:54:49 +00:00
import classnames from 'classnames';
const baseClass = 'form-field';
class FormField extends Component {
static propTypes = {
children: PropTypes.node,
2016-12-16 15:54:49 +00:00
className: PropTypes.string,
error: PropTypes.string,
hint: PropTypes.oneOfType([PropTypes.array, PropTypes.node, PropTypes.string]),
label: PropTypes.oneOfType([PropTypes.array, PropTypes.string, PropTypes.node]),
2016-12-16 15:54:49 +00:00
name: PropTypes.string,
type: PropTypes.string,
};
renderLabel = () => {
const { error, label, name } = this.props;
const labelWrapperClasses = classnames(
`${baseClass}__label`,
{ [`${baseClass}__label--error`]: error },
2016-12-16 15:54:49 +00:00
);
if (!label) {
return false;
}
return (
<label className={labelWrapperClasses} htmlFor={name}>
{error || label}
</label>
);
}
renderHint = () => {
const { hint } = this.props;
if (hint) {
return <span className={`${baseClass}__hint`}>{hint}</span>;
}
return false;
}
render () {
const { renderLabel, renderHint } = this;
const { children, className, type } = this.props;
const formFieldClass = classnames(baseClass, {
2016-12-16 15:54:49 +00:00
[`${baseClass}--${type}`]: type,
}, className);
2016-12-16 15:54:49 +00:00
return (
<div className={formFieldClass}>
{renderLabel()}
{children}
{renderHint()}
</div>
);
}
}
export default FormField;