"use strict";

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

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

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

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

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));

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 _omit = _interopRequireDefault(require("lodash/omit"));

var _choerodonUiFont = require("choerodon-ui-font");

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

var _select = _interopRequireDefault(require("../select"));

var _pagination = _interopRequireDefault(require("../pagination"));

var _tooltip = _interopRequireDefault(require("../tooltip"));

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

var Option = _select["default"].Option;

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

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

  function IconSelect(props, context) {
    var _this;

    (0, _classCallCheck2["default"])(this, IconSelect);
    _this = _super.call(this, props, context);

    _this.handleRender = function (label) {
      if (typeof label === 'string' && label) {
        return /*#__PURE__*/_react["default"].createElement("span", null, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
          type: label
        }), " ", label);
      }

      if ((0, _typeof2["default"])(label) === 'object' && label.props) {
        var children = label.props.children;
        return children ? /*#__PURE__*/_react["default"].createElement("span", null, children) : null;
      }

      return null;
    };

    _this.handlePageChange = function (current, pageSize) {
      var filterValue = _this.state.filterValue;

      _this.initIcon(current, pageSize, filterValue);
    };

    _this.handleFilter = function (value) {
      _this.initIcon(1, 20, value);
    };

    _this.saveRef = function (node) {
      if (node) {
        _this.rcSelect = node;
      }
    };

    _this.state = {
      current: 1,
      total: 0,
      pageSize: 20,
      filterValue: '',
      data: []
    };
    return _this;
  }

  (0, _createClass2["default"])(IconSelect, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.initIcon();
    }
  }, {
    key: "initIcon",
    value: function initIcon() {
      var current = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
      var pageSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 20;
      var filterValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
      var showAll = this.props.showAll;
      var minIndex = (current - 1) * pageSize;
      var maxIndex = current * pageSize;
      var items;

      if (showAll) {
        items = _choerodonUiFont.icons["default"];

        if (filterValue) {
          items = _choerodonUiFont.icons.favorite.filter(function (name) {
            return name.toLowerCase().indexOf(filterValue.toLowerCase()) !== -1;
          });
        }
      } else {
        items = _choerodonUiFont.icons.favorite;

        if (filterValue) {
          items = _choerodonUiFont.icons.favorite.filter(function (name) {
            return name.toLowerCase().indexOf(filterValue.toLowerCase()) !== -1;
          });
        }
      }

      var total = items.length || 0;
      var currentData = items.filter(function (name, index) {
        return name && index >= minIndex && index < maxIndex;
      });
      this.setState({
        data: currentData,
        total: total,
        pageSize: pageSize,
        current: current,
        filterValue: filterValue
      });
    }
  }, {
    key: "renderOption",
    value: function renderOption() {
      var data = this.state.data;
      return data.map(function (icon) {
        return /*#__PURE__*/_react["default"].createElement(Option, {
          key: icon,
          value: icon
        }, /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
          placement: "bottomLeft",
          title: icon
        }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
          type: icon
        }), /*#__PURE__*/_react["default"].createElement("span", {
          className: "text"
        }, icon)));
      });
    }
  }, {
    key: "renderFooter",
    value: function renderFooter() {
      var _this$state = this.state,
          total = _this$state.total,
          pageSize = _this$state.pageSize,
          current = _this$state.current;
      return /*#__PURE__*/_react["default"].createElement(_pagination["default"], {
        total: total,
        onChange: this.handlePageChange,
        pageSizeOptions: ['20', '40', '80'],
        pageSize: pageSize,
        onShowSizeChange: this.handlePageChange,
        current: current
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          className = _this$props.className,
          customizePrefixCls = _this$props.prefixCls,
          dropdownClassName = _this$props.dropdownClassName;
      var filterValue = this.state.filterValue;
      var getPrefixCls = this.context.getPrefixCls;
      var prefixCls = getPrefixCls('icon-select', customizePrefixCls);
      var selectCls = (0, _classnames["default"])(className, prefixCls);
      var dropDownCls = (0, _classnames["default"])(dropdownClassName, "".concat(prefixCls, "-dropdown"));
      var selectProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, this.props), {}, {
        className: selectCls,
        dropdownClassName: dropDownCls
      });
      var otherProps = (0, _omit["default"])(selectProps, ['prefixCls']);
      return /*#__PURE__*/_react["default"].createElement(_select["default"], (0, _extends2["default"])({}, otherProps, {
        footer: this.renderFooter(),
        onFilterChange: this.handleFilter,
        filterValue: filterValue,
        choiceRender: this.handleRender,
        filter: true,
        ref: this.saveRef
      }), this.renderOption());
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _ConfigContext["default"];
    }
  }]);
  return IconSelect;
}(_react.Component);

exports["default"] = IconSelect;
IconSelect.displayName = 'IconSelect';
IconSelect.defaultProps = {
  filter: true,
  showArrow: false,
  showCheckAll: false,
  showAll: false
};
//# sourceMappingURL=IconSelect.js.map
