2019-10-20 16:59:02 +02:00
|
|
|
import React, { ChangeEvent, KeyboardEvent } from "react";
|
|
|
|
|
import classNames from "classnames";
|
|
|
|
|
import LabelWithHelpIcon from "./LabelWithHelpIcon";
|
2018-09-03 16:17:36 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
label?: string;
|
|
|
|
|
name?: string;
|
|
|
|
|
placeholder?: string;
|
|
|
|
|
value?: string;
|
|
|
|
|
type?: string;
|
|
|
|
|
autofocus?: boolean;
|
|
|
|
|
onChange: (value: string, name?: string) => void;
|
|
|
|
|
onReturnPressed?: () => void;
|
|
|
|
|
validationError: boolean;
|
|
|
|
|
errorMessage: string;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
helpText?: string;
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class InputField extends React.Component<Props> {
|
|
|
|
|
static defaultProps = {
|
2019-10-20 16:59:02 +02:00
|
|
|
type: "text",
|
|
|
|
|
placeholder: ""
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
field: HTMLInputElement | null | undefined;
|
2018-09-03 16:17:36 +02:00
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
if (this.props.autofocus && this.field) {
|
|
|
|
|
this.field.focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
handleInput = (event: ChangeEvent<HTMLInputElement>) => {
|
2018-11-06 07:59:34 +01:00
|
|
|
this.props.onChange(event.target.value, this.props.name);
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
handleKeyPress = (event: KeyboardEvent<HTMLInputElement>) => {
|
2018-09-03 16:17:36 +02:00
|
|
|
const onReturnPressed = this.props.onReturnPressed;
|
|
|
|
|
if (!onReturnPressed) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-10-20 16:59:02 +02:00
|
|
|
if (event.key === "Enter") {
|
2018-09-03 16:17:36 +02:00
|
|
|
event.preventDefault();
|
|
|
|
|
onReturnPressed();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const {
|
|
|
|
|
type,
|
|
|
|
|
placeholder,
|
|
|
|
|
value,
|
|
|
|
|
validationError,
|
|
|
|
|
errorMessage,
|
2018-10-02 13:34:04 +02:00
|
|
|
disabled,
|
|
|
|
|
label,
|
2019-10-20 16:59:02 +02:00
|
|
|
helpText
|
2018-09-03 16:17:36 +02:00
|
|
|
} = this.props;
|
2019-10-20 16:59:02 +02:00
|
|
|
const errorView = validationError ? "is-danger" : "";
|
2018-09-03 16:17:36 +02:00
|
|
|
const helper = validationError ? (
|
|
|
|
|
<p className="help is-danger">{errorMessage}</p>
|
|
|
|
|
) : (
|
2019-10-20 16:59:02 +02:00
|
|
|
""
|
2018-09-03 16:17:36 +02:00
|
|
|
);
|
|
|
|
|
return (
|
2018-10-04 10:16:20 +02:00
|
|
|
<div className="field">
|
|
|
|
|
<LabelWithHelpIcon label={label} helpText={helpText} />
|
|
|
|
|
<div className="control">
|
|
|
|
|
<input
|
|
|
|
|
ref={input => {
|
|
|
|
|
this.field = input;
|
|
|
|
|
}}
|
2019-10-20 16:59:02 +02:00
|
|
|
className={classNames("input", errorView)}
|
2018-10-04 10:16:20 +02:00
|
|
|
type={type}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
value={value}
|
|
|
|
|
onChange={this.handleInput}
|
|
|
|
|
onKeyPress={this.handleKeyPress}
|
|
|
|
|
disabled={disabled}
|
2018-10-02 13:34:04 +02:00
|
|
|
/>
|
2018-10-04 10:16:20 +02:00
|
|
|
</div>
|
|
|
|
|
{helper}
|
2018-09-03 16:17:36 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default InputField;
|