当前位置: 首页 > news >正文

antd5 虚拟列表原理(rc-virtual-list)

github:https://github.com/react-component/virtual-list

rc-virtual-list 版本 3.11.4(2024-02-01)
版本:virtual-list-3.11.4

在这里插入图片描述

Development

npm install
npm start
open http://localhost:8000/

在这里插入图片描述

List 组件接收 Props

PropDescriptionTypeDefault
childrenRender props of item(item, index, props) => ReactElement-
componentCustomize List dom elementstring | Componentdiv
dataData listArray-
disabledDisable scroll check. Usually used on animation controlbooleanfalse
heightList heightnumber-
itemHeightItem minium heightnumber-
itemKeyMatch key with itemstring-
stylesstyle{ horizontalScrollBar?: React.CSSProperties; horizontalScrollBarThumb?: React.CSSProperties; verticalScrollBar?: React.CSSProperties; verticalScrollBarThumb?: React.CSSProperties; }-

组件解析

import ResizeObserver from "rc-resize-observer";const onHolderResize: ResizeObserverProps["onResize"] = (sizeInfo) => {console.log("sizeInfo", sizeInfo);setSize({width: sizeInfo.width || sizeInfo.offsetWidth,height: sizeInfo.height || sizeInfo.offsetHeight,});
};// 用于监听dom节点resize时返回dom节点信息
<ResizeObserver onResize={onHolderResize}></ResizeObserver>;

打印的 sizeInfo

{height: 200,//可视区高度offsetHeight: 200,offsetWidth: 606,width: 606,//可视区宽度
}
 //component: Component = 'div',
// Component默认是div标签 ,className为rc-virtual-list-holder, 是虚拟列表的可视化区域
<ComponentclassName={`${prefixCls}-holder`}style={componentStyle}ref={componentRef}onScroll={onFallbackScroll}onMouseEnter={delayHideScrollBar}>

componentStyle 计算,是一个 styles 对象 React.CSSProperties

const ScrollStyle: React.CSSProperties = {overflowY: "auto",overflowAnchor: "none",
};// useVirtual: 是否虚拟列表(属性virtual为true 并且height和itemHeight有值)
const useVirtual = !!(virtual !== false && height && itemHeight);let componentStyle: React.CSSProperties = null;
if (height) {componentStyle = {[fullHeight ? "height" : "maxHeight"]: height,...ScrollStyle,};if (useVirtual) {componentStyle.overflowY = "hidden";if (scrollWidth) {componentStyle.overflowX = "hidden";}if (scrollMoving) {componentStyle.pointerEvents = "none";}}
}

overflow-anchor CSS 属性提供一种退出浏览器滚动锚定行为的方法,该行为会调整滚动位置以最大程度地减少内容偏移。
默认情况下,在任何支持滚动锚定行为的浏览器中都将其启用。因此,仅当你在文档或文档的一部分中遇到滚动锚定问题并且需要关闭行为时,才通常需要更改此属性的值。

内容组件
import Filler from ‘./Filler’;

<FillerprefixCls={prefixCls}height={scrollHeight}offsetX={offsetLeft}offsetY={fillerOffset}scrollWidth={scrollWidth}onInnerResize={collectHeight}ref={fillerInnerRef}innerProps={innerProps}rtl={isRTL}extra={extraContent}
>{listChildren}
</Filler>

Filler 组件

<div style={outerStyle}><ResizeObserveronResize={({ offsetHeight }) => {if (offsetHeight && onInnerResize) {onInnerResize();}}}><divstyle={innerStyle}className={classNames({[`${prefixCls}-holder-inner`]: prefixCls,})}ref={ref}{...innerProps}>{children}{extra}</div></ResizeObserver>
</div>

demo 查看渲染内容

在这里插入图片描述

outStyle 计算:

let outerStyle: React.CSSProperties = {};if (offsetY !== undefined) {// Not set `width` since this will break `sticky: right`outerStyle = {height,position: "relative",overflow: "hidden",};
}

innerStyle 计算

let innerStyle: React.CSSProperties = {display: "flex",flexDirection: "column",
};
if (offsetY !== undefined) {innerStyle = {...innerStyle,transform: `translateY(${offsetY}px)`,[rtl ? "marginRight" : "marginLeft"]: -offsetX,position: "absolute",left: 0,right: 0,top: 0,};
}

可以看到最终渲染的元素,有下面几个容器组成:

列表容器:rc-virtual-list
列表内容容器:rc-virtual-list-holder

要点:
Component 组件,默认 div:固定高度,超出部分隐藏,最终也是通过控制该容器的滚动高度来达到元素滚动的目的

div(outStyle):高度为所有列表内容都渲染出来的高度,这里是为了撑开父元素,实现父元素的滚动
渲染列表容器:rc-virtual-list-holder-inner
单个列表内容:item

listChildren

const listChildren = useChildren(mergedData, //列表数据start, //渲染第一个元素的索引end, //渲染最后一个元素的索引scrollWidth,setInstanceRef, //获取元素children,sharedConfig
);

useChildren 主要是进行 list 列表的渲染,而在渲染列表时,又用 Item 组件进行了一层包裹.

export default function useChildren<T>(list: T[],startIndex: number,endIndex: number,scrollWidth: number,setNodeRef: (item: T, element: HTMLElement) => void,renderFunc: RenderFunc<T>,{ getKey }: SharedConfig<T>
) {return list.slice(startIndex, endIndex + 1).map((item, index) => {const eleIndex = startIndex + index;const node = renderFunc(item, eleIndex, {style: {width: scrollWidth,},}) as React.ReactElement;const key = getKey(item);return (<Item key={key} setRef={(ele) => setNodeRef(item, ele)}>{node}</Item>);});
}

Item 组件
用 Item 组件包裹了外部传入的列表元素的 JSXElement

export interface ItemProps {children: React.ReactElement;setRef: (element: HTMLElement) => void;
}export function Item({ children, setRef }: ItemProps) {const refFunc = React.useCallback((node) => {setRef(node);}, []);return React.cloneElement(children, {ref: refFunc,});
}

经过这么一层包装,当通过 ref 获取子节点时,将会调用 refFunc -> setRef -> setInstanceRef。这也是为什么当元素高度可变时需要用 React.forwardRef 进行列表元素的包裹

滚动条组件

<ScrollBarref={verticalScrollBarRef}prefixCls={prefixCls}scrollOffset={offsetTop}scrollRange={scrollHeight}rtl={isRTL}onScroll={onScrollBar} //滚动事件onStartMove={onScrollbarStartMove} //开始滚动事件onStopMove={onScrollbarStopMove} //滚动结束事件spinSize={verticalScrollBarSpinSize}containerSize={size.height}style={styles?.verticalScrollBar}thumbStyle={styles?.verticalScrollBarThumb}
/>

ScrollBar 渲染

<divref={scrollbarRef}className={classNames(scrollbarPrefixCls, {[`${scrollbarPrefixCls}-horizontal`]: horizontal,[`${scrollbarPrefixCls}-vertical`]: !horizontal,[`${scrollbarPrefixCls}-visible`]: visible,})}style={{ ...containerStyle, ...style }}onMouseDown={onContainerMouseDown}onMouseMove={delayHidden}
><divref={thumbRef}className={classNames(`${scrollbarPrefixCls}-thumb`, {[`${scrollbarPrefixCls}-thumb-moving`]: dragging,})}style={{ ...thumbStyle, ...propsThumbStyle }}onMouseDown={onThumbMouseDown}/>
</div>

通过滚动条组件滚动事件

//newScrollOffset 滚动的距离,horizontal是否水平滚动方向
function onScrollBar(newScrollOffset: number, horizontal?: boolean) {const newOffset = newScrollOffset;if (horizontal) {flushSync(() => {setOffsetLeft(newOffset);});triggerScroll();} else {syncScrollTop(newOffset);}
}

滚动条开始滚动事件和滚动结束事件

// 滚动开始事件
const onScrollbarStartMove = () => {console.log("----start-----");setScrollMoving(true);
};//滚动结束事件
const onScrollbarStopMove = () => {console.log("-----end");setScrollMoving(false);
};

注意点

  • 如果子项存在动态高度或者高度不统一的情况,需要使用 React.forwardRef 转发 ref 给子 DOM 元素。
  • 列表项之间不要存在上下间距( margin-top 、 margin-bottom )。
    以上两点如果没有做到,调用组件的 scrollTo(scrollConfig) 方法进行滚动时都会导致滚动位置异常

相关文章:

  • [剪藏] - 由哇哈哈和农夫山泉所想到的
  • OpenCV 图像重映射函数remap()实例详解
  • 游戏开发中的坑之十三 Lut贴图相关问题
  • 数据治理——滴滴大数据成本治理实践
  • ChromeDriver 122 版本为例 国内下载地址及安装教程
  • java零基础入门-Map遍历
  • 通过xlsx库解析读取excel表格内容
  • 现代化的轻量级Redis桌面客户端Tiny RDM
  • 加速 PyTorch 模型预测常见方法梳理
  • 《系统架构设计师教程(第2版)》第6章-据库设计基础知识-03-关系数据库设计
  • Springboot 使用【过滤器】实现在请求到达 Controller 之前修改请求体参数和在结果返回之前修改响应体
  • 14、Linux-Shell03:echo、printf和test命令
  • CSAPP Malloc lab
  • 【LLMs+小羊驼】23.03.Vicuna: 类似GPT4的开源聊天机器人( 90%* ChatGPT Quality)
  • Python (用户登录、身份归属地查询添加异常处理、绘制多角星、电影信息提取)
  • 【译】JS基础算法脚本:字符串结尾
  • $translatePartialLoader加载失败及解决方式
  • Android Volley源码解析
  • IndexedDB
  • input的行数自动增减
  • Java编程基础24——递归练习
  • Java超时控制的实现
  • java架构面试锦集:开源框架+并发+数据结构+大企必备面试题
  • niucms就是以城市为分割单位,在上面 小区/乡村/同城论坛+58+团购
  • RxJS: 简单入门
  • scala基础语法(二)
  • SpingCloudBus整合RabbitMQ
  • SQLServer之创建显式事务
  • Swoft 源码剖析 - 代码自动更新机制
  • 笨办法学C 练习34:动态数组
  • 彻底搞懂浏览器Event-loop
  • 基于阿里云移动推送的移动应用推送模式最佳实践
  • 免费小说阅读小程序
  • 如何进阶一名有竞争力的程序员?
  • 实现菜单下拉伸展折叠效果demo
  • 微信小程序开发问题汇总
  • 一个完整Java Web项目背后的密码
  • #pragma once与条件编译
  • ${factoryList }后面有空格不影响
  • (1)常见O(n^2)排序算法解析
  • (3)(3.5) 遥测无线电区域条例
  • (k8s中)docker netty OOM问题记录
  • (理论篇)httpmoudle和httphandler一览
  • (论文阅读11/100)Fast R-CNN
  • (排序详解之 堆排序)
  • (一) storm的集群安装与配置
  • (转) ns2/nam与nam实现相关的文件
  • (转)ABI是什么
  • (转载)跟我一起学习VIM - The Life Changing Editor
  • .java 9 找不到符号_java找不到符号
  • .net CHARTING图表控件下载地址
  • .net core 6 redis操作类
  • .Net MVC + EF搭建学生管理系统
  • .net oracle 连接超时_Mysql连接数据库异常汇总【必收藏】
  • .NET 将混合了多个不同平台(Windows Mac Linux)的文件 目录的路径格式化成同一个平台下的路径