UNPKG

@livelybone/react-query-list

Version:

A component of React for query list, includes pagination

330 lines (282 loc) 9.55 kB
/** * Bundle of @livelybone/react-query-list * Generated: 2020-07-27 * Version: 1.2.1 * License: MIT * Author: 2631541504@qq.com */ import React, { PureComponent } from 'react'; import ReactPagination from '@livelybone/react-pagination'; import ReactLoading from '@auraxy/react-loading'; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread2(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function isPaginationPropsDiff(props, $props) { if (!props && !$props) return false; if (!props || !$props) return true; return ['pageSize', 'pageIndex', 'pageCount', 'currentPageSize', 'maxPageBtn', 'debounceTime'].some(function (key) { return props[key] !== $props[key]; }); } var DefaultPaginationProps = { pageSize: 10, pageIndex: 1 }; var ReactQueryList = /*#__PURE__*/function (_PureComponent) { _inherits(ReactQueryList, _PureComponent); var _super = _createSuper(ReactQueryList); function ReactQueryList(props) { var _this; _classCallCheck(this, ReactQueryList); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "paginationRef", void 0); _this.state = { $paginationProps: _objectSpread2(_objectSpread2({}, DefaultPaginationProps), props.paginationProps), loading: false, error: '', list: [] }; return _this; } _createClass(ReactQueryList, [{ key: "query", value: function query(reset) { var _this2 = this; /** * 当请求处理时间 < 200ms 时不显示 loading * loading 显示时间至少为 500ms,不然看起来像闪屏 * * Loading component should not displayed when the request processing time is < 200ms * Loading component should displayed at least 500ms, otherwise it will look like a flash screen * */ var pro; var timer = setTimeout(function () { _this2.setState({ loading: true }); pro = new Promise(function (res) { return setTimeout(res, 500); }); }, 200); if (reset) this.paginationRef.setPageNumber(1, false); return Promise.resolve().then(function () { var _this2$paginationRef = _this2.paginationRef, $currentPageNumber = _this2$paginationRef.state.$currentPageNumber, pageSize = _this2$paginationRef.props.pageSize; return Promise.resolve(_this2.props.onQuery({ pageIndex: +$currentPageNumber, pageSize: pageSize })).then(function (_ref) { var pageCount = _ref.pageCount, list = _ref.list; _this2.setState(function (pre) { return { $paginationProps: _objectSpread2(_objectSpread2({}, pre.$paginationProps), {}, { pageCount: pageCount, currentPageSize: list.length }), list: list, error: '' }; }); })["catch"](function (error) { _this2.setState({ error: error }); _this2.props.onError && _this2.props.onError(error); }).then(function () { clearTimeout(timer); Promise.resolve(pro).then(function () { _this2.setState({ loading: false }); }); }); }); } }, { key: "componentDidMount", value: function componentDidMount() { if (this.props.queryAtMounted) this.query(); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps, prevState, snapshot) { var _this3 = this; if (isPaginationPropsDiff(prevProps.paginationProps, this.props.paginationProps)) { this.setState(function (pre) { return { $paginationProps: _objectSpread2(_objectSpread2({}, pre.$paginationProps), _this3.props.paginationProps) }; }); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.setState = function () {}; } }, { key: "render", value: function render() { var _this4 = this; var _this$props = this.props, children = _this$props.children, className = _this$props.className, loadingComp = _this$props.loadingComp; var _this$state = this.state, $paginationProps = _this$state.$paginationProps, loading = _this$state.loading, list = _this$state.list, error = _this$state.error; var $children = // @ts-ignore typeof children === 'function' ? children(list, error) : children; return /*#__PURE__*/React.createElement("div", { className: "react-query-list".concat(className ? " ".concat(className) : '') }, loading && /*#__PURE__*/React.createElement("div", { className: "loading-wrapper" }, !!loadingComp ? loadingComp : /*#__PURE__*/React.createElement(ReactLoading, null)), $children, /*#__PURE__*/React.createElement(ReactPagination, Object.assign({}, $paginationProps, { ref: function ref(_ref2) { return _this4.paginationRef = _ref2; }, onPageChange: function onPageChange() { return _this4.query(); } }))); } }]); return ReactQueryList; }(PureComponent); export default ReactQueryList;