2018-07-11 14:59:01 +02:00
|
|
|
//@flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
label?: string,
|
|
|
|
|
placeholder?: string,
|
|
|
|
|
type?: string,
|
2018-07-11 21:01:29 +02:00
|
|
|
autofocus?: boolean,
|
2018-07-11 14:59:01 +02:00
|
|
|
onChange: string => void
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class InputField extends React.Component<Props> {
|
|
|
|
|
static defaultProps = {
|
|
|
|
|
type: "text",
|
|
|
|
|
placeholder: ""
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-11 21:01:29 +02:00
|
|
|
field: ?HTMLInputElement;
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
if (this.props.autofocus && this.field) {
|
|
|
|
|
this.field.focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-11 14:59:01 +02:00
|
|
|
handleInput = (event: SyntheticInputEvent<HTMLInputElement>) => {
|
|
|
|
|
this.props.onChange(event.target.value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
renderLabel = () => {
|
|
|
|
|
const label = this.props.label;
|
|
|
|
|
if (label) {
|
2018-07-11 17:02:38 +02:00
|
|
|
return <label className="label">{label}</label>;
|
2018-07-11 14:59:01 +02:00
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { type, placeholder } = this.props;
|
|
|
|
|
|
|
|
|
|
return (
|
2018-07-11 17:02:38 +02:00
|
|
|
<div className="field">
|
2018-07-11 14:59:01 +02:00
|
|
|
{this.renderLabel()}
|
2018-07-11 17:02:38 +02:00
|
|
|
<div className="control">
|
2018-07-11 14:59:01 +02:00
|
|
|
<input
|
2018-07-11 21:01:29 +02:00
|
|
|
ref={input => {
|
|
|
|
|
this.field = input;
|
|
|
|
|
}}
|
2018-07-11 17:02:38 +02:00
|
|
|
className="input"
|
2018-07-11 14:59:01 +02:00
|
|
|
type={type}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
onChange={this.handleInput}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default InputField;
|