'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _ErrorMessage = require('../ErrorMessage');

var _ErrorMessage2 = _interopRequireDefault(_ErrorMessage);

require('./style.css');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var InputText = function (_React$Component) {
  _inherits(InputText, _React$Component);

  function InputText(props) {
    _classCallCheck(this, InputText);

    var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

    _this.state = { value: '' };
    _this.handleChange = _this.handleChange.bind(_this);
    return _this;
  }

  InputText.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    this.setState({ value: nextProps.defaultText });
  };

  InputText.prototype.handleChange = function handleChange(event) {
    var input = event.target.value;
    this.setState({ value: input });
    // invokes custom function if passed in the component
    if (typeof this.props.onChange === 'function') {
      this.props.onChange(input);
    }
  };

  InputText.prototype.render = function render() {
    var _props = this.props,
        name = _props.name,
        id = _props.id,
        type = _props.type,
        placeholder = _props.placeholder,
        maxlength = _props.maxlength,
        pattern = _props.pattern,
        width = _props.width,
        required = _props.required,
        labelText = _props.labelText,
        hiddenLabel = _props.hiddenLabel,
        errorMsg = _props.errorMsg;

    var inputLabelClasses = ['ma__label'];
    if (labelText) {
      inputLabelClasses.push('ma__label--' + (required ? 'required' : 'optional'));
      if (hiddenLabel) {
        inputLabelClasses.push('ma__label--hidden');
      }
    }
    var inputClasses = ['ma__input'];
    if (required) {
      inputClasses.push('js-is-required');
    }
    return _react2.default.createElement(
      _react2.default.Fragment,
      null,
      labelText && _react2.default.createElement(
        'label',
        {
          htmlFor: id,
          className: inputLabelClasses.join(' ')
        },
        labelText
      ),
      _react2.default.createElement('input', {
        className: inputClasses.join(' '),
        name: name,
        id: id,
        type: type,
        placeholder: placeholder,
        'data-type': type,
        maxLength: maxlength || null,
        pattern: pattern || null,
        style: width ? { width: width + 'px' } : null,
        onChange: this.handleChange,
        required: required,
        value: this.state.value
      }),
      errorMsg && _react2.default.createElement(_ErrorMessage2.default, { error: errorMsg, inputId: id })
    );
  };

  return InputText;
}(_react2.default.Component);

InputText.propTypes = process.env.NODE_ENV !== "production" ? {
  /** Whether the label should be hidden or not */
  hiddenLabel: _propTypes2.default.bool,
  /** The label text for the input field */
  labelText: _propTypes2.default.string.isRequired,
  /** Whether the field is required or not */
  required: _propTypes2.default.bool,
  /** The unique ID for the input field */
  id: _propTypes2.default.string.isRequired,
  /** The name for the input field */
  name: _propTypes2.default.string.isRequired,
  /** The type of the input field */
  type: _propTypes2.default.string.isRequired,
  /** The max acceptable input length */
  maxlength: _propTypes2.default.number,
  /** The pattern to filter input against, e.g. "[0-9]" for numbers only */
  pattern: _propTypes2.default.string,
  /** The number of characters wide to make the input field */
  width: _propTypes2.default.number,
  /** The placeholder text for the input field */
  placeholder: _propTypes2.default.string,
  /** The message to be displayed in the event of an error */
  errorMsg: _propTypes2.default.string,
  /** Custom change function */
  onChange: _propTypes2.default.func,
  /** Default input text value */
  defaultText: _propTypes2.default.string
} : {};

InputText.defaultProps = {
  hiddenLabel: false,
  required: false
};

exports.default = InputText;
module.exports = exports['default'];