Files
SCM-Manager/scm-ui/src/components/forms/InputField.js

63 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-07-11 14:59:01 +02:00
//@flow
import React from "react";
type Props = {
label?: string,
placeholder?: string,
2018-07-17 08:39:51 +02:00
value?: string,
2018-07-11 14:59:01 +02:00
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() {
2018-07-17 08:39:51 +02:00
const { type, placeholder, value } = this.props;
2018-07-11 14:59:01 +02:00
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}
2018-07-17 08:39:51 +02:00
value={value}
2018-07-11 14:59:01 +02:00
onChange={this.handleInput}
/>
</div>
</div>
);
}
}
export default InputField;