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

76 lines
1.5 KiB
React
Raw Normal View History

import React, { Component } from "react";
import PropTypes from "prop-types";
import classnames from "classnames";
2016-12-16 15:54:49 +00:00
const baseClass = "form-field";
2016-12-16 15:54:49 +00:00
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>
);
};
2016-12-16 15:54:49 +00:00
renderHint = () => {
const { hint } = this.props;
if (hint) {
return <span className={`${baseClass}__hint`}>{hint}</span>;
}
return false;
};
2016-12-16 15:54:49 +00:00
render() {
2016-12-16 15:54:49 +00:00
const { renderLabel, renderHint } = this;
const { children, className, type } = this.props;
const formFieldClass = classnames(
baseClass,
{
[`${baseClass}--${type}`]: type,
},
className
);
2016-12-16 15:54:49 +00:00
return (
<div className={formFieldClass}>
{renderLabel()}
{children}
{renderHint()}
</div>
);
}
}
export default FormField;