"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
var _react = _interopRequireWildcard(require("react"));
var React = _react;
var _search = _interopRequireDefault(require("../../assets/icon/search/search.png"));
var _delete = _interopRequireDefault(require("../../assets/icon/search/delete.png"));
var _childrenNodeHandle = require("../../../src/utils/children-node-handle");
var _excluded = ["onSearch", "searchText", "autofocus", "placeholder", "onConfirm", "children", "iconSrc"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var SearchBar = function SearchBar(props) {
  var onSearch = props.onSearch,
    _props$searchText = props.searchText,
    searchText = _props$searchText === void 0 ? true : _props$searchText,
    _props$autofocus = props.autofocus,
    autofocus = _props$autofocus === void 0 ? false : _props$autofocus,
    _props$placeholder = props.placeholder,
    placeholder = _props$placeholder === void 0 ? '请输入' : _props$placeholder,
    onConfirm = props.onConfirm,
    children = props.children,
    iconSrc = props.iconSrc,
    otherProps = (0, _objectWithoutPropertiesLoose2["default"])(props, _excluded);
  var _useState = (0, _react.useState)(),
    searchValue = _useState[0],
    setSearchValue = _useState[1];
  var searchInput = (0, _react.useRef)(null);
  var handleChange = function handleChange(e) {
    setSearchValue(e.target.value); // 更新 state
  };
  /**
   * 触发confirm事件
   */
  var confirm = function confirm() {
    var _searchInput$current;
    if (onConfirm) {
      onConfirm(searchValue);
    }
    searchInput === null || searchInput === void 0 ? void 0 : (_searchInput$current = searchInput.current) === null || _searchInput$current === void 0 ? void 0 : _searchInput$current.blur();
  };
  var handleKeyDown = function handleKeyDown(e) {
    if (e.key === 'Enter') {
      confirm();
    }
  };
  var clearValue = function clearValue() {
    setSearchValue('');
  };
  return /*#__PURE__*/React.createElement("form", (0, _extends2["default"])({
    action: "",
    className: "search-bar-form"
  }, otherProps, {
    onSubmit: confirm
  }), /*#__PURE__*/React.createElement("div", {
    className: "search-bar",
    onClick: onSearch
  }, /*#__PURE__*/React.createElement("span", {
    className: "search-bar-icon"
  }, /*#__PURE__*/React.createElement("img", {
    src: _search["default"],
    className: "search-bar-icon-img"
  })), /*#__PURE__*/React.createElement("input", {
    className: "search-bar-input",
    placeholder: placeholder,
    value: searchValue,
    autoFocus: autofocus,
    onChange: handleChange,
    onKeyDown: handleKeyDown,
    ref: searchInput
  }), searchValue ? /*#__PURE__*/React.createElement("img", {
    className: "search-bar-delete-img",
    src: _delete["default"],
    onClick: clearValue
  }) : '', searchText ? /*#__PURE__*/React.createElement("span", {
    className: "search-bar-enter",
    onClick: confirm
  }, "\u641C\u7D22") : '', iconSrc && (0, _childrenNodeHandle.convertChildren)(children, 'img-right', {
    imgSrc: iconSrc,
    className: 'search-bar-right-icon'
  })));
};
var _default = exports["default"] = SearchBar;