1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| 'use client';
import React, { useState, useRef, useCallback, useMemo } from 'react';
const ROW_HEIGHT = 30; const VISIBLE_COUNT = 20;
function Page() { const data = useMemo( () => Array.from({ length: 200000 }, () => Array.from({ length: 4 }, () => Math.floor(Math.random() * 256)).join('.') ), [] );
const [startIndex, setStartIndex] = useState(0); const [editingIndex, setEditingIndex] = useState<number | null>(null); const [inputValue, setInputValue] = useState(''); const [searchTerm, setSearchTerm] = useState(''); const [searchResults, setSearchResults] = useState<number[]>([]); const [currentSearchIndex, setCurrentSearchIndex] = useState(0);
const containerRef = useRef<HTMLDivElement>(null);
const handleScroll = useCallback(() => { if (containerRef.current) { const scrollTop = containerRef.current.scrollTop; const newIndex = Math.floor(scrollTop / ROW_HEIGHT); setStartIndex(newIndex); } }, []);
const handleSearch = () => { const results = data .map((item, index) => (item.includes(searchTerm) ? index : -1)) .filter((index) => index !== -1); setSearchResults(results); setCurrentSearchIndex(0); if (results.length > 0) scrollToIndex(results[0]); };
const scrollToIndex = (index: number) => { if (containerRef.current) { containerRef.current.scrollTop = index * ROW_HEIGHT; setStartIndex(Math.floor(containerRef.current.scrollTop / ROW_HEIGHT)); } };
const handleNavigateSearch = (direction: 'prev' | 'next') => { if (searchResults.length === 0) return; const newIndex = direction === 'next' ? (currentSearchIndex + 1) % searchResults.length : (currentSearchIndex - 1 + searchResults.length) % searchResults.length; setCurrentSearchIndex(newIndex); scrollToIndex(searchResults[newIndex]); };
const saveEdit = useCallback( (index: number) => { if (editingIndex !== null) { data[editingIndex] = inputValue; setEditingIndex(null); setInputValue(''); } }, [editingIndex, inputValue, data] );
const renderRow = (index: number) => { const isEditing = index === editingIndex; const isSearchResult = searchResults.includes(index); const isCurrentResult = index === searchResults[currentSearchIndex];
return ( <div key={index} style={{ height: ROW_HEIGHT, display: 'flex', alignItems: 'center', padding: '0 10px', borderBottom: '1px solid #ddd', backgroundColor: isCurrentResult ? '#ffd700' : isSearchResult ? '#f0f8ff' : 'white', position: 'absolute', top: index * ROW_HEIGHT, width: '100%', }} onClick={() => { setEditingIndex(index); setInputValue(data[index]); }} > {isEditing ? ( <input value={inputValue} onChange={(e) => setInputValue(e.target.value)} onBlur={() => saveEdit(index)} style={{ width: '100%', height: '100%', border: 'none', outline: 'none' }} autoFocus /> ) : ( <span>{data[index]}</span> )} </div> ); };
return ( <div> <div style={{ marginBottom: 10 }}> <input type="text" value={searchTerm} onChange={(e) => setSearchTerm(e.target.value)} placeholder="搜索..." style={{ marginRight: 10, padding: '5px', width: '200px' }} /> <button onClick={handleSearch} style={{ marginRight: 10 }}> 搜索 </button> <button onClick={() => handleNavigateSearch('prev')} style={{ marginRight: 10 }}> 上一个 </button> <button onClick={() => handleNavigateSearch('next')}>下一个</button> </div> <div ref={containerRef} style={{ width: '100%', height: `${ROW_HEIGHT * VISIBLE_COUNT}px`, overflowY: 'auto', position: 'relative', border: '1px solid #ddd', }} onScroll={handleScroll} > {/* 虚拟容器,用于撑开滚动区域 */} <div style={{ height: `${data.length * ROW_HEIGHT}px`, position: 'relative' }}> {/* 渲染可见区域的行 */} {Array.from({ length: VISIBLE_COUNT + 1 }).map((_, i) => { const index = startIndex + i; if (index >= data.length) return null; return renderRow(index); })} </div> </div> </div> ); }
export default Page;
|