Skip to content

Commit a005094

Browse files
committed
docs: add Chinese docs
1 parent 5d4a8e9 commit a005094

File tree

2 files changed

+122
-28
lines changed

2 files changed

+122
-28
lines changed

README.md

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,5 @@
1-
# React + TypeScript + Vite
1+
# @phphe/react-base-virtual-list
22

3-
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
3+
[中文](README_CN.md)
44

5-
Currently, two official plugins are available:
6-
7-
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8-
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
9-
10-
## Expanding the ESLint configuration
11-
12-
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:
13-
14-
- Configure the top-level `parserOptions` property like this:
15-
16-
```js
17-
export default {
18-
// other rules...
19-
parserOptions: {
20-
ecmaVersion: 'latest',
21-
sourceType: 'module',
22-
project: ['./tsconfig.json', './tsconfig.node.json'],
23-
tsconfigRootDir: __dirname,
24-
},
25-
}
26-
```
27-
28-
- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
29-
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
30-
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list
5+
TODO

README_CN.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)