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

71 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-07-11 14:59:01 +02:00
//@flow
import React from "react";
2018-07-26 15:29:21 +02:00
import classNames from "classnames";
2018-07-11 14:59:01 +02:00
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-26 15:29:21 +02:00
onChange: string => void,
validationError: boolean,
errorMessage: string
2018-07-11 14:59:01 +02:00
};
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-26 15:29:21 +02:00
const { type, placeholder, value, validationError, errorMessage } = this.props;
const errorView = validationError ? "is-danger" : "";
const helper = validationError ? <p className="help is-danger">{errorMessage}</p> : "";
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-26 15:29:21 +02:00
className={ classNames(
"input",
errorView
)}
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>
2018-07-26 15:29:21 +02:00
{helper}
2018-07-11 14:59:01 +02:00
</div>
);
}
}
export default InputField;