2021-04-12 13:32:25 +00:00
|
|
|
import React, { Component } from "react";
|
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
|
import classnames from "classnames";
|
|
|
|
|
import { noop, pick } from "lodash";
|
2016-12-16 15:54:49 +00:00
|
|
|
|
2023-08-08 14:57:55 +00:00
|
|
|
import { stringToClipboard } from "utilities/copy_text";
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
import FormField from "components/forms/FormField";
|
2023-08-08 14:57:55 +00:00
|
|
|
import Button from "components/buttons/Button";
|
|
|
|
|
import Icon from "components/Icon";
|
2016-10-19 20:22:18 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
const baseClass = "input-field";
|
2016-09-30 18:55:15 +00:00
|
|
|
|
|
|
|
|
class InputField extends Component {
|
|
|
|
|
static propTypes = {
|
|
|
|
|
autofocus: PropTypes.bool,
|
2024-06-11 15:11:40 +00:00
|
|
|
/** readOnly displays a non-editable field */
|
|
|
|
|
readOnly: PropTypes.bool,
|
|
|
|
|
/** disabled displays a greyed out non-editable field */
|
2017-01-13 23:27:58 +00:00
|
|
|
disabled: PropTypes.bool,
|
2016-09-30 18:55:15 +00:00
|
|
|
error: PropTypes.string,
|
2016-10-31 18:08:54 +00:00
|
|
|
inputClassName: PropTypes.string, // eslint-disable-line react/forbid-prop-types
|
2016-11-03 19:40:54 +00:00
|
|
|
inputWrapperClass: PropTypes.string,
|
2016-10-21 23:13:41 +00:00
|
|
|
inputOptions: PropTypes.object, // eslint-disable-line react/forbid-prop-types
|
2016-09-30 18:55:15 +00:00
|
|
|
name: PropTypes.string,
|
|
|
|
|
onChange: PropTypes.func,
|
2022-01-21 17:06:58 +00:00
|
|
|
onBlur: PropTypes.func,
|
2016-12-21 17:07:13 +00:00
|
|
|
onFocus: PropTypes.func,
|
2016-09-30 18:55:15 +00:00
|
|
|
placeholder: PropTypes.string,
|
|
|
|
|
type: PropTypes.string,
|
2022-03-16 06:38:10 +00:00
|
|
|
blockAutoComplete: PropTypes.bool,
|
2021-04-12 13:32:25 +00:00
|
|
|
value: PropTypes.oneOfType([
|
|
|
|
|
PropTypes.bool,
|
|
|
|
|
PropTypes.string,
|
|
|
|
|
PropTypes.number,
|
|
|
|
|
]).isRequired,
|
2025-08-05 20:29:55 +00:00
|
|
|
/** Returns both name and value */
|
2022-01-21 17:06:58 +00:00
|
|
|
parseTarget: PropTypes.bool,
|
2024-10-15 13:23:59 +00:00
|
|
|
tooltip: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
2024-03-20 20:53:34 +00:00
|
|
|
labelTooltipPosition: PropTypes.string,
|
2024-01-18 15:48:44 +00:00
|
|
|
helpText: PropTypes.oneOfType([
|
2023-01-27 22:25:53 +00:00
|
|
|
PropTypes.string,
|
|
|
|
|
PropTypes.arrayOf(PropTypes.string),
|
2023-02-22 14:05:38 +00:00
|
|
|
PropTypes.object,
|
2023-01-27 22:25:53 +00:00
|
|
|
]),
|
2025-04-23 18:42:30 +00:00
|
|
|
/** Use in conjunction with type "password" and enableCopy to see eye icon to view */
|
|
|
|
|
enableShowSecret: PropTypes.bool,
|
2023-08-08 14:57:55 +00:00
|
|
|
enableCopy: PropTypes.bool,
|
2023-09-11 14:01:31 +00:00
|
|
|
ignore1password: PropTypes.bool,
|
2025-08-05 20:29:55 +00:00
|
|
|
// Accepts string or number for HTML compatibility, (e.g., step="0.1", step={0.1})
|
|
|
|
|
/** Only effective on input type number */
|
|
|
|
|
step: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
|
|
|
/** Only effective on input type number */
|
|
|
|
|
min: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
|
|
|
/** Only effective on input type number */
|
|
|
|
|
max: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2016-09-30 18:55:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
|
autofocus: false,
|
2021-04-12 13:32:25 +00:00
|
|
|
inputWrapperClass: "",
|
2016-09-30 18:55:15 +00:00
|
|
|
inputOptions: {},
|
|
|
|
|
label: null,
|
2021-04-12 13:32:25 +00:00
|
|
|
labelClassName: "",
|
2016-12-21 17:07:13 +00:00
|
|
|
onFocus: noop,
|
2022-01-21 17:06:58 +00:00
|
|
|
onBlur: noop,
|
2021-04-12 13:32:25 +00:00
|
|
|
type: "text",
|
2022-01-20 17:39:55 +00:00
|
|
|
blockAutoComplete: false,
|
2021-04-12 13:32:25 +00:00
|
|
|
value: "",
|
2022-01-21 17:06:58 +00:00
|
|
|
parseTarget: false,
|
2022-02-28 21:25:06 +00:00
|
|
|
tooltip: "",
|
2024-05-31 13:22:28 +00:00
|
|
|
labelTooltipPosition: undefined,
|
2024-01-18 15:48:44 +00:00
|
|
|
helpText: "",
|
2023-08-08 14:57:55 +00:00
|
|
|
enableCopy: false,
|
2025-04-23 18:42:30 +00:00
|
|
|
enableShowSecret: false,
|
2023-09-11 14:01:31 +00:00
|
|
|
ignore1password: false,
|
2025-08-05 20:29:55 +00:00
|
|
|
step: undefined,
|
|
|
|
|
min: undefined,
|
|
|
|
|
max: undefined,
|
2016-09-30 18:55:15 +00:00
|
|
|
};
|
|
|
|
|
|
2023-11-29 17:27:01 +00:00
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
this.state = {
|
|
|
|
|
copied: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
componentDidMount() {
|
2016-09-30 18:55:15 +00:00
|
|
|
const { autofocus } = this.props;
|
|
|
|
|
const { input } = this;
|
|
|
|
|
|
2016-10-19 20:22:18 +00:00
|
|
|
if (autofocus) {
|
|
|
|
|
input.focus();
|
|
|
|
|
}
|
2016-09-30 18:55:15 +00:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onInputChange = (evt) => {
|
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
2022-01-21 17:06:58 +00:00
|
|
|
const { value, name } = evt.target;
|
|
|
|
|
const { onChange, parseTarget } = this.props;
|
|
|
|
|
|
|
|
|
|
if (parseTarget) {
|
|
|
|
|
// Returns both name and value
|
|
|
|
|
return onChange({ value, name });
|
|
|
|
|
}
|
2016-09-30 18:55:15 +00:00
|
|
|
|
2016-11-07 16:42:39 +00:00
|
|
|
return onChange(value);
|
2021-04-12 13:32:25 +00:00
|
|
|
};
|
2016-09-30 18:55:15 +00:00
|
|
|
|
2025-04-23 18:42:30 +00:00
|
|
|
onToggleSecret = (evt) => {
|
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
|
|
|
|
this.setState({ showSecret: !this.state.showSecret });
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-21 17:32:18 +00:00
|
|
|
onClickCopy = (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
stringToClipboard(this.props.value).then(() => {
|
|
|
|
|
this.setState({ copied: true });
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.setState({ copied: false });
|
|
|
|
|
}, 2000);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-23 18:42:30 +00:00
|
|
|
renderShowSecretButton = () => {
|
|
|
|
|
const { onToggleSecret } = this;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Button
|
|
|
|
|
variant="icon"
|
|
|
|
|
className={`${baseClass}__show-secret-icon`}
|
|
|
|
|
onClick={onToggleSecret}
|
2025-09-29 17:10:41 +00:00
|
|
|
size="small"
|
2025-04-23 18:42:30 +00:00
|
|
|
>
|
|
|
|
|
<Icon name="eye" />
|
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-05 11:47:34 +00:00
|
|
|
renderCopyButton = () => {
|
2025-05-21 17:32:18 +00:00
|
|
|
const { onClickCopy } = this;
|
2024-09-05 11:47:34 +00:00
|
|
|
|
2025-05-13 14:24:32 +00:00
|
|
|
const copyButtonValue = <Icon name="copy" />;
|
2026-02-16 20:11:40 +00:00
|
|
|
const wrapperClasses = classnames(`${baseClass}__copy-wrapper`, {
|
|
|
|
|
[`${baseClass}__copy-wrapper__text-area`]: this.props.type === "textarea",
|
|
|
|
|
});
|
2024-09-05 11:47:34 +00:00
|
|
|
|
|
|
|
|
const copiedConfirmationClasses = classnames(
|
2025-05-13 14:24:32 +00:00
|
|
|
`${baseClass}__copied-confirmation`
|
2024-09-05 11:47:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={wrapperClasses}>
|
2025-04-23 18:42:30 +00:00
|
|
|
{this.state.copied && (
|
|
|
|
|
<span className={copiedConfirmationClasses}>Copied!</span>
|
|
|
|
|
)}
|
2025-09-29 17:10:41 +00:00
|
|
|
<Button variant="icon" onClick={onClickCopy} size="small" iconStroke>
|
2024-09-05 11:47:34 +00:00
|
|
|
{copyButtonValue}
|
|
|
|
|
</Button>
|
2025-04-23 18:42:30 +00:00
|
|
|
{this.props.enableShowSecret && this.renderShowSecretButton()}
|
2024-09-05 11:47:34 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
render() {
|
2016-12-21 17:07:13 +00:00
|
|
|
const {
|
2024-06-11 15:11:40 +00:00
|
|
|
readOnly,
|
2017-01-13 23:27:58 +00:00
|
|
|
disabled,
|
2016-12-21 17:07:13 +00:00
|
|
|
error,
|
|
|
|
|
inputClassName,
|
|
|
|
|
inputOptions,
|
|
|
|
|
inputWrapperClass,
|
|
|
|
|
name,
|
|
|
|
|
onFocus,
|
2022-01-21 17:06:58 +00:00
|
|
|
onBlur,
|
2016-12-21 17:07:13 +00:00
|
|
|
placeholder,
|
|
|
|
|
type,
|
2022-01-20 17:39:55 +00:00
|
|
|
blockAutoComplete,
|
2016-12-21 17:07:13 +00:00
|
|
|
value,
|
2023-09-11 14:01:31 +00:00
|
|
|
ignore1password,
|
2024-09-05 11:47:34 +00:00
|
|
|
enableCopy,
|
2025-04-23 18:42:30 +00:00
|
|
|
enableShowSecret,
|
2025-08-05 20:29:55 +00:00
|
|
|
step,
|
|
|
|
|
min,
|
|
|
|
|
max,
|
2016-12-21 17:07:13 +00:00
|
|
|
} = this.props;
|
2023-02-22 14:05:38 +00:00
|
|
|
|
2016-12-16 15:54:49 +00:00
|
|
|
const { onInputChange } = this;
|
2025-04-23 18:42:30 +00:00
|
|
|
const shouldShowPasswordClass =
|
|
|
|
|
type === "password" && !this.state.showSecret;
|
2016-11-09 14:00:40 +00:00
|
|
|
const inputClasses = classnames(baseClass, inputClassName, {
|
|
|
|
|
[`${baseClass}--password`]: shouldShowPasswordClass,
|
2024-06-11 15:11:40 +00:00
|
|
|
[`${baseClass}--read-only`]: readOnly || disabled,
|
2017-01-13 23:27:58 +00:00
|
|
|
[`${baseClass}--disabled`]: disabled,
|
2016-11-09 14:00:40 +00:00
|
|
|
[`${baseClass}--error`]: error,
|
2021-04-12 13:32:25 +00:00
|
|
|
[`${baseClass}__textarea`]: type === "textarea",
|
2016-11-09 14:00:40 +00:00
|
|
|
});
|
2016-12-16 15:54:49 +00:00
|
|
|
|
2024-05-02 13:20:41 +00:00
|
|
|
const inputWrapperClasses = classnames(inputWrapperClass, {
|
2024-06-11 15:11:40 +00:00
|
|
|
[`input-field--read-only`]: readOnly || disabled,
|
2024-05-02 13:20:41 +00:00
|
|
|
[`input-field--disabled`]: disabled,
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-28 21:25:06 +00:00
|
|
|
const formFieldProps = pick(this.props, [
|
2024-01-18 15:48:44 +00:00
|
|
|
"helpText",
|
2022-02-28 21:25:06 +00:00
|
|
|
"label",
|
|
|
|
|
"error",
|
|
|
|
|
"name",
|
|
|
|
|
"tooltip",
|
2024-03-20 20:53:34 +00:00
|
|
|
"labelTooltipPosition",
|
2022-02-28 21:25:06 +00:00
|
|
|
]);
|
2016-09-30 18:55:15 +00:00
|
|
|
|
2025-04-23 18:42:30 +00:00
|
|
|
const inputContainerClasses = classnames(`${baseClass}__input-container`, {
|
|
|
|
|
"copy-enabled": enableCopy,
|
|
|
|
|
});
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
if (type === "textarea") {
|
2016-09-30 18:55:15 +00:00
|
|
|
return (
|
2021-04-12 13:32:25 +00:00
|
|
|
<FormField
|
|
|
|
|
{...formFieldProps}
|
|
|
|
|
type="textarea"
|
2024-05-02 13:20:41 +00:00
|
|
|
className={inputWrapperClasses}
|
2021-04-12 13:32:25 +00:00
|
|
|
>
|
2025-04-23 18:42:30 +00:00
|
|
|
<div className={inputContainerClasses}>
|
|
|
|
|
<textarea
|
|
|
|
|
name={name}
|
|
|
|
|
id={name}
|
|
|
|
|
onChange={onInputChange}
|
2026-02-28 01:51:56 +00:00
|
|
|
onBlur={onBlur}
|
|
|
|
|
onFocus={onFocus}
|
2025-04-23 18:42:30 +00:00
|
|
|
className={inputClasses}
|
|
|
|
|
disabled={readOnly || disabled}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
ref={(r) => {
|
|
|
|
|
this.input = r;
|
|
|
|
|
}}
|
|
|
|
|
type={type}
|
|
|
|
|
{...inputOptions}
|
|
|
|
|
value={value}
|
|
|
|
|
/>
|
|
|
|
|
{enableCopy && this.renderCopyButton()}
|
|
|
|
|
</div>
|
2016-12-16 15:54:49 +00:00
|
|
|
</FormField>
|
2016-09-30 18:55:15 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-23 18:42:30 +00:00
|
|
|
const inputType = this.state.showSecret ? "text" : type;
|
2023-08-08 14:57:55 +00:00
|
|
|
|
2016-09-30 18:55:15 +00:00
|
|
|
return (
|
2024-05-02 13:20:41 +00:00
|
|
|
<FormField
|
|
|
|
|
{...formFieldProps}
|
|
|
|
|
type="input"
|
|
|
|
|
className={inputWrapperClasses}
|
|
|
|
|
>
|
2023-08-08 14:57:55 +00:00
|
|
|
<div className={inputContainerClasses}>
|
|
|
|
|
<input
|
2024-06-11 15:11:40 +00:00
|
|
|
disabled={readOnly || disabled}
|
2023-08-08 14:57:55 +00:00
|
|
|
name={name}
|
|
|
|
|
id={name}
|
|
|
|
|
onChange={onInputChange}
|
|
|
|
|
onFocus={onFocus}
|
|
|
|
|
onBlur={onBlur}
|
|
|
|
|
className={inputClasses}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
ref={(r) => {
|
|
|
|
|
this.input = r;
|
|
|
|
|
}}
|
2025-04-23 18:42:30 +00:00
|
|
|
type={inputType}
|
2023-08-08 14:57:55 +00:00
|
|
|
{...inputOptions}
|
|
|
|
|
value={value}
|
|
|
|
|
autoComplete={blockAutoComplete ? "new-password" : ""}
|
2023-09-11 14:01:31 +00:00
|
|
|
data-1p-ignore={ignore1password}
|
2025-08-05 20:29:55 +00:00
|
|
|
step={step}
|
|
|
|
|
min={min}
|
|
|
|
|
max={max}
|
2023-08-08 14:57:55 +00:00
|
|
|
/>
|
2024-09-05 11:47:34 +00:00
|
|
|
|
|
|
|
|
{enableCopy && this.renderCopyButton()}
|
2023-08-08 14:57:55 +00:00
|
|
|
</div>
|
2016-12-16 15:54:49 +00:00
|
|
|
</FormField>
|
2016-09-30 18:55:15 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 19:40:54 +00:00
|
|
|
export default InputField;
|