"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

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

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

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

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

var _Input = _interopRequireDefault(require("./Input"));

var _button = _interopRequireDefault(require("../button"));

var _enum = require("../_util/enum");

var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext"));

var _icon = _interopRequireDefault(require("../icon"));

var _excluded = ["className", "inputPrefixCls", "size", "suffix", "enterButton"];

var Search = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(Search, _Component);

  var _super = (0, _createSuper2["default"])(Search);

  function Search() {
    var _this;

    (0, _classCallCheck2["default"])(this, Search);
    _this = _super.apply(this, arguments);

    _this.onSearch = function (e) {
      var onSearch = _this.props.onSearch;

      if (onSearch) {
        onSearch(_this.input.input.value, e);
      }

      _this.input.focus();
    };

    _this.saveInput = function (node) {
      _this.input = node;
    };

    return _this;
  }

  (0, _createClass2["default"])(Search, [{
    key: "focus",
    value: function focus() {
      this.input.focus();
    }
  }, {
    key: "blur",
    value: function blur() {
      this.input.blur();
    }
  }, {
    key: "getPrefixCls",
    value: function getPrefixCls() {
      var prefixCls = this.props.prefixCls;
      var getPrefixCls = this.context.getPrefixCls;
      return getPrefixCls('input-search', prefixCls);
    }
  }, {
    key: "getButtonOrIcon",
    value: function getButtonOrIcon(prefixCls) {
      var _this2 = this;

      var _this$props = this.props,
          enterButton = _this$props.enterButton,
          size = _this$props.size,
          disabled = _this$props.disabled,
          buttonProps = _this$props.buttonProps;

      if (!enterButton) {
        return /*#__PURE__*/_react["default"].createElement(_icon["default"], {
          className: "".concat(prefixCls, "-icon"),
          type: "search",
          key: "searchIcon",
          onClick: this.onSearch
        });
      }

      var enterButtonAsElement = enterButton;
      var isButton = enterButtonAsElement.type && (enterButtonAsElement.type.__C7N_BUTTON === true || enterButtonAsElement.type.__PRO_BUTTON === true || enterButtonAsElement.type.__ANT_BUTTON === true);

      if (isButton || enterButtonAsElement.type === 'button') {
        return /*#__PURE__*/(0, _react.cloneElement)(enterButtonAsElement, isButton ? {
          className: "".concat(prefixCls, "-button"),
          size: size,
          onClick: function onClick(e) {
            if (enterButtonAsElement) {
              var props = enterButtonAsElement.props;

              if (props && props.onClick) {
                props.onClick(e);
              }
            }

            _this2.onSearch(e);
          }
        } : {
          onClick: this.onSearch
        });
      }

      return /*#__PURE__*/_react["default"].createElement(_button["default"], (0, _extends2["default"])({}, buttonProps, {
        className: "".concat(prefixCls, "-button"),
        type: "primary",
        size: size,
        disabled: disabled,
        key: "enterButton",
        icon: enterButton === true || !enterButton ? 'search' : undefined,
        onClick: this.onSearch
      }), enterButton === true ? undefined : enterButton);
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props2 = this.props,
          className = _this$props2.className,
          inputPrefixCls = _this$props2.inputPrefixCls,
          size = _this$props2.size,
          suffix = _this$props2.suffix,
          enterButton = _this$props2.enterButton,
          others = (0, _objectWithoutProperties2["default"])(_this$props2, _excluded);
      var getPrefixCls = this.context.getPrefixCls;
      var prefixCls = this.getPrefixCls();
      delete others.onSearch;
      delete others.prefixCls;
      var buttonOrIcon = this.getButtonOrIcon(prefixCls);
      var searchSuffix = suffix ? [suffix, buttonOrIcon] : buttonOrIcon;
      var inputClassName = (0, _classnames["default"])(prefixCls, className, (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-enter-button"), !!enterButton), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-").concat(size), !!size), _classNames));
      return /*#__PURE__*/_react["default"].createElement(_Input["default"], (0, _extends2["default"])({
        onPressEnter: this.onSearch
      }, others, {
        size: size,
        className: inputClassName,
        prefixCls: getPrefixCls('input', inputPrefixCls),
        suffix: searchSuffix,
        ref: this.saveInput
      }));
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _ConfigContext["default"];
    }
  }]);
  return Search;
}(_react.Component);

exports["default"] = Search;
Search.displayName = 'Search';
Search.defaultProps = {
  enterButton: true,
  size: _enum.Size.small
};
//# sourceMappingURL=Search.js.map
