2018-03-01 17:53:29 +00:00
|
|
|
import React, { HTMLProps } from 'react';
|
|
|
|
import './Input.scss';
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
hasBlurred: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Input extends React.Component<HTMLProps<HTMLInputElement>, State> {
|
|
|
|
public state: State = {
|
|
|
|
hasBlurred: false
|
|
|
|
};
|
2018-03-03 19:11:05 +00:00
|
|
|
|
2018-03-01 17:53:29 +00:00
|
|
|
public render() {
|
|
|
|
return (
|
|
|
|
<input
|
|
|
|
{...this.props}
|
|
|
|
onBlur={e => {
|
|
|
|
this.setState({ hasBlurred: true });
|
|
|
|
if (this.props && this.props.onBlur) {
|
|
|
|
this.props.onBlur(e);
|
|
|
|
}
|
|
|
|
}}
|
2018-03-03 19:11:05 +00:00
|
|
|
onWheel={this.props.type === 'number' ? this.preventNumberScroll : undefined}
|
2018-03-01 17:53:29 +00:00
|
|
|
className={`input-group-input ${this.props.className} ${
|
|
|
|
this.state.hasBlurred ? 'has-blurred' : ''
|
|
|
|
} ${!!this.props.value && this.props.value.toString().length > 0 ? 'has-value' : ''}`}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-03-03 19:11:05 +00:00
|
|
|
|
|
|
|
// When number inputs are scrolled on while in focus, the number changes. So we blur
|
|
|
|
// it if it's focused to prevent that behavior, without preventing the scroll.
|
|
|
|
private preventNumberScroll(ev: React.WheelEvent<HTMLInputElement>) {
|
|
|
|
if (document.activeElement === ev.currentTarget) {
|
|
|
|
ev.currentTarget.blur();
|
|
|
|
}
|
|
|
|
}
|
2018-03-01 17:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Input;
|