@aliretail/react-materials-components
Version:
127 lines (105 loc) • 4.65 kB
JavaScript
import _Pagination from "@alifd/next/es/pagination";
import _extends from "@babel/runtime/helpers/extends";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import * as React from 'react';
import cx from 'classnames';
import * as R from 'ramda';
import { TableConsumer } from "../../TableContext";
export var defaultPageSizeList = [20, 50, 100];
var AliretailPagination = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(AliretailPagination, _React$Component);
function AliretailPagination() {
var _this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
_this.state = {
tableXElement: {}
};
return _this;
}
var _proto = AliretailPagination.prototype;
_proto.componentDidMount = function componentDidMount() {
var tableXElement = document.getElementById(this.props.tableEleId);
this.setState(function () {
return {
tableXElement: tableXElement
};
});
};
_proto.render = function render() {
var _cx,
_this2 = this;
var _this$props = this.props,
className = _this$props.className,
pageSize = _this$props.pageSize,
total = _this$props.total,
current = _this$props.current,
onChange = _this$props.onChange,
onPageSizeChange = _this$props.onPageSizeChange; // 追加 class 到默认的 pagination 上
var classes = cx((_cx = {
'table-with-pagination': true,
'aliretail-table-pagination': true
}, _cx[className] = !!className, _cx)); // 获取非特定的 props 追加到 pagination 上
var otherProps = R.omit(['className', 'pageSize', 'total', 'current', 'onChange', 'onPageSizeChange', 'tableEleId', 'i18nBundle'], this.props);
var scrollToTableHead = function scrollToTableHead() {
var tableXElement = _this2.state.tableXElement;
if (tableXElement) {
tableXElement.scrollIntoView();
}
};
var handlePageSizeChange = function handlePageSizeChange(value) {
scrollToTableHead();
onPageSizeChange(value);
};
var handleChange = function handleChange(value, ctx) {
scrollToTableHead();
onChange(value, ctx);
};
return /*#__PURE__*/React.createElement(_Pagination, _extends({
key: "pagination-for-table",
"data-name": "AliretailTablePagination",
className: classes,
pageSizeSelector: "dropdown",
pageSizePosition: "end",
popupProps: {
align: 'bl tl'
},
shape: "arrow-only",
pageSize: pageSize,
total: total,
totalRender: function totalRender(number) {
return /*#__PURE__*/React.createElement("span", {
className: "total"
}, "\u5171", /*#__PURE__*/React.createElement("b", null, number), number > 1 ? '项' : '项');
},
current: current,
onChange: handleChange,
onPageSizeChange: handlePageSizeChange
}, otherProps));
};
return AliretailPagination;
}(React.Component);
var i18nAliretailPagination = AliretailPagination; // 接受全部 paginationProps 的属性传入
var withContextPagination = function withContextPagination(Comp) {
return function (props) {
// props 为组件直接传入的 props 一般不需要使用此 props
return /*#__PURE__*/React.createElement(TableConsumer, null, function (context) {
var _props$paginationProp;
var filterProps = R.pickAll(['paginationProps', 'language'], context);
var _filterProps$paginati = filterProps.paginationProps,
contextPaginationProps = _filterProps$paginati === void 0 ? {} : _filterProps$paginati;
var pageSizeList = (props === null || props === void 0 ? void 0 : (_props$paginationProp = props.paginationProps) === null || _props$paginationProp === void 0 ? void 0 : _props$paginationProp.pageSizeList) || (contextPaginationProps === null || contextPaginationProps === void 0 ? void 0 : contextPaginationProps.pageSizeList) || defaultPageSizeList; // 获取 paginationProps 后将其分解为一级 props
// TODO: 常用 props,例如 total 考虑分解到一级 props
var paginationProps = _extends({
pageSizeList: pageSizeList,
pageSize: pageSizeList[0],
language: filterProps.language,
tableEleId: context.tableState.containerId
}, contextPaginationProps, props);
return /*#__PURE__*/React.createElement(Comp, paginationProps);
});
};
};
export default withContextPagination(i18nAliretailPagination);