Skip to content

Commit 05c43de

Browse files
committed
chore: add example
1 parent 33fd8c8 commit 05c43de

File tree

6 files changed

+4029
-30
lines changed

6 files changed

+4029
-30
lines changed

src/App.css

Whitespace-only changes.

src/App.tsx

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
import { useMemo } from 'react'
2-
import './App.css'
3-
import { VirtualList, } from './components/VirtualList'
2+
import { VirtualList, } from './VirtualList'
3+
import exampleData from './example_data.json'
44

55
function App() {
6-
const items = useMemo(
7-
() =>
8-
new Array(1000).fill(1).map((v, i) => ({
9-
id: i,
10-
text: 'Item ' + i,
11-
lineHeight: 20 + (i % 20) + 'px',
12-
width: 100 + (i % 30) + 'px',
13-
})),
14-
[],
15-
)
16-
17-
const styles = {
18-
node: {
19-
border: '1px solid #ccc',
20-
},
21-
}
226
return (
23-
<div>
7+
<>
8+
<b>@phphe/react-base-virtual-list</b> on <a href="https://github.com/phphe/react-base-virtual-list">Github</a>
249
<div>
25-
<h1>vertical</h1>
26-
<VirtualList
27-
items={items}
28-
style={{ height: '600px' }}
29-
></VirtualList>
30-
</div>
31-
</div>
10+
<h1>Virtual List Demo</h1>
11+
<ul>
12+
<li>Dynamic, the list items are not the same height.</li>
13+
<li>1000 items in the demo.</li>
14+
</ul>
15+
<div>
16+
<VirtualList
17+
items={exampleData}
18+
style={{ height: '600px', width: '600px', border: '1px solid #ccc', padding: '10px' }}
19+
renderItem={(item, index) => <div key={index} style={{ marginBottom: '10px' }}>
20+
<h3>{index}. {item.headline}</h3>
21+
<div>
22+
<div style={{ float: 'left', width: '100px', height: '100px', background: '#f0f0f0', borderRadius: '5px', marginRight: '10px' }}></div>
23+
{item.content}
24+
</div>
25+
</div>}
26+
></VirtualList><a href="https://github.com/phphe/react-base-virtual-list/blob/main/src/App.tsx">Source Code</a>
27+
</div>
28+
</div >
29+
</>
3230
)
3331
}
3432

src/components/VirtualList.tsx renamed to src/VirtualList.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import React, {
1212
export function VirtualList(
1313
props,
1414
) {
15-
const buffer = props.buffer || 100
1615
const [itemSize, setitemSize] = useState(props.itemSize || 100);
16+
const buffer = useMemo(() => props.buffer || Math.max(itemSize * 5, 100), [props.buffer, itemSize]);
1717
const count = props.items.length
1818
const list = useRef(null);
1919
const listInner = useRef(null);
@@ -71,9 +71,9 @@ export function VirtualList(
7171
}
7272
}
7373
//
74-
return <div ref={list} onScroll={handleScroll} style={{ height: '500px', overflow: 'auto', }}>
74+
return <div ref={list} onScroll={handleScroll} className={props.className} style={{ overflow: 'auto', ...props.style }}>
7575
<div ref={listInner} style={{ display: 'flex', flexDirection: 'column', ...listInnerStyle }}>
76-
{visible.map((item, i) => <div key={item.id}>{item.text}</div>)}
76+
{visible.map((item, i) => props.renderItem(item, i + startIndex))}
7777
</div>
7878
</div>
7979
}

0 commit comments

Comments
 (0)