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
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
import FormField from "components/forms/FormField";
|
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,
|
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,
|
2022-01-21 17:06:58 +00:00
|
|
|
parseTarget: PropTypes.bool,
|
2022-02-28 21:25:06 +00:00
|
|
|
tooltip: PropTypes.string,
|
2023-01-27 22:25:53 +00:00
|
|
|
hint: PropTypes.oneOfType([
|
|
|
|
|
PropTypes.string,
|
|
|
|
|
PropTypes.arrayOf(PropTypes.string),
|
|
|
|
|
]),
|
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: "",
|
2023-01-27 22:25:53 +00:00
|
|
|
hint: "",
|
2016-09-30 18:55:15 +00:00
|
|
|
};
|
|
|
|
|
|
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
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
render() {
|
2016-12-21 17:07:13 +00:00
|
|
|
const {
|
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,
|
|
|
|
|
} = this.props;
|
2016-12-16 15:54:49 +00:00
|
|
|
const { onInputChange } = this;
|
2021-04-12 13:32:25 +00:00
|
|
|
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,
|
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
|
|
|
|
2022-02-28 21:25:06 +00:00
|
|
|
const formFieldProps = pick(this.props, [
|
|
|
|
|
"hint",
|
|
|
|
|
"label",
|
|
|
|
|
"error",
|
|
|
|
|
"name",
|
|
|
|
|
"tooltip",
|
|
|
|
|
]);
|
2016-09-30 18:55:15 +00:00
|
|
|
|
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"
|
|
|
|
|
className={inputWrapperClass}
|
|
|
|
|
>
|
2016-09-30 18:55:15 +00:00
|
|
|
<textarea
|
|
|
|
|
name={name}
|
2021-04-09 15:47:16 +00:00
|
|
|
id={name}
|
2016-09-30 18:55:15 +00:00
|
|
|
onChange={onInputChange}
|
2016-11-03 19:40:54 +00:00
|
|
|
className={inputClasses}
|
2017-01-13 23:27:58 +00:00
|
|
|
disabled={disabled}
|
2016-09-30 18:55:15 +00:00
|
|
|
placeholder={placeholder}
|
2021-04-12 13:32:25 +00:00
|
|
|
ref={(r) => {
|
|
|
|
|
this.input = r;
|
|
|
|
|
}}
|
2016-09-30 18:55:15 +00:00
|
|
|
type={type}
|
|
|
|
|
{...inputOptions}
|
2016-10-03 17:54:22 +00:00
|
|
|
value={value}
|
2016-09-30 18:55:15 +00:00
|
|
|
/>
|
2016-12-16 15:54:49 +00:00
|
|
|
</FormField>
|
2016-09-30 18:55:15 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2016-12-16 15:54:49 +00:00
|
|
|
<FormField {...formFieldProps} type="input" className={inputWrapperClass}>
|
2016-09-30 18:55:15 +00:00
|
|
|
<input
|
2017-01-13 23:27:58 +00:00
|
|
|
disabled={disabled}
|
2016-09-30 18:55:15 +00:00
|
|
|
name={name}
|
2021-04-09 15:47:16 +00:00
|
|
|
id={name}
|
2016-09-30 18:55:15 +00:00
|
|
|
onChange={onInputChange}
|
2016-12-21 17:07:13 +00:00
|
|
|
onFocus={onFocus}
|
2022-01-21 17:06:58 +00:00
|
|
|
onBlur={onBlur}
|
2016-11-03 19:40:54 +00:00
|
|
|
className={inputClasses}
|
2016-09-30 18:55:15 +00:00
|
|
|
placeholder={placeholder}
|
2021-04-12 13:32:25 +00:00
|
|
|
ref={(r) => {
|
|
|
|
|
this.input = r;
|
|
|
|
|
}}
|
2016-09-30 18:55:15 +00:00
|
|
|
type={type}
|
|
|
|
|
{...inputOptions}
|
2016-10-03 17:54:22 +00:00
|
|
|
value={value}
|
2022-01-20 17:39:55 +00:00
|
|
|
autoComplete={blockAutoComplete ? "new-password" : ""}
|
2016-09-30 18:55:15 +00:00
|
|
|
/>
|
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;
|