Skip to content

Commit c22609a

Browse files
authored
fix(useVirtualScroll): optimize visibleData rendering logic (#3863)
* fix: simplify virtual scroll condition checks * docs(list): remove unused sections * chore(changelog): correct formatting * chore(useVirtualScroll): rename * chore: update demo * chore: correct typos * chore: update CHANGELOG.md * chore: typo * chore: update CHANGELOG.md
1 parent 8a14d74 commit c22609a

File tree

9 files changed

+22
-39
lines changed

9 files changed

+22
-39
lines changed

packages/components/hooks/useVirtualScroll.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,8 @@ const useVirtualScroll = (container: MutableRefObject<HTMLElement>, params: UseV
212212
// 如果之前存在滚动,基于原先数据计算位置
213213
const currentScrollTop = container.current?.scrollTop || 0;
214214
let currentIndex = Math.floor(currentScrollTop / tScroll.rowHeight);
215-
const prevScrollTopHeightList = trScrollTopHeightList.current;
216-
for (let i = 0; i < prevScrollTopHeightList?.length; i++) {
217-
if (prevScrollTopHeightList[i] >= currentScrollTop) {
215+
for (let i = 0; i < scrollTopHeightList?.length; i++) {
216+
if (scrollTopHeightList[i] >= currentScrollTop) {
218217
currentIndex = i;
219218
break;
220219
}
@@ -226,8 +225,8 @@ const useVirtualScroll = (container: MutableRefObject<HTMLElement>, params: UseV
226225

227226
let translateY = startIndex * tScroll.rowHeight;
228227

229-
if (prevScrollTopHeightList?.length > 0 && startIndex > 0) {
230-
const prevHeight = prevScrollTopHeightList[Math.min(startIndex - 1, prevScrollTopHeightList.length - 1)] || 0;
228+
if (scrollTopHeightList?.length > 0 && startIndex > 0) {
229+
const prevHeight = scrollTopHeightList[Math.min(startIndex - 1, scrollTopHeightList.length - 1)] || 0;
231230
translateY = Math.max(0, prevHeight);
232231
}
233232

packages/components/list/List.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const List = forwardRefWithStatics(
101101

102102
const renderContent = () => (
103103
<>
104-
{isVirtualScroll && virtualConfig.visibleData.length ? (
104+
{isVirtualScroll ? (
105105
<>
106106
<div style={cursorStyle}></div>
107107
<ul className={`${COMPONENT_NAME}__inner`} style={listStyle}>

packages/components/list/_example/scroll.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { List } from 'tdesign-react';
33

44
import type { ListProps } from 'tdesign-react';
@@ -9,16 +9,16 @@ export default function BasicList() {
99
const [listData, setListData] = useState([]);
1010
const [isLoading, setIsLoading] = useState(false);
1111
const [pageNum, setPageNum] = useState(1);
12-
const pageSize = 5;
1312

1413
const dataSource: any = [];
15-
const total = 30;
14+
const total = 3000;
15+
const pageSize = 50;
1616
for (let i = 0; i < total; i++) {
1717
dataSource.push({
1818
id: i,
1919
content: '列表内容列表内容列表内容',
2020
icon: 'https://tdesign.gtimg.com/list-icon.png',
21-
title: '列表主内容',
21+
title: `列表标题 ${i + 1}`,
2222
});
2323
}
2424

packages/components/list/list.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
:: BASE_DOC ::
22

3-
### 斑马纹区分列表
4-
5-
{{ stripe }}
6-
7-
### 异步加载的列表
8-
9-
{{ asyncLoading }}
10-
11-
### 带头部及尾部的列表
12-
13-
{{ header-footer }}
14-
15-
### 带滚动事件的列表
16-
17-
{{ scroll }}
18-
193
## API
204
### List Props
215

packages/components/menu/SubMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ const SubTitleMenu: FC<SubMenuWithCustomizeProps> = (props) => {
193193

194194
useRipple(subMenuDom);
195195

196-
// pupup 导航
196+
// popup 导航
197197
const isPopUp = expandType === 'popup';
198198
// 当前二级导航激活
199199
const isActive = checkSubMenuChildrenActive(children, active) || active === value;

packages/components/select/base/PopupContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ const PopupContent = React.forwardRef<HTMLDivElement, SelectPopupProps>((props,
234234
{!loading && !isEmpty && renderOptions(renderedOptions)}
235235
</div>
236236
);
237-
if (isVirtual && visibleData.length) {
237+
if (isVirtual) {
238238
return (
239239
<>
240240
{panelTopContent}

packages/components/table/_example/filter-controlled.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ const columns: TableProps['columns'] = [
102102
// eslint-disable-next-line
103103
// function IconText(props = {}) {
104104
// // 根据不同的 Props,允许定义不同的筛选图标(col, colIndex 在 Table 组件内部已经注入)
105-
// const { col, colIndx } = props;
106-
// console.log(col, colIndx);
105+
// const { col, colIndex } = props;
106+
// console.log(col, colIndex);
107107
// if (col.colKey === 'email') return <div>EmailIcon</div>;
108108
// return <i>Icon</i>;
109109
// }

packages/components/tree/__tests__/tree.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ describe('Tree test', () => {
246246
],
247247
},
248248
];
249-
it('wehen props.line is customized, it works fine', async () => {
249+
it('when props.line is customized, it works fine', async () => {
250250
const { container } = await renderTreeWithProps({
251251
data,
252252
expandAll: true,
@@ -256,7 +256,7 @@ describe('Tree test', () => {
256256
expect(container.querySelectorAll('.custom-line').length).toBe(6);
257257
});
258258

259-
it('wehen props.line is a customized function, it works fine', async () => {
259+
it('when props.line is a customized function, it works fine', async () => {
260260
const line: any = () => <span className="custom-line"></span>;
261261
const { container } = await renderTreeWithProps({
262262
data,

packages/tdesign-react/CHANGELOG.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ spline: explain
77

88
## 🌈 1.15.3 `2025-09-29`
99
### 🐞 Bug Fixes
10-
- `Select`: 修复 OptionGroup 的 style 与 className 没有生效的问题 @uyarn ([#3855](https://github.com/Tencent/tdesign-react/pull/3855))
10+
- `Select`: 修复 `OptionGroup``style``className` 没有生效的问题 @uyarn ([#3855](https://github.com/Tencent/tdesign-react/pull/3855))
1111

1212
## 🌈 1.15.2 `2025-09-29`
1313
### 🚀 Features
@@ -19,11 +19,11 @@ spline: explain
1919
- `TagInput`: 修复 `onBlur` 中的 `inputValue` 始终为空的问题 @RylanBot ([#3841](https://github.com/Tencent/tdesign-react/pull/3841))
2020
- `Cascader`: 修复 `single` 模式下,选中唯一的子节点时,父节点意外被高亮的问题 @RylanBot ([#3840](https://github.com/Tencent/tdesign-react/pull/3840))
2121
- `DateRangePickerPanel`: 修复 `preset` 涉及跨年份的日期时,点击面板后无法同步的问题 @RylanBot ([#3818](https://github.com/Tencent/tdesign-react/pull/3818))
22-
- `EnhancedTable`: 修复节点拖拽后,再点击展开时,位置被重置的问题 ([#3780](https://github.com/Tencent/tdesign-react/pull/3780))
22+
- `EnhancedTable`: 修复节点拖拽后,再点击展开时,位置被重置的问题 @RylanBot ([#3780](https://github.com/Tencent/tdesign-react/pull/3780))
2323
- `Table`: @RylanBot
2424
- 修复开启 `multipleSort` 但没有声明 `sort``defaultSort` 时,`onSortChange` 始终返回 `undefined` 的问题 ([#3824](https://github.com/Tencent/tdesign-react/pull/3824))
25-
- 修复同时开启虚拟滚动与设置 `firstFullRow` / `lastFullRow` 等情况时,最后一行内容被遮挡的问题 ([#3792](https://github.com/Tencent/tdesign-react/pull/3792))
26-
- 修复 `fixedRows` / `firstFullRow` / `lastFullRow` 无法在虚拟滚动下组合使用的问题 ([#3792](https://github.com/Tencent/tdesign-react/pull/3792))
25+
- 修复同时开启虚拟滚动与设置 `firstFullRow` / `lastFullRow` 等情况时,最后一行内容被遮挡的问题 ([#3792](https://github.com/Tencent/tdesign-react/pull/3792))
26+
- 修复 `fixedRows` / `firstFullRow` / `lastFullRow` 无法在虚拟滚动下组合使用的问题 ([#3792](https://github.com/Tencent/tdesign-react/pull/3792))
2727
- 修复虚拟滚动初始化时滚动条长度异常的问题 ([#3792](https://github.com/Tencent/tdesign-react/pull/3792))
2828
- 修复固定表头与固定列无法对齐的问题 ([#3792](https://github.com/Tencent/tdesign-react/pull/3792))
2929
- 修复 `pagination` 为非受控时,必须声明 `defaultCurrent` 才能正确分页的问题 ([#3822](https://github.com/Tencent/tdesign-react/pull/3822))
@@ -33,12 +33,12 @@ spline: explain
3333
- 修复自定义 `popupVisible={false}` 时,`onBlur` 不生效的问题
3434
- 修复开启 `multiple` 时,`onBlur` 缺少 `tagInputValue` 参数的问题
3535
- `Select`:
36-
- 修复使用 `keys` 配置 `content` 作为 `label``value` 无法生效的问题 @RylanBot @uyarn ([#3829](https://github.com/Tencent/tdesign-react/pull/3829))
37-
- 修复动态切换到虚拟滚动时,出现白屏和滚动条被意外重置的问题 @RylanBot ([#3836](https://github.com/Tencent/tdesign-react/pull/3836))
36+
- 修复使用 `keys` 配置 `content` 作为 `label``value` 无法生效的问题 @RylanBot @uyarn ([#3829](https://github.com/Tencent/tdesign-react/pull/3829))
37+
- 修复动态切换到虚拟滚动时,出现白屏和滚动条被意外重置的问题 @RylanBot ([#3792](https://github.com/Tencent/tdesign-react/pull/3792)) ([#3836](https://github.com/Tencent/tdesign-react/pull/3836))
3838
- 修复开启虚拟滚动且动态更新数据,展示数据不同步的问题 @huangchen1031 ([#3839](https://github.com/Tencent/tdesign-react/pull/3839))
3939
- `List`:
4040
- 修复开启虚拟滚动后,`ListItem` 的部分 API 无法生效的问题 @FlowerBlackG ([#3835](https://github.com/Tencent/tdesign-react/pull/3835))
41-
- 修复动态切换到虚拟滚动时,滚动条被意外重置的问题 @RylanBot ([#3836](https://github.com/Tencent/tdesign-react/pull/3836))
41+
- 修复动态切换到虚拟滚动时,滚动条被意外重置的问题 @RylanBot ([#3792](https://github.com/Tencent/tdesign-react/pull/3792)) ([#3836](https://github.com/Tencent/tdesign-react/pull/3836))
4242

4343
## 🌈 1.15.1 `2025-09-12`
4444
### 🐞 Bug Fixes

0 commit comments

Comments
 (0)