|
| 1 | +# @phphe/react-base-virtual-list |
| 2 | + |
| 3 | +[English](README.md) |
| 4 | + |
| 5 | +React 基础虚拟列表。[在线示例](TODO) |
| 6 | + |
| 7 | +## 特点 |
| 8 | + |
| 9 | +- 支持每项高度不同的列表。 |
| 10 | +- 简单易扩展,只含有常见功能。 |
| 11 | +- 高性能。针对每项高度不同的列表,不会获取每项的高度。 |
| 12 | + |
| 13 | +## 安装 |
| 14 | + |
| 15 | +```sh |
| 16 | +npm install @phphe/react-base-virtual-list --save |
| 17 | +``` |
| 18 | + |
| 19 | +## 使用 |
| 20 | + |
| 21 | +```tsx |
| 22 | +import { VirtualList } from "@phphe/react-base-virtual-list"; |
| 23 | + |
| 24 | +export default function BaseExample() { |
| 25 | + const exampleData = [ |
| 26 | + { |
| 27 | + headline: "in magna bibendum imperdiet", |
| 28 | + content: "Praesent blandit. Nam nulla.", |
| 29 | + }, |
| 30 | + { |
| 31 | + headline: "non velit donec diam", |
| 32 | + content: "Aenean fermentum.", |
| 33 | + }, |
| 34 | + ]; |
| 35 | + return ( |
| 36 | + <> |
| 37 | + <VirtualList |
| 38 | + items={exampleData} |
| 39 | + style={{ height: "600px", border: "1px solid #ccc", padding: "10px" }} |
| 40 | + renderItem={(item, index) => ( |
| 41 | + <div key={index} style={{ marginBottom: "10px" }}> |
| 42 | + <h3> |
| 43 | + {index}. {item.headline} |
| 44 | + </h3> |
| 45 | + <div> |
| 46 | + <div |
| 47 | + style={{ |
| 48 | + float: "left", |
| 49 | + width: "100px", |
| 50 | + height: "100px", |
| 51 | + background: "#f0f0f0", |
| 52 | + borderRadius: "5px", |
| 53 | + marginRight: "10px", |
| 54 | + }} |
| 55 | + ></div> |
| 56 | + {item.content} |
| 57 | + </div> |
| 58 | + </div> |
| 59 | + )} |
| 60 | + ></VirtualList> |
| 61 | + </> |
| 62 | + ); |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## props(必须的) |
| 67 | + |
| 68 | +- `items`: `Array`. 列表数据。 |
| 69 | +- `renderItem`: `(item, index: number) => ReactNode`. 列表每项的渲染函数。index 是列表项在整个列表中的索引。 |
| 70 | + |
| 71 | +## props(可选的) |
| 72 | + |
| 73 | +- `itemSize`: `number`. 列表单项的估计高度。 |
| 74 | +- `buffer`: `number`. 虚拟列表可见区域外额外渲染的空间。 |
| 75 | +- `persistentIndices`: `number[]`. 持久化渲染的项的索引数组。使对应索引的项持续渲染而不会因为在渲染区域外而被删除。你再使用 css 的`position:sticky`就可以使其黏着显示。 |
| 76 | +- `listSize`: `number`, 默认值: 1000. 列表的可见区域高度。仅用于 DOM 创建前使用,适用于 SSR. |
| 77 | +- `className`: `string`. 附加 css class 到根元素。 |
| 78 | +- `style`: `React.CSSProperties`. 附加 css style 到根元素。 |
| 79 | + |
| 80 | +## 暴露的方法 |
| 81 | + |
| 82 | +首先使用`ref`获取暴露的对象。 |
| 83 | + |
| 84 | +```tsx |
| 85 | +import { useRef } from "react"; |
| 86 | +import { VirtualList, VirtualListHandle } from "@phphe/react-base-virtual-list"; |
| 87 | + |
| 88 | +export default function BaseExample() { |
| 89 | + const ref = useRef<VirtualListHandle>(null); |
| 90 | + return ( |
| 91 | + <> |
| 92 | + <VirtualList ref={ref}></VirtualList> |
| 93 | + </> |
| 94 | + ); |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +上面代码省略了不相关的地方。`VirtualListHandle`是`typescript`类型,纯 js 请忽略。 |
| 99 | + |
| 100 | +`VirtualListHandle`类型代码。 |
| 101 | + |
| 102 | +```ts |
| 103 | +interface VirtualListHandle { |
| 104 | + scrollToIndex( |
| 105 | + index: number, |
| 106 | + block?: "start" | "end" | "center" | "nearest" |
| 107 | + ): void; |
| 108 | + forceUpdate(): void; |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +然后使用获取到的`ref`对象操作暴露的方法。 |
| 113 | + |
| 114 | +- `scrollToIndex`: `(index:number, block = 'start'):void`. 滚动到指定索引位置。`block`等于 HTML 原生方法`scrollIntoView`的`block`选项。 |
| 115 | +- `forceUpdate`: 强制重新渲染列表。可以再列表可见区域变换后调用此方法。 |
| 116 | + |
| 117 | +## 注意点 |
| 118 | + |
| 119 | +- 记得给列表设置高度。class, style, px, em, 百分比等都可以。 |
0 commit comments