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