fleet/frontend/components/forms/fields/InputFieldWithIcon/InputFieldWithIcon.jsx
Martavis Parker 33c5f0651c
New tooltips! (#4326)
* Allow sort by more than one key

* created custom tooltip component

* remove unused code

* fixed style for more layouts

* added tooltip to query side panel

* tooltips added to setting form

* finished settings form

* added tooltip for perf impact table headers

* tooltip for pack table and user form

* tooltip on manage policies page

* tooltip for manage schedules

* tooltip for automations; spacing for form input

* tooltip for automations modal

* user form; fixed input with icon component

* more user form tooltips

* tooltip for homepage; style fixes

* replaced many more tooltips with new version

* added story for tooltip

* added position prop

* fixed tests

* re-work how we click react-select dropdowns

* forcing the update button click

* trying a blur

* fixed typo

* trying blur on another element

* temp check-in

* replaced tooltip from host details software

* more consolidation of tooltip use for software

* fixed settings flow test

Co-authored-by: Tomas Touceda <chiiph@gmail.com>
2022-02-28 13:25:06 -08:00

126 lines
3.1 KiB
JavaScript

import React from "react";
import PropTypes from "prop-types";
import classnames from "classnames";
import FleetIcon from "components/icons/FleetIcon";
import TooltipWrapper from "components/TooltipWrapper";
import InputField from "../InputField";
const baseClass = "input-icon-field";
class InputFieldWithIcon extends InputField {
static propTypes = {
autofocus: PropTypes.bool,
error: PropTypes.string,
hint: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
iconName: PropTypes.string,
label: PropTypes.string,
name: PropTypes.string,
onChange: PropTypes.func,
placeholder: PropTypes.string,
tabIndex: PropTypes.number,
type: PropTypes.string,
className: PropTypes.string,
disabled: PropTypes.bool,
iconPosition: PropTypes.oneOf(["start", "end"]),
inputOptions: PropTypes.object, // eslint-disable-line react/forbid-prop-types
tooltip: PropTypes.string,
};
renderHeading = () => {
const { error, placeholder, name, label, tooltip } = this.props;
const labelClasses = classnames(`${baseClass}__label`);
if (error) {
return <div className={`${baseClass}__errors`}>{error}</div>;
}
return (
<label
htmlFor={name}
className={labelClasses}
data-has-tooltip={!!tooltip}
>
{tooltip ? (
<TooltipWrapper tipContent={tooltip}>{label}</TooltipWrapper>
) : (
<>{label || placeholder}</>
)}
</label>
);
};
renderHint = () => {
const { hint } = this.props;
if (hint) {
return <span className={`${baseClass}__hint`}>{hint}</span>;
}
return false;
};
render() {
const {
className,
error,
iconName,
name,
placeholder,
tabIndex,
type,
value,
disabled,
iconPosition,
inputOptions,
} = this.props;
const { onInputChange, renderHint } = this;
const wrapperClasses = classnames(baseClass, {
[`${baseClass}--icon-start`]: iconPosition && iconPosition === "start",
});
const inputClasses = classnames(
`${baseClass}__input`,
"input-with-icon",
className,
{ [`${baseClass}__input--error`]: error },
{ [`${baseClass}__input--password`]: type === "password" && value },
{
[`${baseClass}__input--icon-start`]:
iconPosition && iconPosition === "start",
}
);
const iconClasses = classnames(
`${baseClass}__icon`,
{ [`${baseClass}__icon--error`]: error },
{ [`${baseClass}__icon--active`]: value }
);
return (
<div className={wrapperClasses}>
{this.renderHeading()}
<input
id={name}
name={name}
onChange={onInputChange}
className={inputClasses}
placeholder={placeholder}
ref={(r) => {
this.input = r;
}}
tabIndex={tabIndex}
type={type}
value={value}
disabled={disabled}
{...inputOptions}
/>
{iconName && <FleetIcon name={iconName} className={iconClasses} />}
{renderHint()}
</div>
);
}
}
export default InputFieldWithIcon;