UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

206 lines (181 loc) 5.98 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _createSuper from "@babel/runtime/helpers/createSuper"; import { __decorate } from "tslib"; import React, { Component } from 'react'; import { observer } from 'mobx-react'; import { action, observable } from 'mobx'; import { pxToRem } from '../../../es/_util/UnitConvertor'; import measureScrollbar from '../../../es/_util/measureScrollbar'; import IconItem from './IconItem'; import Pagination from '../pagination/Pagination'; import autobind from '../_util/autobind'; var IconCategory = /*#__PURE__*/function (_Component) { _inherits(IconCategory, _Component); var _super = _createSuper(IconCategory); function IconCategory(props) { var _this; _classCallCheck(this, IconCategory); _this = _super.call(this, props); _this.setPage(props.page); return _this; } _createClass(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.createElement(Pagination, { key: "page", className: "".concat(prefixCls, "-pagination"), total: total, page: page, pageSize: pageSize, showSizeChanger: false, onChange: this.handlePageChange, style: { right: pxToRem(measureScrollbar(), 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.createElement(IconItem, { 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.createElement("div", { className: "".concat(prefixCls, "-category") }, this.renderPagination(), /*#__PURE__*/React.createElement("ul", { key: "icon-items", ref: this.saveRef }, this.renderIcons())); } }]); return IconCategory; }(Component); IconCategory.displayName = 'IconCategory'; IconCategory.defaultProps = { paging: true }; __decorate([observable], IconCategory.prototype, "page", void 0); __decorate([autobind], IconCategory.prototype, "saveRef", null); __decorate([autobind], IconCategory.prototype, "handlePageChange", null); __decorate([autobind], IconCategory.prototype, "handleItemSelect", null); __decorate([action], IconCategory.prototype, "setPage", null); IconCategory = __decorate([observer], IconCategory); export default IconCategory; //# sourceMappingURL=IconCategory.js.map