@tamagui/react-native-web-lite
Version:
React Native for Web
339 lines (336 loc) • 12.8 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { invariant } from "@tamagui/react-native-web-internals";
import ScrollView from "../../../ScrollView";
import RefreshControl from "../../../RefreshControl";
import Batchinator from "../Batchinator";
import ChildListCollection from "./ChildListCollection.native.js";
import FillRateHelper from "../FillRateHelper";
import StateSafePureComponent from "./StateSafePureComponent.native.js";
import ViewabilityHelper from "../ViewabilityHelper.native.js";
import CellRenderer from "./VirtualizedListCellRenderer.native.js";
import { VirtualizedListContext } from "./VirtualizedListContext.native.js";
function _assert_this_initialized(self) {
if (self === void 0) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
return self;
}
function _call_super(_this, derived, args) {
return derived = _get_prototype_of(derived), _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
}
function _class_call_check(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 || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _create_class(Constructor, protoProps, staticProps) {
return protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Constructor;
}
function _define_property(obj, key, value) {
return key in obj ? Object.defineProperty(obj, key, {
value,
enumerable: !0,
configurable: !0,
writable: !0
}) : obj[key] = value, obj;
}
function _get_prototype_of(o) {
return _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function (o2) {
return o2.__proto__ || Object.getPrototypeOf(o2);
}, _get_prototype_of(o);
}
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: !0,
configurable: !0
}
}), superClass && _set_prototype_of(subClass, superClass);
}
function _possible_constructor_return(self, call) {
return call && (_type_of(call) === "object" || typeof call == "function") ? call : _assert_this_initialized(self);
}
function _set_prototype_of(o, p) {
return _set_prototype_of = Object.setPrototypeOf || function (o2, p2) {
return o2.__proto__ = p2, o2;
}, _set_prototype_of(o, p);
}
function _type_of(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol < "u" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
function _is_native_reflect_construct() {
try {
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
} catch {}
return (_is_native_reflect_construct = function () {
return !!result;
})();
}
var __DEV__ = process.env.NODE_ENV !== "production";
function horizontalOrDefault(horizontal) {
return horizontal ?? !1;
}
function scrollEventThrottleOrDefault(scrollEventThrottle) {
return scrollEventThrottle ?? 50;
}
var VirtualizedList = /* @__PURE__ */function (StateSafePureComponent2) {
"use strict";
_inherits(VirtualizedList2, StateSafePureComponent2);
function VirtualizedList2(props) {
_class_call_check(this, VirtualizedList2);
var _this;
_this = _call_super(this, VirtualizedList2, [props]), _define_property(_this, "_captureRef", function (ref) {
_this._scrollRef = ref;
}), _define_property(_this, "_onContentSizeChange", function (width, height) {}),
// Handle content size changes
_define_property(_this, "_onLayout", function (event) {}),
// Handle layout changes
_define_property(_this, "_onScroll", function (event) {}), _this._nestedChildLists = new ChildListCollection(), _this._viewabilityTuples = [], _this._scrollMetrics = {
contentLength: 0,
dOffset: 0,
dt: 10,
offset: 0,
timestamp: 0,
velocity: 0,
visibleLength: 0
}, _this._highestMeasuredFrameIndex = 0, _this._headerLength = 0, _this._footerLength = 0, _this._averageCellLength = 0, _this._hasWarned = {}, _this._fillRateHelper = new FillRateHelper(_this._getFrameMetrics);
var _this_props_updateCellsBatchingPeriod;
return _this._updateCellsToRenderBatcher = new Batchinator(_this._updateCellsToRender, (_this_props_updateCellsBatchingPeriod = _this.props.updateCellsBatchingPeriod) !== null && _this_props_updateCellsBatchingPeriod !== void 0 ? _this_props_updateCellsBatchingPeriod : 50), _this.props.viewabilityConfig && _this.props.onViewableItemsChanged && _this._viewabilityTuples.push({
viewabilityHelper: new ViewabilityHelper(_this.props.viewabilityConfig),
onViewableItemsChanged: _this.props.onViewableItemsChanged
}), _this;
}
return _create_class(VirtualizedList2, [{
key: "scrollToEnd",
value: function (params) {
var animated = params ? params.animated : !0,
veryLast = this.props.getItemCount(this.props.data) - 1;
if (!(veryLast < 0)) {
var frame = this.__getFrameMetricsApprox(veryLast, this.props),
offset = Math.max(0, frame.offset + frame.length + this._footerLength - this._scrollMetrics.visibleLength);
if (this._scrollRef != null) {
if (this._scrollRef.scrollTo == null) {
console.warn("No scrollTo method provided. This may be because you have two nested VirtualizedLists with the same orientation, or because you are using a custom component that does not implement scrollTo.");
return;
}
this._scrollRef.scrollTo(horizontalOrDefault(this.props.horizontal) ? {
x: offset,
animated
} : {
y: offset,
animated
});
}
}
}
}, {
key: "scrollToIndex",
value: function (params) {
var {
data,
horizontal,
getItemCount,
getItemLayout,
onScrollToIndexFailed
} = this.props,
{
animated,
index,
viewOffset,
viewPosition
} = params;
if (invariant(index >= 0, `scrollToIndex out of range: requested index ${index} but minimum is 0`), invariant(getItemCount(data) >= 1, `scrollToIndex out of range: item length ${getItemCount(data)} but minimum is 1`), invariant(index < getItemCount(data), `scrollToIndex out of range: requested index ${index} is out of 0 to ${getItemCount(data) - 1}`), !getItemLayout && index > this._highestMeasuredFrameIndex) {
invariant(!!onScrollToIndexFailed, "scrollToIndex should be used in conjunction with getItemLayout or onScrollToIndexFailed, otherwise there is no way to know the location of offscreen indices or handle failures."), onScrollToIndexFailed({
averageItemLength: this._averageCellLength,
highestMeasuredFrameIndex: this._highestMeasuredFrameIndex,
index
});
return;
}
var frame = this.__getFrameMetricsApprox(Math.floor(index), this.props),
offset = Math.max(0, this._getOffsetApprox(index, this.props) - (viewPosition || 0) * (this._scrollMetrics.visibleLength - frame.length)) - (viewOffset || 0);
this._scrollRef != null && this._scrollRef.scrollTo(horizontalOrDefault(horizontal) ? {
x: offset,
animated
} : {
y: offset,
animated
});
}
}, {
key: "scrollToItem",
value: function (params) {
var {
data,
getItem,
getItemCount,
horizontal,
onScrollToIndexFailed
} = this.props,
{
animated,
item,
viewPosition,
viewOffset
} = params,
index = this.props.data.indexOf(item);
if (index !== -1) this.scrollToIndex({
animated,
index,
viewOffset,
viewPosition
});else for (var itemCount = getItemCount(data), i = 0; i < itemCount; i++) if (getItem(data, i) === item) {
this.scrollToIndex({
animated,
index: i,
viewOffset,
viewPosition
});
break;
}
}
}, {
key: "scrollToOffset",
value: function (params) {
var {
animated,
offset
} = params;
this._scrollRef != null && this._scrollRef.scrollTo(horizontalOrDefault(this.props.horizontal) ? {
x: offset,
animated
} : {
y: offset,
animated
});
}
}, {
key: "recordInteraction",
value: function () {
this._nestedChildLists.forEach(function (childList) {
childList.recordInteraction();
}), this._viewabilityTuples.forEach(function (viewabilityTuple) {
viewabilityTuple.viewabilityHelper.recordInteraction();
});
}
}, {
key: "flashScrollIndicators",
value: function () {
this._scrollRef && this._scrollRef.flashScrollIndicators && this._scrollRef.flashScrollIndicators();
}
}, {
key: "getScrollResponder",
value: function () {
if (this._scrollRef && this._scrollRef.getScrollResponder) return this._scrollRef.getScrollResponder();
}
}, {
key: "getScrollableNode",
value: function () {
if (this._scrollRef && this._scrollRef.getScrollableNode) return this._scrollRef.getScrollableNode();
}
}, {
key: "getScrollRef",
value: function () {
return this._scrollRef;
}
}, {
key: "setNativeProps",
value: function (props) {
this._scrollRef && this._scrollRef.setNativeProps(props);
}
}, {
key: "render",
value: function () {
var {
ListEmptyComponent,
ListFooterComponent,
ListHeaderComponent,
data,
debug,
disableVirtualization,
getItem,
getItemCount,
getItemLayout,
horizontal,
keyExtractor,
numColumns,
onEndReached,
onEndReachedThreshold,
onLayout,
onRefresh,
onScroll,
onScrollBeginDrag,
onScrollEndDrag,
onMomentumScrollBegin,
onMomentumScrollEnd,
onStartReached,
onStartReachedThreshold,
onViewableItemsChanged,
refreshing,
removeClippedSubviews,
renderItem,
viewabilityConfig,
viewabilityConfigCallbackPairs,
...restProps
} = this.props,
itemCount = getItemCount(data);
return itemCount === 0 ? ListEmptyComponent ? /* @__PURE__ */_jsx(ListEmptyComponent, {}) : null : /* @__PURE__ */_jsx(ScrollView, {
...restProps,
ref: this._captureRef,
onContentSizeChange: this._onContentSizeChange,
onLayout: this._onLayout,
onScroll: this._onScroll,
refreshControl: onRefresh && /* @__PURE__ */_jsx(RefreshControl, {
refreshing,
onRefresh
}),
scrollEventThrottle: scrollEventThrottleOrDefault(this.props.scrollEventThrottle),
removeClippedSubviews,
children: this._renderChildren()
});
}
}, {
key: "_renderChildren",
value: function () {
for (var {
data,
getItem,
getItemCount,
renderItem
} = this.props, items = [], i = 0; i < getItemCount(data); i++) {
var item = getItem(data, i);
items.push(/* @__PURE__ */_jsx(CellRenderer, {
cellKey: String(i),
index: i,
item,
renderItem
}, this.props.keyExtractor ? this.props.keyExtractor(item, i) : i));
}
return items;
}
}, {
// Handle scroll events
key: "__getFrameMetricsApprox",
value: function (index, props) {
var frame = {
length: this._averageCellLength,
offset: this._averageCellLength * index
};
return frame;
}
}, {
key: "_getOffsetApprox",
value: function (index, props) {
return this.__getFrameMetricsApprox(index, props).offset;
}
}]), VirtualizedList2;
}(StateSafePureComponent);
_define_property(VirtualizedList, "contextType", VirtualizedListContext);
var VirtualizedList_default = VirtualizedList;
export { VirtualizedList_default as default };
//# sourceMappingURL=index.native.js.map