2021-06-18 20:33:45 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import classnames from "classnames";
|
|
|
|
|
import { isEmpty } from "lodash";
|
|
|
|
|
|
2022-02-28 21:25:06 +00:00
|
|
|
import TooltipWrapper from "components/TooltipWrapper";
|
2024-03-20 20:53:34 +00:00
|
|
|
import { PlacesType } from "react-tooltip-5";
|
2022-02-28 21:25:06 +00:00
|
|
|
|
2024-01-18 15:48:44 +00:00
|
|
|
// all form-field styles are defined in _global.scss, which apply here and elsewhere
|
2021-06-18 20:33:45 +00:00
|
|
|
const baseClass = "form-field";
|
|
|
|
|
|
|
|
|
|
export interface IFormFieldProps {
|
|
|
|
|
children: JSX.Element;
|
2026-04-09 13:41:48 +00:00
|
|
|
label: React.ReactNode;
|
2021-06-18 20:33:45 +00:00
|
|
|
name: string;
|
2026-04-09 13:41:48 +00:00
|
|
|
helpText?: React.ReactNode;
|
2024-04-16 16:22:08 +00:00
|
|
|
type?: string;
|
2026-04-09 13:41:48 +00:00
|
|
|
error?: string | null;
|
2024-04-16 16:22:08 +00:00
|
|
|
className?: string;
|
2023-11-07 21:15:49 +00:00
|
|
|
tooltip?: React.ReactNode;
|
2024-03-20 20:53:34 +00:00
|
|
|
labelTooltipPosition?: PlacesType;
|
2024-08-20 13:41:49 +00:00
|
|
|
disabled?: boolean;
|
2021-06-18 20:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const FormField = ({
|
|
|
|
|
children,
|
|
|
|
|
className,
|
|
|
|
|
error,
|
2024-01-18 15:48:44 +00:00
|
|
|
helpText,
|
2021-06-18 20:33:45 +00:00
|
|
|
label,
|
|
|
|
|
name,
|
|
|
|
|
type,
|
2022-02-28 21:25:06 +00:00
|
|
|
tooltip,
|
2024-03-20 20:53:34 +00:00
|
|
|
labelTooltipPosition,
|
2024-08-20 13:41:49 +00:00
|
|
|
disabled,
|
2021-12-17 21:40:57 +00:00
|
|
|
}: IFormFieldProps): JSX.Element => {
|
2021-06-18 20:33:45 +00:00
|
|
|
const renderLabel = () => {
|
|
|
|
|
const labelWrapperClasses = classnames(`${baseClass}__label`, {
|
|
|
|
|
[`${baseClass}__label--error`]: !isEmpty(error),
|
2024-08-20 13:41:49 +00:00
|
|
|
[`${baseClass}__label--disabled`]: disabled,
|
2021-06-18 20:33:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!label) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2022-02-28 21:25:06 +00:00
|
|
|
<label
|
|
|
|
|
className={labelWrapperClasses}
|
|
|
|
|
htmlFor={name}
|
|
|
|
|
data-has-tooltip={!!tooltip}
|
|
|
|
|
>
|
|
|
|
|
{error ||
|
|
|
|
|
(tooltip ? (
|
2024-03-20 20:53:34 +00:00
|
|
|
<TooltipWrapper
|
|
|
|
|
tipContent={tooltip}
|
2024-05-24 21:30:54 +00:00
|
|
|
position={labelTooltipPosition}
|
2024-05-31 13:22:28 +00:00
|
|
|
clickable={false} // Not block form behind tooltip
|
2024-03-20 20:53:34 +00:00
|
|
|
>
|
2022-02-28 21:25:06 +00:00
|
|
|
{label as string}
|
|
|
|
|
</TooltipWrapper>
|
|
|
|
|
) : (
|
|
|
|
|
<>{label}</>
|
|
|
|
|
))}
|
2021-06-18 20:33:45 +00:00
|
|
|
</label>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-18 15:48:44 +00:00
|
|
|
const renderHelpText = () => {
|
|
|
|
|
if (helpText) {
|
|
|
|
|
return <span className={`${baseClass}__help-text`}>{helpText}</span>;
|
2021-06-18 20:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const formFieldClass = classnames(
|
|
|
|
|
baseClass,
|
|
|
|
|
{
|
|
|
|
|
[`${baseClass}--${type}`]: !isEmpty(type),
|
|
|
|
|
},
|
|
|
|
|
className
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={formFieldClass}>
|
|
|
|
|
{renderLabel()}
|
|
|
|
|
{children}
|
2024-01-18 15:48:44 +00:00
|
|
|
{renderHelpText()}
|
2021-06-18 20:33:45 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default FormField;
|