mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 21:47:20 +00:00
relates to #17031 Adds functionality to create manual labels in fleet. - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [x] Added/updated tests - [x] M0anual QA for all new/changed functionality --------- Co-authored-by: Martin Angers <martin.n.angers@gmail.com>
89 lines
1.9 KiB
TypeScript
89 lines
1.9 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: Array<any> | JSX.Element | string;
|
|
name: string;
|
|
helpText?: Array<any> | JSX.Element | string;
|
|
type?: string;
|
|
error?: string;
|
|
className?: string;
|
|
tooltip?: React.ReactNode;
|
|
labelTooltipPosition?: PlacesType;
|
|
}
|
|
|
|
const FormField = ({
|
|
children,
|
|
className,
|
|
error,
|
|
helpText,
|
|
label,
|
|
name,
|
|
type,
|
|
tooltip,
|
|
labelTooltipPosition,
|
|
}: IFormFieldProps): JSX.Element => {
|
|
const renderLabel = () => {
|
|
const labelWrapperClasses = classnames(`${baseClass}__label`, {
|
|
[`${baseClass}__label--error`]: !isEmpty(error),
|
|
});
|
|
|
|
if (!label) {
|
|
return false;
|
|
}
|
|
|
|
return (
|
|
<label
|
|
className={labelWrapperClasses}
|
|
htmlFor={name}
|
|
data-has-tooltip={!!tooltip}
|
|
>
|
|
{error ||
|
|
(tooltip ? (
|
|
<TooltipWrapper
|
|
tipContent={tooltip}
|
|
position={labelTooltipPosition || "top-start"}
|
|
>
|
|
{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;
|