fleet/frontend/components/forms/fields/InputField/InputField.jsx

212 lines
5.1 KiB
React
Raw Normal View History

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
import { stringToClipboard } from "utilities/copy_text";
import FormField from "components/forms/FormField";
import Button from "components/buttons/Button";
import Icon from "components/Icon";
const baseClass = "input-field";
class InputField extends Component {
static propTypes = {
autofocus: PropTypes.bool,
2017-01-13 23:27:58 +00:00
disabled: PropTypes.bool,
error: PropTypes.string,
inputClassName: PropTypes.string, // eslint-disable-line react/forbid-prop-types
inputWrapperClass: PropTypes.string,
inputOptions: PropTypes.object, // eslint-disable-line react/forbid-prop-types
name: PropTypes.string,
onChange: PropTypes.func,
onBlur: PropTypes.func,
onFocus: PropTypes.func,
placeholder: PropTypes.string,
type: PropTypes.string,
2022-03-16 06:38:10 +00:00
blockAutoComplete: PropTypes.bool,
value: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string,
PropTypes.number,
]).isRequired,
parseTarget: PropTypes.bool,
tooltip: PropTypes.string,
2023-01-27 22:25:53 +00:00
hint: PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.string),
PropTypes.object,
2023-01-27 22:25:53 +00:00
]),
enableCopy: PropTypes.bool,
ignore1password: PropTypes.bool,
};
static defaultProps = {
autofocus: false,
inputWrapperClass: "",
inputOptions: {},
label: null,
labelClassName: "",
onFocus: noop,
onBlur: noop,
type: "text",
blockAutoComplete: false,
value: "",
parseTarget: false,
tooltip: "",
2023-01-27 22:25:53 +00:00
hint: "",
enableCopy: false,
ignore1password: false,
};
constructor() {
super();
this.state = {
copied: false,
};
}
componentDidMount() {
const { autofocus } = this.props;
const { input } = this;
if (autofocus) {
input.focus();
}
return false;
}
onInputChange = (evt) => {
evt.preventDefault();
const { value, name } = evt.target;
const { onChange, parseTarget } = this.props;
if (parseTarget) {
// Returns both name and value
return onChange({ value, name });
}
return onChange(value);
};
render() {
const {
2017-01-13 23:27:58 +00:00
disabled,
error,
inputClassName,
inputOptions,
inputWrapperClass,
name,
onFocus,
onBlur,
placeholder,
type,
blockAutoComplete,
value,
ignore1password,
} = this.props;
2016-12-16 15:54:49 +00:00
const { onInputChange } = this;
const shouldShowPasswordClass = type === "password";
2016-11-09 14:00:40 +00:00
const inputClasses = classnames(baseClass, inputClassName, {
[`${baseClass}--password`]: shouldShowPasswordClass,
2017-01-13 23:27:58 +00:00
[`${baseClass}--disabled`]: disabled,
2016-11-09 14:00:40 +00:00
[`${baseClass}--error`]: error,
[`${baseClass}__textarea`]: type === "textarea",
2016-11-09 14:00:40 +00:00
});
2016-12-16 15:54:49 +00:00
const formFieldProps = pick(this.props, [
"hint",
"label",
"error",
"name",
"tooltip",
]);
const copyValue = (e) => {
e.preventDefault();
stringToClipboard(value).then(() => {
this.setState({ copied: true });
setTimeout(() => {
this.setState({ copied: false });
}, 2000);
});
};
if (type === "textarea") {
return (
<FormField
{...formFieldProps}
type="textarea"
className={inputWrapperClass}
>
<textarea
name={name}
id={name}
onChange={onInputChange}
className={inputClasses}
2017-01-13 23:27:58 +00:00
disabled={disabled}
placeholder={placeholder}
ref={(r) => {
this.input = r;
}}
type={type}
{...inputOptions}
value={value}
/>
2016-12-16 15:54:49 +00:00
</FormField>
);
}
const inputContainerClasses = classnames(`${baseClass}__input-container`, {
"copy-enabled": this.props.enableCopy,
});
return (
2016-12-16 15:54:49 +00:00
<FormField {...formFieldProps} type="input" className={inputWrapperClass}>
<div className={inputContainerClasses}>
<input
disabled={disabled}
name={name}
id={name}
onChange={onInputChange}
onFocus={onFocus}
onBlur={onBlur}
className={inputClasses}
placeholder={placeholder}
ref={(r) => {
this.input = r;
}}
type={type}
{...inputOptions}
value={value}
autoComplete={blockAutoComplete ? "new-password" : ""}
data-1p-ignore={ignore1password}
/>
{this.props.enableCopy && (
<div className={`${baseClass}__copy-wrapper`}>
<Button
variant="text-icon"
onClick={copyValue}
className={`${baseClass}__copy-value-button`}
>
<Icon name="copy" /> Copy
</Button>
{this.state.copied && (
<span className={`${baseClass}__copied-confirmation`}>
Copied!
</span>
)}
</div>
)}
</div>
2016-12-16 15:54:49 +00:00
</FormField>
);
}
}
export default InputField;