Repository URL to install this package:
|
Version:
3.12.16 ▾
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import { memo, useCallback, useEffect, useRef } from 'react';
import { ViewportList } from 'react-viewport-list';
import getScrollableParent from '@filerobot/explorer/lib/utils/getScrollableParent';
import VirtualListItem from './VirtualListItem';
/**
* A Virtual list that applies (windowing) concept for reusing the DOM elements and avoid rerendering for all items.
* @param {Array} items - The items that will be used in the list (required).
* @param {ReactNode} children - The children that will be rendered for each item (required).
* @param {String} injectedItemPropName - The name of the prop that will be injected in the children (optional).
* @param {Number} overscan - The number of items that will be rendered before and after the viewport (optional).
* @param {Function} getItemKey - The function that will be used to get the item key (optional).
* @param {Function} renderSpacer - The function that will be used to render the spacer (optional).
* @param {ReactRef} parentRef - the reference for the parent wrapper for the virtual list to be used in recognized the scroller/viewport element, not required if the virtual list is the parent and u don't need to have the apiRef (optional)
* @param {ReactRef} apiRef - the reference that will contain the api functions to deal with the grid (optional)
* 1- `scrollToItem({ key, index })` function that scrolls the grid to the specified item's key, the key should be the same property that's returned from getItemKey, index should be the item's index.
* @returns {ReactNode} - The VirtualList component.
*/
import { jsx as _jsx } from "react/jsx-runtime";
var defaultGetItemKey = function defaultGetItemKey(item, index) {
return (_typeof(item) === 'object' ? item.uuid || item.id : item) || index;
};
var VirtualList = function VirtualList(_ref) {
var children = _ref.children,
_ref$items = _ref.items,
items = _ref$items === void 0 ? [] : _ref$items,
_ref$injectedItemProp = _ref.injectedItemPropName,
injectedItemPropName = _ref$injectedItemProp === void 0 ? 'item' : _ref$injectedItemProp,
_ref$overscan = _ref.overscan,
overscan = _ref$overscan === void 0 ? 1 : _ref$overscan,
apiRef = _ref.apiRef,
renderSpacer = _ref.renderSpacer,
_ref$getItemKey = _ref.getItemKey,
getItemKey = _ref$getItemKey === void 0 ? defaultGetItemKey : _ref$getItemKey,
_ref$parentRef = _ref.parentRef,
parentRef = _ref$parentRef === void 0 ? null : _ref$parentRef;
var scrollableElemRef = useRef();
var listRef = useRef();
var scrollToItem = useCallback(function (_ref2) {
var key = _ref2.key,
index = _ref2.index;
if ((key || index) && items.length > 0) {
var itemIndex = index || items.findIndex(function (item) {
return getItemKey(item) === key;
});
if (itemIndex > -1 && listRef.current) {
listRef.current.scrollToIndex({
index: itemIndex
});
}
}
}, [items]);
useEffect(function () {
if (parentRef !== null && parentRef !== void 0 && parentRef.current) {
scrollableElemRef.current = getScrollableParent(parentRef.current);
}
}, []);
useEffect(function () {
if (apiRef && parentRef !== null && parentRef !== void 0 && parentRef.current) {
apiRef.current = _objectSpread(_objectSpread({}, apiRef.current), {}, {
scrollToItem: scrollToItem
});
}
}, [scrollToItem]);
var renderItem = useCallback(function (item, index) {
return /*#__PURE__*/_jsx(VirtualListItem, {
item: item,
index: index,
itemPropName: injectedItemPropName,
children: children
}, getItemKey(item, index));
}, [items, children]);
return /*#__PURE__*/_jsx(ViewportList, {
items: items,
overscan: overscan,
renderSpacer: renderSpacer,
ref: listRef,
viewportRef: parentRef !== null && parentRef !== void 0 && parentRef.current ? scrollableElemRef : undefined,
children: renderItem
});
};
export default /*#__PURE__*/memo(VirtualList);