UNPKG

@spaced-out/ui-design-system

Version:
109 lines (108 loc) 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InfinitePagination = void 0; var React = _interopRequireWildcard(require("react")); var _reactWindow = require("react-window"); var _hooks = require("../../hooks"); var _space = require("../../styles/variables/_space"); var _classify = _interopRequireDefault(require("../../utils/classify")); var _qa = require("../../utils/qa"); var _CircularLoader = require("../CircularLoader"); var _InfinitePaginationModule = _interopRequireDefault(require("./InfinitePagination.module.css")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } const DEFAULT_THRESHOLD = 200; const InfinitePagination = exports.InfinitePagination = /*#__PURE__*/React.forwardRef((props, ref) => { const { items, width = _space.spaceFluid, itemSize, renderItem, threshold = DEFAULT_THRESHOLD, getItemKey, classNames, hasNextPage, loadMoreItems, isVariableSizeList, testId } = props; const listRef = React.useRef(null); const itemsLength = items.length; const totalItemsCount = itemsLength + (hasNextPage ? 1 : 0); const [containerRef, containerHeight] = (0, _hooks.useResizeObserver)(); React.useImperativeHandle(ref, () => listRef.current); React.useEffect(() => { if (listRef.current) { listRef.current.scrollToItem(0); } }, [itemsLength === 0]); const handleScroll = (0, _hooks.useInfiniteScroll)({ itemSize, threshold, itemsLength, hasNextPage, loadMoreItems, containerHeight, isVariableSizeList }); const Row = _ref => { let { index, style } = _ref; return index === itemsLength ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { style: style, className: _InfinitePaginationModule.default.loader, "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'loader' }), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_CircularLoader.CircularLoader, { size: "large", testId: (0, _qa.generateTestId)({ base: testId, slot: 'loader-icon' }) }) }) : /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { style: style, className: _InfinitePaginationModule.default.listRow, "data-testid": (0, _qa.generateTestId)({ base: testId, slot: `item-${index}` }), children: renderItem(items[index], index) }); }; const itemKey = React.useCallback(index => index === itemsLength ? 'loading-indicator' : getItemKey ? getItemKey(items[index], index) : index, [items, getItemKey]); return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { ref: containerRef, "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'root' }), className: (0, _classify.default)(_InfinitePaginationModule.default.wrapper, classNames?.wrapper), children: isVariableSizeList ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactWindow.VariableSizeList, { ref: listRef, width: width, height: containerHeight, itemKey: itemKey, itemSize: itemSize, onScroll: handleScroll, itemCount: totalItemsCount, children: Row }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactWindow.FixedSizeList, { ref: listRef, width: width, height: containerHeight, itemKey: itemKey, itemSize: itemSize, onScroll: handleScroll, itemCount: totalItemsCount, children: Row }) }); });