2021-04-12 13:32:25 +00:00
|
|
|
import React, { Component } from "react";
|
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
|
import classnames from "classnames";
|
2016-12-16 15:54:49 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
const baseClass = "form-field";
|
2016-12-16 15:54:49 +00:00
|
|
|
|
|
|
|
|
class FormField extends Component {
|
|
|
|
|
static propTypes = {
|
2016-12-23 18:40:16 +00:00
|
|
|
children: PropTypes.node,
|
2016-12-16 15:54:49 +00:00
|
|
|
className: PropTypes.string,
|
|
|
|
|
error: PropTypes.string,
|
2021-04-12 13:32:25 +00:00
|
|
|
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;
|
2021-04-12 13:32:25 +00:00
|
|
|
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>
|
|
|
|
|
);
|
2021-04-12 13:32:25 +00:00
|
|
|
};
|
2016-12-16 15:54:49 +00:00
|
|
|
|
|
|
|
|
renderHint = () => {
|
|
|
|
|
const { hint } = this.props;
|
|
|
|
|
|
|
|
|
|
if (hint) {
|
|
|
|
|
return <span className={`${baseClass}__hint`}>{hint}</span>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2021-04-12 13:32:25 +00:00
|
|
|
};
|
2016-12-16 15:54:49 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
render() {
|
2016-12-16 15:54:49 +00:00
|
|
|
const { renderLabel, renderHint } = this;
|
|
|
|
|
const { children, className, type } = this.props;
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
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;
|