"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 _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 _tslib = require("tslib");

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

var _mobxReact = require("mobx-react");

var _mobx = require("mobx");

var _UnitConvertor = require("../../../lib/_util/UnitConvertor");

var _measureScrollbar = _interopRequireDefault(require("../../../lib/_util/measureScrollbar"));

var _IconItem = _interopRequireDefault(require("./IconItem"));

var _Pagination = _interopRequireDefault(require("../pagination/Pagination"));

var _autobind = _interopRequireDefault(require("../_util/autobind"));

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

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

  function IconCategory(props) {
    var _this;

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

    _this.setPage(props.page);

    return _this;
  }

  (0, _createClass2["default"])(IconCategory, [{
    key: "saveRef",
    value: function saveRef(node) {
      this.ul = node;
    }
  }, {
    key: "handlePageChange",
    value: function handlePageChange(page) {
      this.setPage(page);
      var _this$props = this.props,
          onPageChange = _this$props.onPageChange,
          category = _this$props.category;

      if (onPageChange) {
        onPageChange(page, category);
      }
    }
  }, {
    key: "handleItemSelect",
    value: function handleItemSelect(icon) {
      var onSelect = this.props.onSelect;
      onSelect(icon);
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      if (nextProps.page && nextProps.page !== this.page) {
        this.setPage(nextProps.page);
      }
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      this.syncItemPosition();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      this.syncItemPosition();
    }
  }, {
    key: "syncItemPosition",
    value: function syncItemPosition() {
      var _this$props2 = this.props,
          value = _this$props2.value,
          prefixCls = _this$props2.prefixCls,
          ul = this.ul;

      if (value && ul) {
        var item = ul.querySelector("li.".concat(prefixCls, "-item-selected"));

        if (item) {
          var offsetHeight = ul.offsetHeight,
              scrollTop = ul.scrollTop;
          var offsetTop = item.offsetTop,
              height = item.offsetHeight;

          if (offsetTop < scrollTop) {
            if (ul.scrollTo) {
              ul.scrollTo(0, offsetTop);
            } else {
              ul.scrollTop = offsetTop;
            }
          } else if (offsetTop + height > scrollTop + offsetHeight) {
            if (ul.scrollTo) {
              ul.scrollTo(0, offsetTop + height - offsetHeight);
            } else {
              ul.scrollTop = offsetTop + height - offsetHeight;
            }
          }
        }
      }
    }
  }, {
    key: "setPage",
    value: function setPage() {
      var page = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
      this.page = page;
    }
  }, {
    key: "renderPagination",
    value: function renderPagination() {
      var page = this.page,
          _this$props3 = this.props,
          paging = _this$props3.paging,
          pageSize = _this$props3.pageSize,
          prefixCls = _this$props3.prefixCls,
          icons = _this$props3.icons;
      var total = icons.length;

      if (paging && total > pageSize) {
        return /*#__PURE__*/_react["default"].createElement(_Pagination["default"], {
          key: "page",
          className: "".concat(prefixCls, "-pagination"),
          total: total,
          page: page,
          pageSize: pageSize,
          showSizeChanger: false,
          onChange: this.handlePageChange,
          style: {
            right: (0, _UnitConvertor.pxToRem)((0, _measureScrollbar["default"])(), true)
          }
        });
      }
    }
  }, {
    key: "renderIcons",
    value: function renderIcons() {
      var _this2 = this;

      var _this$props4 = this.props,
          value = _this$props4.value,
          prefixCls = _this$props4.prefixCls,
          customFontName = _this$props4.customFontName;
      return this.getIcons().map(function (icon) {
        return /*#__PURE__*/_react["default"].createElement(_IconItem["default"], {
          key: icon,
          customFontName: customFontName,
          prefixCls: prefixCls,
          type: icon,
          onSelect: _this2.handleItemSelect,
          active: value === icon
        });
      });
    }
  }, {
    key: "getIcons",
    value: function getIcons() {
      var page = this.page,
          _this$props5 = this.props,
          paging = _this$props5.paging,
          pageSize = _this$props5.pageSize,
          icons = _this$props5.icons;

      if (paging && icons.length > pageSize) {
        return icons.slice((page - 1) * pageSize, page * pageSize);
      }

      return icons;
    }
  }, {
    key: "render",
    value: function render() {
      var prefixCls = this.props.prefixCls;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-category")
      }, this.renderPagination(), /*#__PURE__*/_react["default"].createElement("ul", {
        key: "icon-items",
        ref: this.saveRef
      }, this.renderIcons()));
    }
  }]);
  return IconCategory;
}(_react.Component);

IconCategory.displayName = 'IconCategory';
IconCategory.defaultProps = {
  paging: true
};
(0, _tslib.__decorate)([_mobx.observable], IconCategory.prototype, "page", void 0);
(0, _tslib.__decorate)([_autobind["default"]], IconCategory.prototype, "saveRef", null);
(0, _tslib.__decorate)([_autobind["default"]], IconCategory.prototype, "handlePageChange", null);
(0, _tslib.__decorate)([_autobind["default"]], IconCategory.prototype, "handleItemSelect", null);
(0, _tslib.__decorate)([_mobx.action], IconCategory.prototype, "setPage", null);
IconCategory = (0, _tslib.__decorate)([_mobxReact.observer], IconCategory);
var _default = IconCategory;
exports["default"] = _default;
//# sourceMappingURL=IconCategory.js.map
