@aliretail/react-materials-components
Version:
147 lines (113 loc) • 6.33 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.defaultPageSizeList = exports["default"] = void 0;
var _pagination = _interopRequireDefault(require("@alifd/next/lib/pagination"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var R = _interopRequireWildcard(require("ramda"));
var _TableContext = require("../../TableContext");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
var defaultPageSizeList = [20, 50, 100];
exports.defaultPageSizeList = defaultPageSizeList;
var AliretailPagination = /*#__PURE__*/function (_React$Component) {
(0, _inheritsLoose2["default"])(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 = (0, _classnames["default"])((_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["default"], (0, _extends2["default"])({
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(_TableContext.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 = (0, _extends2["default"])({
pageSizeList: pageSizeList,
pageSize: pageSizeList[0],
language: filterProps.language,
tableEleId: context.tableState.containerId
}, contextPaginationProps, props);
return /*#__PURE__*/React.createElement(Comp, paginationProps);
});
};
};
var _default = withContextPagination(i18nAliretailPagination);
exports["default"] = _default;