import React, {PureComponent} from 'react';
import omit from 'lodash.omit';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import FormField from 'shared/components/form/form_field';

import './input.scss';

@FormField
class Input extends PureComponent {
  static propTypes = {
    value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
    isValid: PropTypes.bool,
    className: PropTypes.string,
    onChange: PropTypes.func
  };

  focus() {
    this.element.focus();
  }

  onChange = event => this.props.onChange(event.target.value);

  render() {
    const {isValid, className} = this.props;

    return (
      <input
        {...omit(this.props, 'isValid', 'required')}
        value={this.props.value || ''}
        ref={element => (this.element = element)}
        className={classNames('mo-web-input', className, {
          'mo-web-input--invalid': !isValid
        })}
        onChange={this.onChange}
      />
    );
  }
}

export default Input;
