2021-04-12 13:32:25 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
|
import classnames from "classnames";
|
2016-10-19 20:22:18 +00:00
|
|
|
|
2023-07-19 14:40:59 +00:00
|
|
|
import Icon from "components/Icon/Icon";
|
2021-06-07 01:56:30 +00:00
|
|
|
import FleetIcon from "components/icons/FleetIcon";
|
2022-02-28 21:25:06 +00:00
|
|
|
import TooltipWrapper from "components/TooltipWrapper";
|
2021-04-12 13:32:25 +00:00
|
|
|
import InputField from "../InputField";
|
2016-09-12 15:14:07 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
const baseClass = "input-icon-field";
|
2016-11-03 19:40:54 +00:00
|
|
|
|
2016-09-30 18:55:15 +00:00
|
|
|
class InputFieldWithIcon extends InputField {
|
2016-09-12 15:14:07 +00:00
|
|
|
static propTypes = {
|
2016-09-19 18:33:43 +00:00
|
|
|
autofocus: PropTypes.bool,
|
2016-09-14 20:31:54 +00:00
|
|
|
error: PropTypes.string,
|
2024-01-18 15:48:44 +00:00
|
|
|
helpText: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
|
2016-09-12 15:14:07 +00:00
|
|
|
iconName: PropTypes.string,
|
2023-07-19 14:40:59 +00:00
|
|
|
iconSvg: PropTypes.string,
|
2021-09-10 19:06:37 +00:00
|
|
|
label: PropTypes.string,
|
2016-09-12 15:14:07 +00:00
|
|
|
name: PropTypes.string,
|
|
|
|
|
onChange: PropTypes.func,
|
2023-11-21 21:49:41 +00:00
|
|
|
onClick: PropTypes.func,
|
2016-09-12 15:14:07 +00:00
|
|
|
placeholder: PropTypes.string,
|
2016-12-01 18:57:19 +00:00
|
|
|
tabIndex: PropTypes.number,
|
2016-09-12 15:14:07 +00:00
|
|
|
type: PropTypes.string,
|
2016-11-29 22:29:14 +00:00
|
|
|
className: PropTypes.string,
|
2021-07-06 16:22:51 +00:00
|
|
|
disabled: PropTypes.bool,
|
2021-09-10 19:06:37 +00:00
|
|
|
iconPosition: PropTypes.oneOf(["start", "end"]),
|
2022-02-09 01:40:38 +00:00
|
|
|
inputOptions: PropTypes.object, // eslint-disable-line react/forbid-prop-types
|
2022-02-28 21:25:06 +00:00
|
|
|
tooltip: PropTypes.string,
|
2023-09-11 14:01:31 +00:00
|
|
|
ignore1Password: PropTypes.bool,
|
2016-09-12 15:14:07 +00:00
|
|
|
};
|
|
|
|
|
|
2016-09-14 20:31:54 +00:00
|
|
|
renderHeading = () => {
|
2023-05-09 17:12:29 +00:00
|
|
|
const { error, placeholder, name, tooltip } = this.props;
|
|
|
|
|
const label = this.props.label ?? placeholder;
|
2016-09-14 20:31:54 +00:00
|
|
|
|
2023-05-09 17:12:29 +00:00
|
|
|
const labelClasses = classnames(`${baseClass}__label`, {
|
|
|
|
|
[`${baseClass}__errors`]: !!error,
|
|
|
|
|
});
|
2016-09-14 20:31:54 +00:00
|
|
|
|
2021-04-28 22:14:26 +00:00
|
|
|
return (
|
2022-02-28 21:25:06 +00:00
|
|
|
<label
|
|
|
|
|
htmlFor={name}
|
|
|
|
|
className={labelClasses}
|
|
|
|
|
data-has-tooltip={!!tooltip}
|
|
|
|
|
>
|
2023-05-09 17:12:29 +00:00
|
|
|
{tooltip && !error ? (
|
2024-05-24 21:30:54 +00:00
|
|
|
<TooltipWrapper position="bottom-start" tipContent={tooltip}>
|
2023-05-09 17:12:29 +00:00
|
|
|
{label}
|
|
|
|
|
</TooltipWrapper>
|
2022-02-28 21:25:06 +00:00
|
|
|
) : (
|
2023-05-09 17:12:29 +00:00
|
|
|
<>{error || label}</>
|
2022-02-28 21:25:06 +00:00
|
|
|
)}
|
2021-04-28 22:14:26 +00:00
|
|
|
</label>
|
|
|
|
|
);
|
2021-04-12 13:32:25 +00:00
|
|
|
};
|
2016-09-12 15:14:07 +00:00
|
|
|
|
2024-01-18 15:48:44 +00:00
|
|
|
renderHelpText = () => {
|
|
|
|
|
const { helpText } = this.props;
|
2016-12-01 18:57:19 +00:00
|
|
|
|
2024-01-18 15:48:44 +00:00
|
|
|
if (helpText) {
|
|
|
|
|
return (
|
|
|
|
|
<span className={`${baseClass}__help-text form-field__help-text`}>
|
|
|
|
|
{helpText}
|
|
|
|
|
</span>
|
|
|
|
|
);
|
2016-12-01 18:57:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2021-04-12 13:32:25 +00:00
|
|
|
};
|
2016-12-01 18:57:19 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
render() {
|
|
|
|
|
const {
|
|
|
|
|
className,
|
|
|
|
|
error,
|
|
|
|
|
iconName,
|
2023-07-19 14:40:59 +00:00
|
|
|
iconSvg,
|
2021-04-12 13:32:25 +00:00
|
|
|
name,
|
|
|
|
|
placeholder,
|
|
|
|
|
tabIndex,
|
|
|
|
|
type,
|
|
|
|
|
value,
|
2021-07-06 16:22:51 +00:00
|
|
|
disabled,
|
2021-09-10 19:06:37 +00:00
|
|
|
iconPosition,
|
2022-02-09 01:40:38 +00:00
|
|
|
inputOptions,
|
2023-09-11 14:01:31 +00:00
|
|
|
ignore1Password,
|
2023-11-21 21:49:41 +00:00
|
|
|
onClick,
|
2021-04-12 13:32:25 +00:00
|
|
|
} = this.props;
|
2024-01-18 15:48:44 +00:00
|
|
|
const { onInputChange, renderHelpText } = this;
|
2016-09-12 15:14:07 +00:00
|
|
|
|
2024-01-18 15:48:44 +00:00
|
|
|
const wrapperClasses = classnames(baseClass, "form-field", {
|
2021-09-10 19:06:37 +00:00
|
|
|
[`${baseClass}--icon-start`]: iconPosition && iconPosition === "start",
|
|
|
|
|
});
|
|
|
|
|
|
2016-11-03 19:40:54 +00:00
|
|
|
const inputClasses = classnames(
|
|
|
|
|
`${baseClass}__input`,
|
2021-04-12 13:32:25 +00:00
|
|
|
"input-with-icon",
|
2016-11-29 22:29:14 +00:00
|
|
|
className,
|
2016-11-03 19:40:54 +00:00
|
|
|
{ [`${baseClass}__input--error`]: error },
|
2021-09-10 19:06:37 +00:00
|
|
|
{ [`${baseClass}__input--password`]: type === "password" && value },
|
|
|
|
|
{
|
|
|
|
|
[`${baseClass}__input--icon-start`]:
|
|
|
|
|
iconPosition && iconPosition === "start",
|
|
|
|
|
}
|
2016-11-03 19:40:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const iconClasses = classnames(
|
|
|
|
|
`${baseClass}__icon`,
|
|
|
|
|
{ [`${baseClass}__icon--error`]: error },
|
2021-04-12 13:32:25 +00:00
|
|
|
{ [`${baseClass}__icon--active`]: value }
|
2016-11-03 19:40:54 +00:00
|
|
|
);
|
|
|
|
|
|
2016-09-12 15:14:07 +00:00
|
|
|
return (
|
2021-09-10 19:06:37 +00:00
|
|
|
<div className={wrapperClasses}>
|
2023-07-19 14:40:59 +00:00
|
|
|
{this.props.label && this.renderHeading()}
|
2024-02-29 13:49:36 +00:00
|
|
|
<div className={`${baseClass}__input-wrapper`}>
|
|
|
|
|
<input
|
|
|
|
|
id={name}
|
|
|
|
|
name={name}
|
|
|
|
|
onChange={onInputChange}
|
|
|
|
|
onClick={onClick}
|
|
|
|
|
className={inputClasses}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
ref={(r) => {
|
|
|
|
|
this.input = r;
|
|
|
|
|
}}
|
|
|
|
|
tabIndex={tabIndex}
|
|
|
|
|
type={type}
|
|
|
|
|
value={value}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
{...inputOptions}
|
|
|
|
|
data-1p-ignore={ignore1Password}
|
|
|
|
|
/>
|
|
|
|
|
{iconSvg && <Icon name={iconSvg} className={iconClasses} />}
|
|
|
|
|
{iconName && <FleetIcon name={iconName} className={iconClasses} />}
|
|
|
|
|
</div>
|
2024-01-18 15:48:44 +00:00
|
|
|
{renderHelpText()}
|
2016-09-12 15:14:07 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 19:40:54 +00:00
|
|
|
export default InputFieldWithIcon;
|