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
|
|
|
|
2021-06-07 01:56:30 +00:00
|
|
|
import FleetIcon from "components/icons/FleetIcon";
|
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,
|
2016-12-01 18:57:19 +00:00
|
|
|
hint: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
|
2016-09-12 15:14:07 +00:00
|
|
|
iconName: 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,
|
|
|
|
|
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"]),
|
2016-09-12 15:14:07 +00:00
|
|
|
};
|
|
|
|
|
|
2016-09-14 20:31:54 +00:00
|
|
|
renderHeading = () => {
|
2021-09-10 19:06:37 +00:00
|
|
|
const { error, placeholder, name, label } = this.props;
|
2020-12-02 14:59:44 +00:00
|
|
|
const labelClasses = classnames(`${baseClass}__label`);
|
2016-09-14 20:31:54 +00:00
|
|
|
|
|
|
|
|
if (error) {
|
2016-11-03 19:40:54 +00:00
|
|
|
return <div className={`${baseClass}__errors`}>{error}</div>;
|
2016-09-14 20:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-28 22:14:26 +00:00
|
|
|
return (
|
|
|
|
|
<label htmlFor={name} className={labelClasses}>
|
2021-09-10 19:06:37 +00:00
|
|
|
{label || placeholder}
|
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
|
|
|
|
2016-12-01 18:57:19 +00:00
|
|
|
renderHint = () => {
|
|
|
|
|
const { hint } = this.props;
|
|
|
|
|
|
|
|
|
|
if (hint) {
|
|
|
|
|
return <span className={`${baseClass}__hint`}>{hint}</span>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
name,
|
|
|
|
|
placeholder,
|
|
|
|
|
tabIndex,
|
|
|
|
|
type,
|
|
|
|
|
value,
|
2021-07-06 16:22:51 +00:00
|
|
|
disabled,
|
2021-09-10 19:06:37 +00:00
|
|
|
iconPosition,
|
2021-04-12 13:32:25 +00:00
|
|
|
} = this.props;
|
2016-12-01 18:57:19 +00:00
|
|
|
const { onInputChange, renderHint } = this;
|
2016-09-12 15:14:07 +00:00
|
|
|
|
2021-09-10 19:06:37 +00:00
|
|
|
const wrapperClasses = classnames(baseClass, {
|
|
|
|
|
[`${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}>
|
2016-09-14 20:31:54 +00:00
|
|
|
{this.renderHeading()}
|
2016-09-12 15:14:07 +00:00
|
|
|
<input
|
2021-04-28 22:14:26 +00:00
|
|
|
id={name}
|
2016-09-12 15:14:07 +00:00
|
|
|
name={name}
|
|
|
|
|
onChange={onInputChange}
|
2016-11-03 19:40:54 +00:00
|
|
|
className={inputClasses}
|
2016-09-12 15:14:07 +00:00
|
|
|
placeholder={placeholder}
|
2021-04-12 13:32:25 +00:00
|
|
|
ref={(r) => {
|
|
|
|
|
this.input = r;
|
|
|
|
|
}}
|
2016-12-01 18:57:19 +00:00
|
|
|
tabIndex={tabIndex}
|
2016-09-12 15:14:07 +00:00
|
|
|
type={type}
|
2016-10-03 17:54:22 +00:00
|
|
|
value={value}
|
2021-07-06 16:22:51 +00:00
|
|
|
disabled={disabled}
|
2016-09-12 15:14:07 +00:00
|
|
|
/>
|
2021-06-07 01:56:30 +00:00
|
|
|
{iconName && <FleetIcon name={iconName} className={iconClasses} />}
|
2016-12-01 18:57:19 +00:00
|
|
|
{renderHint()}
|
2016-09-12 15:14:07 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 19:40:54 +00:00
|
|
|
export default InputFieldWithIcon;
|