mirror of
https://github.com/empayre/fleet.git
synced 2024-11-07 09:18:59 +00:00
3af64748ab
* #917 fixed enter key for last page; TS overhaul * #917 clean up * Update frontend/components/forms/FormField/FormField.tsx Co-authored-by: Zach Wasserman <zach@fleetdm.com> * #917 fixed tests and linted Co-authored-by: Zach Wasserman <zach@fleetdm.com>
68 lines
1.2 KiB
TypeScript
68 lines
1.2 KiB
TypeScript
import React from "react";
|
|
import classnames from "classnames";
|
|
import { isEmpty } from "lodash";
|
|
|
|
const baseClass = "form-field";
|
|
|
|
export interface IFormFieldProps {
|
|
children: JSX.Element;
|
|
className: string;
|
|
error: string;
|
|
hint: Array<any> | JSX.Element | string;
|
|
label: Array<any> | JSX.Element | string;
|
|
name: string;
|
|
type: string;
|
|
}
|
|
|
|
const FormField = ({
|
|
children,
|
|
className,
|
|
error,
|
|
hint,
|
|
label,
|
|
name,
|
|
type,
|
|
}: IFormFieldProps) => {
|
|
const renderLabel = () => {
|
|
const labelWrapperClasses = classnames(`${baseClass}__label`, {
|
|
[`${baseClass}__label--error`]: !isEmpty(error),
|
|
});
|
|
|
|
if (!label) {
|
|
return false;
|
|
}
|
|
|
|
return (
|
|
<label className={labelWrapperClasses} htmlFor={name}>
|
|
{error || label}
|
|
</label>
|
|
);
|
|
};
|
|
|
|
const renderHint = () => {
|
|
if (hint) {
|
|
return <span className={`${baseClass}__hint`}>{hint}</span>;
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
const formFieldClass = classnames(
|
|
baseClass,
|
|
{
|
|
[`${baseClass}--${type}`]: !isEmpty(type),
|
|
},
|
|
className
|
|
);
|
|
|
|
return (
|
|
<div className={formFieldClass}>
|
|
{renderLabel()}
|
|
{children}
|
|
{renderHint()}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default FormField;
|