"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransferSearch = void 0;
var tslib_1 = require("tslib");
/**
 * Tranasfer搜索
 */
var react_1 = (0, tslib_1.__importDefault)(require("react"));
var lodash_1 = require("lodash");
var theme_1 = require("../theme");
var icons_1 = require("./icons");
var helper_1 = require("../utils/helper");
var locale_1 = require("../locale");
var InputBox_1 = (0, tslib_1.__importDefault)(require("./InputBox"));
var TransferSearch = /** @class */ (function (_super) {
    (0, tslib_1.__extends)(TransferSearch, _super);
    function TransferSearch() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            inputValue: ''
        };
        _this.lazySearch = (0, lodash_1.debounce)(function () {
            var inputValue = _this.state.inputValue;
            // 防止由于防抖导致空值问题
            if (!inputValue) {
                return;
            }
            var onSearch = _this.props.onSearch;
            onSearch(inputValue);
        }, 250, { trailing: true, leading: false });
        return _this;
    }
    TransferSearch.itemRender = function (option) {
        return react_1.default.createElement("span", null, "".concat(option.scopeLabel || '').concat(option.label));
    };
    TransferSearch.prototype.componentWillUnmount = function () {
        this.lazySearch.cancel();
    };
    TransferSearch.prototype.handleSearch = function (inputValue) {
        var _this = this;
        // text 有值的时候，走搜索否则直接走 handleSeachCancel ，等同于右侧的 clear 按钮
        this.setState({ inputValue: inputValue }, function () {
            if (inputValue) {
                // 如果有取消搜索，先取消掉。
                _this.cancelSearch && _this.cancelSearch();
                _this.lazySearch();
            }
            else {
                _this.handleSeachCancel();
            }
        });
    };
    TransferSearch.prototype.handleSearchKeyDown = function (e) {
        if (e.key === 'Enter') {
            e.preventDefault();
        }
    };
    TransferSearch.prototype.handleSeachCancel = function () {
        var _a, _b;
        (_b = (_a = this.props).onCancelSearch) === null || _b === void 0 ? void 0 : _b.call(_a);
        this.setState({
            inputValue: ''
        });
    };
    TransferSearch.prototype.render = function () {
        var _a = this.props, cx = _a.classnames, __ = _a.translate, _b = _a.placeholder, placeholder = _b === void 0 ? __('Transfer.searchKeyword') : _b;
        var inputValue = this.state.inputValue;
        return (react_1.default.createElement("div", { className: cx('Transfer-search') },
            react_1.default.createElement(InputBox_1.default, { value: inputValue, onChange: this.handleSearch, clearable: false, onKeyDown: this.handleSearchKeyDown, placeholder: placeholder }, !!inputValue ? (react_1.default.createElement("a", { onClick: this.handleSeachCancel },
                react_1.default.createElement(icons_1.Icon, { icon: "close", className: "icon" }))) : (react_1.default.createElement(icons_1.Icon, { icon: "search", className: "icon" })))));
    };
    var _a;
    TransferSearch.defaultProps = {
        placeholder: 'placeholder.selectData'
    };
    (0, tslib_1.__decorate)([
        helper_1.autobind,
        (0, tslib_1.__metadata)("design:type", Function),
        (0, tslib_1.__metadata)("design:paramtypes", [String]),
        (0, tslib_1.__metadata)("design:returntype", void 0)
    ], TransferSearch.prototype, "handleSearch", null);
    (0, tslib_1.__decorate)([
        helper_1.autobind,
        (0, tslib_1.__metadata)("design:type", Function),
        (0, tslib_1.__metadata)("design:paramtypes", [typeof (_a = typeof react_1.default !== "undefined" && react_1.default.KeyboardEvent) === "function" ? _a : Object]),
        (0, tslib_1.__metadata)("design:returntype", void 0)
    ], TransferSearch.prototype, "handleSearchKeyDown", null);
    (0, tslib_1.__decorate)([
        helper_1.autobind,
        (0, tslib_1.__metadata)("design:type", Function),
        (0, tslib_1.__metadata)("design:paramtypes", []),
        (0, tslib_1.__metadata)("design:returntype", void 0)
    ], TransferSearch.prototype, "handleSeachCancel", null);
    return TransferSearch;
}(react_1.default.Component));
exports.TransferSearch = TransferSearch;
exports.default = (0, theme_1.themeable)((0, locale_1.localeable)(TransferSearch));
//# sourceMappingURL=./components/TransferSearch.js.map
