fleet/frontend/components/forms/FormField/FormField.tsx
Ian Littman 2891904f31
🤖 Switch InputField + InputFieldWithIcon JSX components to TS, add more test coverage, fix Storybook build (#43307)
Zed + Opus 4.6; prompt: Convert the InputField JSX component to
TypeScript and remove the ts-ignore directives that we no longer need
after doing so.

- [x] Changes file added
- [x] Automated tests updated
2026-04-09 08:41:48 -05:00

93 lines
2 KiB
TypeScript

import React from "react";
import classnames from "classnames";
import { isEmpty } from "lodash";
import TooltipWrapper from "components/TooltipWrapper";
import { PlacesType } from "react-tooltip-5";
// all form-field styles are defined in _global.scss, which apply here and elsewhere
const baseClass = "form-field";
export interface IFormFieldProps {
children: JSX.Element;
label: React.ReactNode;
name: string;
helpText?: React.ReactNode;
type?: string;
error?: string | null;
className?: string;
tooltip?: React.ReactNode;
labelTooltipPosition?: PlacesType;
disabled?: boolean;
}
const FormField = ({
children,
className,
error,
helpText,
label,
name,
type,
tooltip,
labelTooltipPosition,
disabled,
}: IFormFieldProps): JSX.Element => {
const renderLabel = () => {
const labelWrapperClasses = classnames(`${baseClass}__label`, {
[`${baseClass}__label--error`]: !isEmpty(error),
[`${baseClass}__label--disabled`]: disabled,
});
if (!label) {
return false;
}
return (
<label
className={labelWrapperClasses}
htmlFor={name}
data-has-tooltip={!!tooltip}
>
{error ||
(tooltip ? (
<TooltipWrapper
tipContent={tooltip}
position={labelTooltipPosition}
clickable={false} // Not block form behind tooltip
>
{label as string}
</TooltipWrapper>
) : (
<>{label}</>
))}
</label>
);
};
const renderHelpText = () => {
if (helpText) {
return <span className={`${baseClass}__help-text`}>{helpText}</span>;
}
return false;
};
const formFieldClass = classnames(
baseClass,
{
[`${baseClass}--${type}`]: !isEmpty(type),
},
className
);
return (
<div className={formFieldClass}>
{renderLabel()}
{children}
{renderHelpText()}
</div>
);
};
export default FormField;