Socket
Socket
Sign inDemoInstall

rc-virtual-list

Package Overview
Dependencies
Maintainers
1
Versions
126
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rc-virtual-list - npm Package Compare versions

Comparing version 0.0.0-alpha.7 to 0.0.0-alpha.8

15

es/List.d.ts

@@ -22,5 +22,8 @@ import * as React from 'react';

disabled?: boolean;
/** When `disabled`, trigger if changed item not render. */
onSkipRender?: () => void;
}
declare type Status = 'NONE' | 'MEASURE_START' | 'MEASURE_DONE' | 'SWITCH_TO_VIRTUAL' | 'SWITCH_TO_RAW';
interface ListState<T> {
status: 'NONE' | 'MEASURE_START' | 'MEASURE_DONE';
status: Status;
scrollTop: number | null;

@@ -39,2 +42,10 @@ /** Located item index */

startItemTop: number;
/**
* Tell if is using virtual scroll
*/
isVirtual: boolean;
/**
* Only used when turn virtual list to raw list
*/
cacheScroll?: RelativeScroll;
}

@@ -67,3 +78,2 @@ /**

};
state: ListState<T>;
listRef: React.RefObject<HTMLElement>;

@@ -99,2 +109,3 @@ itemElements: {

onScroll: () => void;
onRawScroll: () => void;
getIndexKey: (index: number, props?: Partial<ListProps<T>>) => any;

@@ -101,0 +112,0 @@ getItemKey: (item: T, props?: Partial<ListProps<T>>) => any;

200

es/List.js

@@ -31,3 +31,3 @@ function _typeof(obj) { 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); }

import Filler from './Filler';
import { getElementScrollPercentage, getScrollPercentage, getNodeHeight, getRangeIndex, getItemAbsoluteTop, GHOST_ITEM_KEY, getItemRelativeTop, getCompareItemRelativeTop, alignScrollTop } from './utils/itemUtil';
import { getElementScrollPercentage, getScrollPercentage, getNodeHeight, getRangeIndex, getItemAbsoluteTop, GHOST_ITEM_KEY, getItemRelativeTop, getCompareItemRelativeTop, alignScrollTop, requireVirtual } from './utils/itemUtil';
import { getIndexByStartLoc, findListDiffIndex } from './utils/algorithmUtil';

@@ -72,11 +72,2 @@ var ScrollStyle = {

_this = _possibleConstructorReturn(this, _getPrototypeOf(List).call(this, props));
_this.state = {
status: 'NONE',
scrollTop: null,
itemIndex: 0,
itemOffsetPtg: 0,
startIndex: 0,
endIndex: 0,
startItemTop: 0
};
_this.listRef = React.createRef();

@@ -130,2 +121,10 @@ _this.itemElements = {};

_this.onRawScroll = function () {
var scrollTop = _this.listRef.current.scrollTop;
_this.setState({
scrollTop: scrollTop
});
};
_this.getIndexKey = function (index, props) {

@@ -201,2 +200,12 @@ var mergedProps = props || _this.props;

_this.cachedProps = props;
_this.state = {
status: 'NONE',
scrollTop: null,
itemIndex: 0,
itemOffsetPtg: 0,
startIndex: 0,
endIndex: 0,
startItemTop: 0,
isVirtual: requireVirtual(props.height, props.itemHeight, props.data.length)
};
return _this;

@@ -232,14 +241,46 @@ }

itemHeight = _this$props2.itemHeight,
disabled = _this$props2.disabled;
disabled = _this$props2.disabled,
onSkipRender = _this$props2.onSkipRender;
var prevData = this.cachedProps.data || [];
var changedItemIndex = prevData.length !== data.length ? findListDiffIndex(prevData, data, this.getItemKey) : null;
if (disabled || !this.listRef.current) {
if (disabled) {
// Should trigger `onSkipRender` to tell that diff component is not render in the list
if (data.length > prevData.length) {
var _this$state2 = this.state,
startIndex = _this$state2.startIndex,
endIndex = _this$state2.endIndex;
if (onSkipRender && (changedItemIndex < startIndex || endIndex < changedItemIndex)) {
onSkipRender();
}
}
return;
}
var isVirtual = requireVirtual(height, itemHeight, data.length);
var nextStatus = status;
if (this.state.isVirtual !== isVirtual) {
nextStatus = isVirtual ? 'SWITCH_TO_VIRTUAL' : 'SWITCH_TO_RAW';
this.setState({
isVirtual: isVirtual,
status: nextStatus
});
/**
* We will wait a tick to let list turn to virtual list.
* And then use virtual list sync logic to adjust the scroll.
*/
if (nextStatus === 'SWITCH_TO_VIRTUAL') {
return;
}
}
if (status === 'MEASURE_START') {
var _this$state2 = this.state,
startIndex = _this$state2.startIndex,
itemIndex = _this$state2.itemIndex,
itemOffsetPtg = _this$state2.itemOffsetPtg;
var _this$state3 = this.state,
_startIndex = _this$state3.startIndex,
itemIndex = _this$state3.itemIndex,
itemOffsetPtg = _this$state3.itemOffsetPtg;
var scrollTop = this.listRef.current.scrollTop; // Record here since measure item height will get warning in `render`

@@ -260,3 +301,3 @@

for (var index = itemIndex - 1; index >= startIndex; index -= 1) {
for (var index = itemIndex - 1; index >= _startIndex; index -= 1) {
startItemTop -= this.itemElementHeights[this.getIndexKey(index)] || 0;

@@ -270,34 +311,64 @@ }

}
/**
* Re-calculate the item position since `data` length changed.
* [IMPORTANT] We use relative position calculate here.
*/
if (status === 'SWITCH_TO_RAW') {
/**
* After virtual list back to raw list,
* we update the `scrollTop` to real top instead of percentage top.
*/
var _this$state$cacheScro = this.state.cacheScroll,
_itemIndex = _this$state$cacheScro.itemIndex,
relativeTop = _this$state$cacheScro.relativeTop;
var rawTop = relativeTop;
if (prevData.length !== data.length && height) {
var _this$state3 = this.state,
originItemIndex = _this$state3.itemIndex,
originItemOffsetPtg = _this$state3.itemOffsetPtg,
originStartIndex = _this$state3.startIndex,
originEndIndex = _this$state3.endIndex,
originScrollTop = _this$state3.scrollTop; // 1. Refresh item heights
for (var _index = 0; _index < _itemIndex; _index += 1) {
rawTop -= this.itemElementHeights[this.getIndexKey(_index)] || 0;
}
this.lockScroll = true;
this.listRef.current.scrollTop = -rawTop;
this.setState({
status: 'MEASURE_DONE'
});
requestAnimationFrame(function () {
requestAnimationFrame(function () {
_this2.lockScroll = false;
});
});
} else if (prevData.length !== data.length && height) {
/**
* Re-calculate the item position since `data` length changed.
* [IMPORTANT] We use relative position calculate here.
*/
var originItemIndex = this.state.itemIndex;
var _this$state4 = this.state,
originItemOffsetPtg = _this$state4.itemOffsetPtg,
originStartIndex = _this$state4.startIndex,
originEndIndex = _this$state4.endIndex,
originScrollTop = _this$state4.scrollTop; // 1. Refresh item heights
this.collectItemHeights(); // 1. Get origin located item top
var originLocatedItemRelativeTop = getItemRelativeTop({
itemIndex: originItemIndex,
itemOffsetPtg: originItemOffsetPtg,
itemElementHeights: this.itemElementHeights,
scrollPtg: getScrollPercentage({
scrollTop: originScrollTop,
scrollHeight: prevData.length * itemHeight,
clientHeight: this.listRef.current.clientHeight
}),
clientHeight: this.listRef.current.clientHeight,
getItemKey: function getItemKey(index) {
return _this2.getIndexKey(index, _this2.cachedProps);
}
}); // 2. Find the compare item
var originLocatedItemRelativeTop;
var changedItemIndex = findListDiffIndex(prevData, data, this.getItemKey);
if (this.state.status === 'SWITCH_TO_VIRTUAL') {
originItemIndex = 0;
originLocatedItemRelativeTop = -this.state.scrollTop;
} else {
originLocatedItemRelativeTop = getItemRelativeTop({
itemIndex: originItemIndex,
itemOffsetPtg: originItemOffsetPtg,
itemElementHeights: this.itemElementHeights,
scrollPtg: getScrollPercentage({
scrollTop: originScrollTop,
scrollHeight: prevData.length * itemHeight,
clientHeight: this.listRef.current.clientHeight
}),
clientHeight: this.listRef.current.clientHeight,
getItemKey: function getItemKey(index) {
return _this2.getIndexKey(index, _this2.cachedProps);
}
});
} // 2. Find the compare item
var originCompareItemIndex = changedItemIndex - 1; // Use next one since there are not more item before removed

@@ -321,6 +392,19 @@

});
this.internalScrollTo({
itemIndex: originCompareItemIndex,
relativeTop: originCompareItemTop
});
if (nextStatus === 'SWITCH_TO_RAW') {
/**
* We will record current measure relative item top and apply in raw list after list turned
*/
this.setState({
cacheScroll: {
itemIndex: originCompareItemIndex,
relativeTop: originCompareItemTop
}
});
} else {
this.internalScrollTo({
itemIndex: originCompareItemIndex,
relativeTop: originCompareItemTop
});
}
}

@@ -440,2 +524,4 @@

value: function render() {
var isVirtual = this.state.isVirtual;
var _this$props4 = this.props,

@@ -450,6 +536,7 @@ style = _this$props4.style,

itemKey = _this$props4.itemKey,
restProps = _objectWithoutProperties(_this$props4, ["style", "component", "height", "itemHeight", "data", "children", "itemKey"]); // Render pure list if not set height or height is enough for all items
onSkipRender = _this$props4.onSkipRender,
restProps = _objectWithoutProperties(_this$props4, ["style", "component", "height", "itemHeight", "data", "children", "itemKey", "onSkipRender"]); // Render pure list if not set height or height is enough for all items
if (typeof height !== 'number' || data.length * itemHeight <= height) {
if (!isVirtual) {
return React.createElement(Component, Object.assign({

@@ -459,3 +546,6 @@ style: height ? _objectSpread({}, style, {

}, ScrollStyle) : style
}, restProps), React.createElement(Filler, {
}, restProps, {
onScroll: this.onRawScroll,
ref: this.listRef
}), React.createElement(Filler, {
height: height

@@ -470,7 +560,7 @@ }, this.renderChildren(data, 0, children)));

var _this$state4 = this.state,
status = _this$state4.status,
startIndex = _this$state4.startIndex,
endIndex = _this$state4.endIndex,
startItemTop = _this$state4.startItemTop;
var _this$state5 = this.state,
status = _this$state5.status,
startIndex = _this$state5.startIndex,
endIndex = _this$state5.endIndex,
startItemTop = _this$state5.startItemTop;
var contentHeight = data.length * itemHeight * ITEM_SCALE_RATE;

@@ -477,0 +567,0 @@ return React.createElement(Component, Object.assign({

@@ -62,2 +62,3 @@ /**

export declare function getCompareItemRelativeTop({ locatedItemRelativeTop, locatedItemIndex, compareItemIndex, startIndex, endIndex, getItemKey, itemElementHeights, }: CompareItemConfig): number;
export declare function requireVirtual(height: number, itemHeight: number, count: number): boolean;
export {};

@@ -159,2 +159,5 @@ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

return originCompareItemTop;
}
export function requireVirtual(height, itemHeight, count) {
return typeof height === 'number' && count * itemHeight > height;
}

@@ -22,5 +22,8 @@ import * as React from 'react';

disabled?: boolean;
/** When `disabled`, trigger if changed item not render. */
onSkipRender?: () => void;
}
declare type Status = 'NONE' | 'MEASURE_START' | 'MEASURE_DONE' | 'SWITCH_TO_VIRTUAL' | 'SWITCH_TO_RAW';
interface ListState<T> {
status: 'NONE' | 'MEASURE_START' | 'MEASURE_DONE';
status: Status;
scrollTop: number | null;

@@ -39,2 +42,10 @@ /** Located item index */

startItemTop: number;
/**
* Tell if is using virtual scroll
*/
isVirtual: boolean;
/**
* Only used when turn virtual list to raw list
*/
cacheScroll?: RelativeScroll;
}

@@ -67,3 +78,2 @@ /**

};
state: ListState<T>;
listRef: React.RefObject<HTMLElement>;

@@ -99,2 +109,3 @@ itemElements: {

onScroll: () => void;
onRawScroll: () => void;
getIndexKey: (index: number, props?: Partial<ListProps<T>>) => any;

@@ -101,0 +112,0 @@ getItemKey: (item: T, props?: Partial<ListProps<T>>) => any;

@@ -86,11 +86,2 @@ "use strict";

_this = _possibleConstructorReturn(this, _getPrototypeOf(List).call(this, props));
_this.state = {
status: 'NONE',
scrollTop: null,
itemIndex: 0,
itemOffsetPtg: 0,
startIndex: 0,
endIndex: 0,
startItemTop: 0
};
_this.listRef = React.createRef();

@@ -144,2 +135,10 @@ _this.itemElements = {};

_this.onRawScroll = function () {
var scrollTop = _this.listRef.current.scrollTop;
_this.setState({
scrollTop: scrollTop
});
};
_this.getIndexKey = function (index, props) {

@@ -215,2 +214,12 @@ var mergedProps = props || _this.props;

_this.cachedProps = props;
_this.state = {
status: 'NONE',
scrollTop: null,
itemIndex: 0,
itemOffsetPtg: 0,
startIndex: 0,
endIndex: 0,
startItemTop: 0,
isVirtual: (0, _itemUtil.requireVirtual)(props.height, props.itemHeight, props.data.length)
};
return _this;

@@ -246,14 +255,46 @@ }

itemHeight = _this$props2.itemHeight,
disabled = _this$props2.disabled;
disabled = _this$props2.disabled,
onSkipRender = _this$props2.onSkipRender;
var prevData = this.cachedProps.data || [];
var changedItemIndex = prevData.length !== data.length ? (0, _algorithmUtil.findListDiffIndex)(prevData, data, this.getItemKey) : null;
if (disabled || !this.listRef.current) {
if (disabled) {
// Should trigger `onSkipRender` to tell that diff component is not render in the list
if (data.length > prevData.length) {
var _this$state2 = this.state,
startIndex = _this$state2.startIndex,
endIndex = _this$state2.endIndex;
if (onSkipRender && (changedItemIndex < startIndex || endIndex < changedItemIndex)) {
onSkipRender();
}
}
return;
}
var isVirtual = (0, _itemUtil.requireVirtual)(height, itemHeight, data.length);
var nextStatus = status;
if (this.state.isVirtual !== isVirtual) {
nextStatus = isVirtual ? 'SWITCH_TO_VIRTUAL' : 'SWITCH_TO_RAW';
this.setState({
isVirtual: isVirtual,
status: nextStatus
});
/**
* We will wait a tick to let list turn to virtual list.
* And then use virtual list sync logic to adjust the scroll.
*/
if (nextStatus === 'SWITCH_TO_VIRTUAL') {
return;
}
}
if (status === 'MEASURE_START') {
var _this$state2 = this.state,
startIndex = _this$state2.startIndex,
itemIndex = _this$state2.itemIndex,
itemOffsetPtg = _this$state2.itemOffsetPtg;
var _this$state3 = this.state,
_startIndex = _this$state3.startIndex,
itemIndex = _this$state3.itemIndex,
itemOffsetPtg = _this$state3.itemOffsetPtg;
var scrollTop = this.listRef.current.scrollTop; // Record here since measure item height will get warning in `render`

@@ -274,3 +315,3 @@

for (var index = itemIndex - 1; index >= startIndex; index -= 1) {
for (var index = itemIndex - 1; index >= _startIndex; index -= 1) {
startItemTop -= this.itemElementHeights[this.getIndexKey(index)] || 0;

@@ -284,34 +325,64 @@ }

}
/**
* Re-calculate the item position since `data` length changed.
* [IMPORTANT] We use relative position calculate here.
*/
if (status === 'SWITCH_TO_RAW') {
/**
* After virtual list back to raw list,
* we update the `scrollTop` to real top instead of percentage top.
*/
var _this$state$cacheScro = this.state.cacheScroll,
_itemIndex = _this$state$cacheScro.itemIndex,
relativeTop = _this$state$cacheScro.relativeTop;
var rawTop = relativeTop;
if (prevData.length !== data.length && height) {
var _this$state3 = this.state,
originItemIndex = _this$state3.itemIndex,
originItemOffsetPtg = _this$state3.itemOffsetPtg,
originStartIndex = _this$state3.startIndex,
originEndIndex = _this$state3.endIndex,
originScrollTop = _this$state3.scrollTop; // 1. Refresh item heights
for (var _index = 0; _index < _itemIndex; _index += 1) {
rawTop -= this.itemElementHeights[this.getIndexKey(_index)] || 0;
}
this.lockScroll = true;
this.listRef.current.scrollTop = -rawTop;
this.setState({
status: 'MEASURE_DONE'
});
requestAnimationFrame(function () {
requestAnimationFrame(function () {
_this2.lockScroll = false;
});
});
} else if (prevData.length !== data.length && height) {
/**
* Re-calculate the item position since `data` length changed.
* [IMPORTANT] We use relative position calculate here.
*/
var originItemIndex = this.state.itemIndex;
var _this$state4 = this.state,
originItemOffsetPtg = _this$state4.itemOffsetPtg,
originStartIndex = _this$state4.startIndex,
originEndIndex = _this$state4.endIndex,
originScrollTop = _this$state4.scrollTop; // 1. Refresh item heights
this.collectItemHeights(); // 1. Get origin located item top
var originLocatedItemRelativeTop = (0, _itemUtil.getItemRelativeTop)({
itemIndex: originItemIndex,
itemOffsetPtg: originItemOffsetPtg,
itemElementHeights: this.itemElementHeights,
scrollPtg: (0, _itemUtil.getScrollPercentage)({
scrollTop: originScrollTop,
scrollHeight: prevData.length * itemHeight,
clientHeight: this.listRef.current.clientHeight
}),
clientHeight: this.listRef.current.clientHeight,
getItemKey: function getItemKey(index) {
return _this2.getIndexKey(index, _this2.cachedProps);
}
}); // 2. Find the compare item
var originLocatedItemRelativeTop;
var changedItemIndex = (0, _algorithmUtil.findListDiffIndex)(prevData, data, this.getItemKey);
if (this.state.status === 'SWITCH_TO_VIRTUAL') {
originItemIndex = 0;
originLocatedItemRelativeTop = -this.state.scrollTop;
} else {
originLocatedItemRelativeTop = (0, _itemUtil.getItemRelativeTop)({
itemIndex: originItemIndex,
itemOffsetPtg: originItemOffsetPtg,
itemElementHeights: this.itemElementHeights,
scrollPtg: (0, _itemUtil.getScrollPercentage)({
scrollTop: originScrollTop,
scrollHeight: prevData.length * itemHeight,
clientHeight: this.listRef.current.clientHeight
}),
clientHeight: this.listRef.current.clientHeight,
getItemKey: function getItemKey(index) {
return _this2.getIndexKey(index, _this2.cachedProps);
}
});
} // 2. Find the compare item
var originCompareItemIndex = changedItemIndex - 1; // Use next one since there are not more item before removed

@@ -335,6 +406,19 @@

});
this.internalScrollTo({
itemIndex: originCompareItemIndex,
relativeTop: originCompareItemTop
});
if (nextStatus === 'SWITCH_TO_RAW') {
/**
* We will record current measure relative item top and apply in raw list after list turned
*/
this.setState({
cacheScroll: {
itemIndex: originCompareItemIndex,
relativeTop: originCompareItemTop
}
});
} else {
this.internalScrollTo({
itemIndex: originCompareItemIndex,
relativeTop: originCompareItemTop
});
}
}

@@ -454,2 +538,4 @@

value: function render() {
var isVirtual = this.state.isVirtual;
var _this$props4 = this.props,

@@ -464,6 +550,7 @@ style = _this$props4.style,

itemKey = _this$props4.itemKey,
restProps = _objectWithoutProperties(_this$props4, ["style", "component", "height", "itemHeight", "data", "children", "itemKey"]); // Render pure list if not set height or height is enough for all items
onSkipRender = _this$props4.onSkipRender,
restProps = _objectWithoutProperties(_this$props4, ["style", "component", "height", "itemHeight", "data", "children", "itemKey", "onSkipRender"]); // Render pure list if not set height or height is enough for all items
if (typeof height !== 'number' || data.length * itemHeight <= height) {
if (!isVirtual) {
return React.createElement(Component, Object.assign({

@@ -473,3 +560,6 @@ style: height ? _objectSpread({}, style, {

}, ScrollStyle) : style
}, restProps), React.createElement(_Filler.default, {
}, restProps, {
onScroll: this.onRawScroll,
ref: this.listRef
}), React.createElement(_Filler.default, {
height: height

@@ -484,7 +574,7 @@ }, this.renderChildren(data, 0, children)));

var _this$state4 = this.state,
status = _this$state4.status,
startIndex = _this$state4.startIndex,
endIndex = _this$state4.endIndex,
startItemTop = _this$state4.startItemTop;
var _this$state5 = this.state,
status = _this$state5.status,
startIndex = _this$state5.startIndex,
endIndex = _this$state5.endIndex,
startItemTop = _this$state5.startItemTop;
var contentHeight = data.length * itemHeight * ITEM_SCALE_RATE;

@@ -491,0 +581,0 @@ return React.createElement(Component, Object.assign({

@@ -62,2 +62,3 @@ /**

export declare function getCompareItemRelativeTop({ locatedItemRelativeTop, locatedItemIndex, compareItemIndex, startIndex, endIndex, getItemKey, itemElementHeights, }: CompareItemConfig): number;
export declare function requireVirtual(height: number, itemHeight: number, count: number): boolean;
export {};

@@ -14,2 +14,3 @@ "use strict";

exports.getCompareItemRelativeTop = getCompareItemRelativeTop;
exports.requireVirtual = requireVirtual;
exports.GHOST_ITEM_KEY = void 0;

@@ -186,2 +187,6 @@

return originCompareItemTop;
}
function requireVirtual(height, itemHeight, count) {
return typeof height === 'number' && count * itemHeight > height;
}
{
"name": "rc-virtual-list",
"version": "0.0.0-alpha.7",
"version": "0.0.0-alpha.8",
"description": "React Virtual List Component",

@@ -5,0 +5,0 @@ "keywords": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc