"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var React = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _withStyles = _interopRequireDefault(require("../../hoc/withStyles"));

var _FieldLabel = _interopRequireDefault(require("../FieldLabel"));

var _ErrorBoundary = _interopRequireDefault(require("../../hoc/ErrorBoundary"));

var _styles = _interopRequireDefault(require("./styles"));

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var TextField =
/*#__PURE__*/
function (_React$Component) {
  _inherits(TextField, _React$Component);

  function TextField() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, TextField);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(TextField)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "inputRef", void 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      value: _this.props.value,
      isFocused: false,
      showingPassword: false
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "focus", function () {
      if (_this.inputRef) {
        _this.inputRef.focus();
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_handleOnChange", function (e) {
      _this.setState({
        value: e.currentTarget.value
      });

      if (_this.props.onChangeText) {
        _this.props.onChangeText(e.currentTarget.value);
      }

      if (_this.props.onChange) {
        _this.props.onChange(e);
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_handleOnFocus", function (e) {
      _this.setState({
        isFocused: true
      });

      if (_this.props.onFocus) {
        _this.props.onFocus(e);
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_handleOnBlur", function (e) {
      _this.setState({
        isFocused: false
      });

      if (_this.props.onBlur) {
        _this.props.onBlur(e);
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_handleOnKeyPress", function (e) {
      if (_this.props.isNumber) {
        if (/[\d\.]/.test(e.key)) {
          if (_this.props.onKeyPress) {
            _this.props.onKeyPress(e);
          }

          return true;
        }

        e.preventDefault();
        return false;
      }

      if (_this.props.onKeyPress) {
        _this.props.onKeyPress(e);
      }

      return true;
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_clearTextField", function () {
      _this.setState({
        value: ''
      });

      if (_this.props.onChangeText) {
        _this.props.onChangeText('');
      }

      if (_this.inputRef) {
        _this.inputRef.focus();
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_showPassowrd", function () {
      _this.inputRef.type = 'text';

      _this.setState({
        showingPassword: true
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_hidePassowrd", function () {
      _this.inputRef.type = 'password';

      _this.setState({
        showingPassword: false
      });
    });

    return _this;
  }

  _createClass(TextField, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.props.getRef(this);
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var props = this.props;
      var state = this.state;

      if (props.value !== prevProps.value || state.value !== props.value) {
        this.setState({
          value: props.value
        });
      }
    }
  }, {
    key: "_renderIcon",
    value: function _renderIcon() {
      var _this$props = this.props,
          icon = _this$props.icon,
          classes = _this$props.classes;

      if (!icon) {
        return null;
      }

      return React.createElement("div", {
        className: (0, _classnames.default)(classes.leftIcon)
      }, icon);
    }
  }, {
    key: "_renderTextInput",
    value: function _renderTextInput() {
      var _this2 = this;

      var _this$props2 = this.props,
          classes = _this$props2.classes,
          multiline = _this$props2.multiline;

      if (multiline) {
        return React.createElement("textarea", {
          className: (0, _classnames.default)(classes.textInput, classes.textarea),
          ref: function ref(_ref) {
            _this2.inputRef = _ref;
          },
          value: this.state.value,
          placeholder: this.props.placeholder,
          autoFocus: this.props.autoFocus,
          disabled: this.props.disabled,
          id: this.props.id,
          name: this.props.name,
          onBlur: this._handleOnBlur,
          onChange: this._handleOnChange,
          onFocus: this._handleOnFocus,
          onKeyDown: this.props.onKeyDown,
          onKeyPress: this.props.onKeyPress,
          onKeyUp: this.props.onKeyUp,
          readOnly: this.props.isReadOnly,
          required: this.props.required
        });
      }

      return React.createElement("input", {
        className: (0, _classnames.default)(classes.textInput),
        ref: function ref(_ref2) {
          _this2.inputRef = _ref2;
        },
        value: this.state.value,
        placeholder: this.props.placeholder,
        autoComplete: this.props.autoComplete,
        autoFocus: this.props.autoFocus,
        disabled: this.props.disabled,
        id: this.props.id,
        name: this.props.name,
        onBlur: this._handleOnBlur,
        onChange: this._handleOnChange,
        onFocus: this._handleOnFocus,
        onKeyDown: this.props.onKeyDown,
        onKeyPress: this._handleOnKeyPress,
        onKeyUp: this.props.onKeyUp,
        readOnly: this.props.isReadOnly,
        required: this.props.required,
        type: this.props.type,
        maxLength: this.props.maxlength
      });
    }
  }, {
    key: "_renderClearIcon",
    value: function _renderClearIcon() {
      var classes = this.props.classes;

      if (!this.state.value) {
        return null;
      }

      return React.createElement("span", {
        className: (0, _classnames.default)(classes.xIcon),
        onClick: this._clearTextField
      }, React.createElement("i", {
        className: "icon icon-close"
      }));
    }
  }, {
    key: "_renderShowPasswordIcon",
    value: function _renderShowPasswordIcon() {
      var classes = this.props.classes;

      if (this.props.type !== 'password' || !this.state.value) {
        return null;
      }

      if (this.state.showingPassword) {
        return React.createElement("span", {
          onClick: this._hidePassowrd,
          className: (0, _classnames.default)(classes.eyeIcon, 'show')
        }, React.createElement("i", {
          className: 'icon icon-show'
        }));
      }

      return React.createElement("span", {
        onClick: this._showPassowrd,
        className: (0, _classnames.default)(classes.eyeIcon)
      }, React.createElement("i", {
        className: 'icon icon-show'
      }));
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          classes = _this$props3.classes,
          label = _this$props3.label,
          error = _this$props3.error,
          showErrorMessage = _this$props3.showErrorMessage,
          showClearIcon = _this$props3.showClearIcon,
          extraLinkWithLabel = _this$props3.extraLinkWithLabel,
          disabled = _this$props3.disabled,
          isReadOnly = _this$props3.isReadOnly,
          multiline = _this$props3.multiline;
      var wrapperClass = (0, _classnames.default)(classes.textInputWrapper, {
        'top-border-radius': this.props.applyTopBorderRadius,
        'bottom-border-radius': this.props.applyBottomBorderRadius,
        'no-border-radius': this.props.noBorderRadius,
        focus: this.state.isFocused || this.state.value,
        'is-filled': Boolean(this.state.value)
      });
      var componentClass = (0, _classnames.default)(classes.component, {
        error: Boolean(error)
      });
      var errorClass = (0, _classnames.default)(classes.error);
      return React.createElement(_ErrorBoundary.default, null, React.createElement("div", {
        className: componentClass
      }, (Boolean(label) || Boolean(extraLinkWithLabel)) && React.createElement(_FieldLabel.default, {
        label: label,
        extraLinkWithLabel: extraLinkWithLabel,
        onExtraLinkClicked: this.props.onExtraLinkClicked,
        classes: {
          label: classes.label
        }
      }), React.createElement("div", {
        className: wrapperClass
      }, this._renderIcon(), this._renderTextInput(), showClearIcon && !disabled && !isReadOnly && !multiline && this._renderClearIcon(), !disabled && !isReadOnly && this._renderShowPasswordIcon()), Boolean(error) && showErrorMessage && React.createElement("div", {
        className: errorClass
      }, error)));
    }
  }]);

  return TextField;
}(React.Component);

_defineProperty(TextField, "propTypes", {
  applyBottomBorderRadius: _propTypes.default.bool,
  applyTopBorderRadius: _propTypes.default.bool,
  autoComplete: _propTypes.default.oneOf(['on', 'off']),
  autoFocus: _propTypes.default.bool,
  classes: _propTypes.default.object,
  disabled: _propTypes.default.bool,
  error: _propTypes.default.string,
  errorColor: _propTypes.default.string,
  extraLinkWithLabel: _propTypes.default.string,
  icon: _propTypes.default.node,
  id: _propTypes.default.string,
  isReadOnly: _propTypes.default.bool,
  label: _propTypes.default.string,
  multiline: _propTypes.default.bool,
  name: _propTypes.default.string,
  noBorderRadius: _propTypes.default.bool,
  onBlur: _propTypes.default.func,
  onChange: _propTypes.default.func,
  onChangeText: _propTypes.default.func,
  onExtraLinkClicked: _propTypes.default.func,
  onFocus: _propTypes.default.func,
  onKeyDown: _propTypes.default.func,
  onKeyPress: _propTypes.default.func,
  onKeyUp: _propTypes.default.func,
  placeholder: _propTypes.default.string,
  placeholderColor: _propTypes.default.string,
  required: _propTypes.default.bool,
  showClearIcon: _propTypes.default.bool,
  showErrorMessage: _propTypes.default.bool,
  type: _propTypes.default.string,
  value: _propTypes.default.string,
  maxlength: _propTypes.default.number,
  isNumber: _propTypes.default.bool,
  getRef: _propTypes.default.func
});

_defineProperty(TextField, "defaultProps", {
  autoComplete: 'on',
  type: 'text',
  disabled: false,
  isReadOnly: false,
  required: false,
  error: '',
  showErrorMessage: true,
  label: '',
  name: '',
  value: '',
  onBlur: function onBlur(e) {},
  onChangeText: function onChangeText() {},
  onFocus: function onFocus() {},
  onKeyDown: function onKeyDown() {},
  onKeyPress: function onKeyPress() {},
  onKeyUp: function onKeyUp() {},
  autoFocus: false,
  showClearIcon: false,
  extraLinkWithLabel: '',
  onExtraLinkClicked: function onExtraLinkClicked() {},
  multiline: false,
  maxlength: undefined,
  isNumber: false,
  getRef: function getRef() {}
});

var _default = (0, _withStyles.default)(_styles.default)(TextField);

exports.default = _default;