fui-material 2.8.24 → 2.8.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4958,56 +4958,103 @@ function FProgressiveList({
4958
4958
  virtualize = false,
4959
4959
  overscan = 2
4960
4960
  }) {
4961
- const totalCount = items.length;
4961
+ const safeItems = useMemo(() => {
4962
+ if (Array.isArray(items)) {
4963
+ return items;
4964
+ }
4965
+ if (items && typeof items === "object") {
4966
+ const wrapper = items;
4967
+ const candidate = wrapper.items ?? wrapper.data ?? wrapper.content ?? wrapper.results;
4968
+ if (Array.isArray(candidate)) {
4969
+ return candidate;
4970
+ }
4971
+ if (typeof wrapper.length === "number" && typeof wrapper.slice === "function") {
4972
+ return Array.from(wrapper);
4973
+ }
4974
+ }
4975
+ return [];
4976
+ }, [items]);
4977
+ const totalCount = safeItems.length;
4978
+ const safeChunkSize = Math.max(1, Number(chunkSize) || 6);
4979
+ const safeEstimatedItemHeight = estimatedItemHeight > 0 ? estimatedItemHeight : 280;
4980
+ const safeOverscan = Math.max(0, Number(overscan) || 0);
4981
+ const safeThreshold = Math.max(0, Number(threshold) || 250);
4982
+ const defaultKeyExtractor = useCallback((item, index) => {
4983
+ if (item && typeof item === "object") {
4984
+ const itemObj = item;
4985
+ const candidate = itemObj.id ?? itemObj.key ?? itemObj.uuid ?? itemObj.code;
4986
+ if (candidate !== void 0 && candidate !== null) {
4987
+ return String(candidate);
4988
+ }
4989
+ }
4990
+ return index;
4991
+ }, []);
4992
+ const safeKeyExtractor = typeof keyExtractor === "function" ? keyExtractor : defaultKeyExtractor;
4962
4993
  const containerRef = useRef(null);
4963
- const [renderedCount, setRenderedCount] = useState(() => Math.min(chunkSize, totalCount));
4994
+ const [renderedCount, setRenderedCount] = useState(() => Math.min(safeChunkSize, totalCount));
4964
4995
  const sentinelRef = useRef(null);
4965
4996
  const prevFirstKeyRef = useRef(void 0);
4966
4997
  const prevTotalCountRef = useRef(totalCount);
4967
4998
  useEffect(() => {
4968
4999
  if (virtualize) return;
4969
- const currentFirstKey = items.length > 0 ? keyExtractor(items[0], 0) : void 0;
5000
+ const currentFirstKey = safeItems.length > 0 ? safeKeyExtractor(safeItems[0], 0) : void 0;
4970
5001
  const isReset = currentFirstKey !== prevFirstKeyRef.current || totalCount < prevTotalCountRef.current;
4971
5002
  if (isReset) {
4972
- setRenderedCount(Math.min(chunkSize, totalCount));
5003
+ setRenderedCount(Math.min(safeChunkSize, totalCount));
5004
+ } else if (renderedCount > totalCount) {
5005
+ setRenderedCount(totalCount);
4973
5006
  }
4974
5007
  prevFirstKeyRef.current = currentFirstKey;
4975
5008
  prevTotalCountRef.current = totalCount;
4976
- }, [items, keyExtractor, chunkSize, totalCount, virtualize]);
5009
+ }, [safeItems, safeKeyExtractor, safeChunkSize, totalCount, virtualize, renderedCount]);
4977
5010
  useEffect(() => {
4978
5011
  if (virtualize) return;
4979
5012
  if (renderedCount >= totalCount) return;
4980
- const frameId = requestAnimationFrame(() => {
4981
- setRenderedCount((prev) => Math.min(prev + chunkSize, totalCount));
5013
+ const raf = typeof requestAnimationFrame !== "undefined" ? requestAnimationFrame : (cb) => setTimeout(cb, 16);
5014
+ const caf = typeof cancelAnimationFrame !== "undefined" ? cancelAnimationFrame : (id) => clearTimeout(id);
5015
+ const frameId = raf(() => {
5016
+ setRenderedCount((prev) => Math.min(prev + safeChunkSize, totalCount));
4982
5017
  });
4983
- return () => cancelAnimationFrame(frameId);
4984
- }, [renderedCount, totalCount, chunkSize, virtualize]);
5018
+ return () => caf(frameId);
5019
+ }, [renderedCount, totalCount, safeChunkSize, virtualize]);
4985
5020
  const measuredHeights = useRef(/* @__PURE__ */ new Map());
4986
5021
  const [measureVersion, setMeasureVersion] = useState(0);
4987
5022
  const resizeObserverRef = useRef(null);
4988
5023
  useEffect(() => {
4989
5024
  if (!virtualize) return;
4990
- const currentFirstKey = items.length > 0 ? keyExtractor(items[0], 0) : void 0;
5025
+ const currentFirstKey = safeItems.length > 0 ? safeKeyExtractor(safeItems[0], 0) : void 0;
4991
5026
  if (currentFirstKey !== prevFirstKeyRef.current || totalCount < prevTotalCountRef.current) {
4992
5027
  measuredHeights.current.clear();
4993
5028
  setMeasureVersion((v) => v + 1);
4994
5029
  }
4995
5030
  prevFirstKeyRef.current = currentFirstKey;
4996
5031
  prevTotalCountRef.current = totalCount;
4997
- }, [items, keyExtractor, totalCount, virtualize]);
5032
+ }, [safeItems, safeKeyExtractor, totalCount, virtualize]);
4998
5033
  useEffect(() => {
4999
5034
  if (!virtualize) return;
5035
+ if (typeof ResizeObserver === "undefined") return;
5000
5036
  const ro = new ResizeObserver((entries) => {
5001
- var _a, _b;
5002
5037
  let changed = false;
5003
5038
  for (const entry of entries) {
5004
5039
  const indexAttr = entry.target.getAttribute("data-virtual-index");
5005
5040
  if (indexAttr !== null) {
5006
5041
  const index = Number(indexAttr);
5007
- const height = ((_b = (_a = entry.borderBoxSize) == null ? void 0 : _a[0]) == null ? void 0 : _b.blockSize) ?? entry.contentRect.height;
5042
+ let height = 0;
5043
+ const borderBoxSize = entry.borderBoxSize;
5044
+ if (Array.isArray(borderBoxSize) && borderBoxSize[0]) {
5045
+ height = borderBoxSize[0].blockSize;
5046
+ } else if (borderBoxSize && "blockSize" in borderBoxSize && typeof borderBoxSize.blockSize === "number") {
5047
+ height = borderBoxSize.blockSize;
5048
+ } else if (entry.contentRect && typeof entry.contentRect.height === "number") {
5049
+ height = entry.contentRect.height;
5050
+ } else if (entry.target instanceof HTMLElement) {
5051
+ height = entry.target.offsetHeight;
5052
+ }
5053
+ const mb = entry.target instanceof HTMLElement && typeof window !== "undefined" ? parseFloat(window.getComputedStyle(entry.target).marginBottom) || 16 : 16;
5054
+ const fullHeight = height > 0 ? height + mb : 0;
5008
5055
  const prevHeight = measuredHeights.current.get(index);
5009
- if (height > 0 && (!prevHeight || Math.abs(prevHeight - height) > 2)) {
5010
- measuredHeights.current.set(index, height);
5056
+ if (fullHeight > 0 && (!prevHeight || Math.abs(prevHeight - fullHeight) > 2)) {
5057
+ measuredHeights.current.set(index, fullHeight);
5011
5058
  changed = true;
5012
5059
  }
5013
5060
  }
@@ -5017,7 +5064,14 @@ function FProgressiveList({
5017
5064
  }
5018
5065
  });
5019
5066
  resizeObserverRef.current = ro;
5020
- return () => ro.disconnect();
5067
+ if (containerRef.current) {
5068
+ const virtualElements = containerRef.current.querySelectorAll("[data-virtual-index]");
5069
+ virtualElements.forEach((el) => ro.observe(el));
5070
+ }
5071
+ return () => {
5072
+ ro.disconnect();
5073
+ resizeObserverRef.current = null;
5074
+ };
5021
5075
  }, [virtualize]);
5022
5076
  const offsets = useMemo(() => {
5023
5077
  if (!virtualize) return new Float64Array(0);
@@ -5025,41 +5079,50 @@ function FProgressiveList({
5025
5079
  let sum = 0;
5026
5080
  for (let i = 0; i < totalCount; i++) {
5027
5081
  arr[i] = sum;
5028
- const h = measuredHeights.current.get(i) ?? estimatedItemHeight;
5082
+ const h = measuredHeights.current.get(i) ?? safeEstimatedItemHeight;
5029
5083
  sum += h;
5030
5084
  }
5031
5085
  arr[totalCount] = sum;
5032
5086
  return arr;
5033
- }, [totalCount, estimatedItemHeight, virtualize, measureVersion]);
5087
+ }, [totalCount, safeEstimatedItemHeight, virtualize, measureVersion]);
5034
5088
  const [scrollState, setScrollState] = useState(() => ({
5035
5089
  scrollTop: 0,
5036
5090
  viewportHeight: typeof window !== "undefined" ? window.innerHeight : 800
5037
5091
  }));
5038
5092
  useEffect(() => {
5039
5093
  if (!virtualize) return;
5094
+ if (typeof window === "undefined") return;
5040
5095
  let rafId = null;
5041
5096
  const handleScroll = () => {
5042
5097
  if (rafId !== null) return;
5043
- rafId = requestAnimationFrame(() => {
5098
+ const raf = typeof requestAnimationFrame !== "undefined" ? requestAnimationFrame : (cb) => setTimeout(cb, 16);
5099
+ rafId = raf(() => {
5044
5100
  rafId = null;
5045
5101
  if (!containerRef.current) return;
5046
5102
  const rect = containerRef.current.getBoundingClientRect();
5047
5103
  const scrollTop = Math.max(0, -rect.top);
5048
5104
  const viewportHeight = window.innerHeight;
5049
5105
  setScrollState((prev) => {
5050
- if (Math.abs(prev.scrollTop - scrollTop) > 30 || prev.viewportHeight !== viewportHeight) {
5106
+ const isEdge = scrollTop === 0 && prev.scrollTop !== 0;
5107
+ if (isEdge || Math.abs(prev.scrollTop - scrollTop) > 30 || prev.viewportHeight !== viewportHeight) {
5051
5108
  return { scrollTop, viewportHeight };
5052
5109
  }
5053
5110
  return prev;
5054
5111
  });
5055
5112
  });
5056
5113
  };
5057
- window.addEventListener("scroll", handleScroll, { passive: true });
5114
+ window.addEventListener("scroll", handleScroll, { passive: true, capture: true });
5058
5115
  window.addEventListener("resize", handleScroll, { passive: true });
5059
5116
  handleScroll();
5060
5117
  return () => {
5061
- if (rafId !== null) cancelAnimationFrame(rafId);
5062
- window.removeEventListener("scroll", handleScroll);
5118
+ if (rafId !== null) {
5119
+ if (typeof cancelAnimationFrame !== "undefined") {
5120
+ cancelAnimationFrame(rafId);
5121
+ } else {
5122
+ clearTimeout(rafId);
5123
+ }
5124
+ }
5125
+ window.removeEventListener("scroll", handleScroll, { capture: true });
5063
5126
  window.removeEventListener("resize", handleScroll);
5064
5127
  };
5065
5128
  }, [virtualize]);
@@ -5071,8 +5134,8 @@ function FProgressiveList({
5071
5134
  const visibleBottom = scrollState.scrollTop + scrollState.viewportHeight;
5072
5135
  const rawStartIndex = findOffsetIndex(offsets, visibleTop);
5073
5136
  const rawEndIndex = findOffsetIndex(offsets, visibleBottom);
5074
- const sIdx = Math.max(0, rawStartIndex - overscan);
5075
- const eIdx = Math.min(totalCount - 1, rawEndIndex + overscan);
5137
+ const sIdx = Math.max(0, rawStartIndex - safeOverscan);
5138
+ const eIdx = Math.min(totalCount - 1, Math.max(sIdx, rawEndIndex + safeOverscan));
5076
5139
  const topHeight = offsets[sIdx] || 0;
5077
5140
  const totalHeight = offsets[totalCount] || 0;
5078
5141
  const bottomHeight = Math.max(0, totalHeight - (offsets[eIdx + 1] || totalHeight));
@@ -5082,9 +5145,24 @@ function FProgressiveList({
5082
5145
  topSpacerHeight: topHeight,
5083
5146
  bottomSpacerHeight: bottomHeight
5084
5147
  };
5085
- }, [virtualize, totalCount, offsets, scrollState, overscan]);
5148
+ }, [virtualize, totalCount, offsets, scrollState, safeOverscan]);
5149
+ const isCallingLoadMoreRef = useRef(false);
5150
+ useEffect(() => {
5151
+ if (!isLoadingMore) {
5152
+ isCallingLoadMoreRef.current = false;
5153
+ }
5154
+ }, [isLoadingMore]);
5155
+ useEffect(() => {
5156
+ isCallingLoadMoreRef.current = false;
5157
+ }, [totalCount]);
5158
+ const triggerLoadMore = useCallback(() => {
5159
+ if (!onLoadMore || !hasMore || isLoadingMore || isCallingLoadMoreRef.current) return;
5160
+ isCallingLoadMoreRef.current = true;
5161
+ onLoadMore();
5162
+ }, [onLoadMore, hasMore, isLoadingMore]);
5086
5163
  useEffect(() => {
5087
5164
  if (!onLoadMore || !hasMore || isLoadingMore) return;
5165
+ if (typeof IntersectionObserver === "undefined") return;
5088
5166
  if (!virtualize && renderedCount < totalCount) return;
5089
5167
  const sentinelEl = sentinelRef.current;
5090
5168
  if (!sentinelEl) return;
@@ -5092,23 +5170,23 @@ function FProgressiveList({
5092
5170
  (entries) => {
5093
5171
  const [entry] = entries;
5094
5172
  if (entry && entry.isIntersecting) {
5095
- onLoadMore();
5173
+ triggerLoadMore();
5096
5174
  }
5097
5175
  },
5098
5176
  {
5099
- rootMargin: `0px 0px ${threshold}px 0px`,
5177
+ rootMargin: `0px 0px ${safeThreshold}px 0px`,
5100
5178
  threshold: 0
5101
5179
  }
5102
5180
  );
5103
5181
  observer.observe(sentinelEl);
5104
5182
  return () => observer.disconnect();
5105
- }, [onLoadMore, hasMore, isLoadingMore, renderedCount, totalCount, threshold, virtualize]);
5183
+ }, [onLoadMore, hasMore, isLoadingMore, renderedCount, totalCount, safeThreshold, virtualize, triggerLoadMore]);
5106
5184
  useEffect(() => {
5107
5185
  if (!virtualize || !onLoadMore || !hasMore || isLoadingMore) return;
5108
- if (totalCount > 0 && endIndex >= totalCount - 1 - overscan) {
5109
- onLoadMore();
5186
+ if (totalCount > 0 && endIndex >= totalCount - 1 - safeOverscan) {
5187
+ triggerLoadMore();
5110
5188
  }
5111
- }, [virtualize, endIndex, totalCount, overscan, onLoadMore, hasMore, isLoadingMore]);
5189
+ }, [virtualize, endIndex, totalCount, safeOverscan, onLoadMore, hasMore, isLoadingMore, triggerLoadMore]);
5112
5190
  const setItemRef = useCallback((el) => {
5113
5191
  if (el && resizeObserverRef.current) {
5114
5192
  resizeObserverRef.current.observe(el);
@@ -5116,7 +5194,7 @@ function FProgressiveList({
5116
5194
  }, []);
5117
5195
  const itemWrapperStyle = {
5118
5196
  contentVisibility: virtualize ? void 0 : "auto",
5119
- containIntrinsicSize: virtualize ? void 0 : `0 ${estimatedItemHeight}px`,
5197
+ containIntrinsicSize: virtualize ? void 0 : `0 ${safeEstimatedItemHeight}px`,
5120
5198
  marginBottom: 16
5121
5199
  };
5122
5200
  return /* @__PURE__ */ jsxs("div", { ref: containerRef, className, style: st, children: [
@@ -5124,7 +5202,7 @@ function FProgressiveList({
5124
5202
  // --- РЕНДЕР С ВИРТУАЛЬНЫМ ОКНОМ ---
5125
5203
  /* @__PURE__ */ jsxs(Fragment, { children: [
5126
5204
  topSpacerHeight > 0 && /* @__PURE__ */ jsx("div", { style: { height: topSpacerHeight, width: "100%" }, "aria-hidden": "true" }),
5127
- items.slice(startIndex, endIndex + 1).map((item, localIndex) => {
5205
+ totalCount > 0 && safeItems.slice(startIndex, endIndex + 1).map((item, localIndex) => {
5128
5206
  const globalIndex = startIndex + localIndex;
5129
5207
  return /* @__PURE__ */ jsx(
5130
5208
  "div",
@@ -5132,22 +5210,22 @@ function FProgressiveList({
5132
5210
  ref: setItemRef,
5133
5211
  "data-virtual-index": globalIndex,
5134
5212
  style: itemWrapperStyle,
5135
- children: children(item, globalIndex)
5213
+ children: typeof children === "function" ? children(item, globalIndex) : null
5136
5214
  },
5137
- keyExtractor(item, globalIndex)
5215
+ safeKeyExtractor(item, globalIndex)
5138
5216
  );
5139
5217
  }),
5140
5218
  bottomSpacerHeight > 0 && /* @__PURE__ */ jsx("div", { style: { height: bottomSpacerHeight, width: "100%" }, "aria-hidden": "true" })
5141
5219
  ] })
5142
5220
  ) : (
5143
5221
  // --- СТАНДАРТНЫЙ ПРОГРЕССИВНЫЙ РЕНДЕР ---
5144
- items.slice(0, renderedCount).map((item, index) => /* @__PURE__ */ jsx(
5222
+ safeItems.slice(0, renderedCount).map((item, index) => /* @__PURE__ */ jsx(
5145
5223
  "div",
5146
5224
  {
5147
5225
  style: itemWrapperStyle,
5148
- children: children(item, index)
5226
+ children: typeof children === "function" ? children(item, index) : null
5149
5227
  },
5150
- keyExtractor(item, index)
5228
+ safeKeyExtractor(item, index)
5151
5229
  ))
5152
5230
  ),
5153
5231
  !virtualize && showProgress && renderProgress && renderedCount < totalCount && /* @__PURE__ */ jsx("div", { style: { textAlign: "center", padding: "8px 0" }, children: renderProgress(renderedCount, totalCount) }),