|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * VirtualGrid Component |
| 11 | + * |
| 12 | + * Implements virtual scrolling using @tanstack/react-virtual for efficient |
| 13 | + * rendering of large datasets. Only renders visible rows, dramatically improving |
| 14 | + * performance with datasets of 1000+ items. |
| 15 | + * |
| 16 | + * Features: |
| 17 | + * - Virtual scrolling for rows |
| 18 | + * - Configurable row height |
| 19 | + * - Overscan for smooth scrolling |
| 20 | + * - Minimal DOM nodes (only visible items) |
| 21 | + */ |
| 22 | + |
| 23 | +import React, { useRef } from 'react'; |
| 24 | +import { useVirtualizer } from '@tanstack/react-virtual'; |
| 25 | + |
| 26 | +export interface VirtualGridColumn { |
| 27 | + header: string; |
| 28 | + accessorKey: string; |
| 29 | + cell?: (value: any, row: any) => React.ReactNode; |
| 30 | + width?: number | string; |
| 31 | + align?: 'left' | 'center' | 'right'; |
| 32 | +} |
| 33 | + |
| 34 | +export interface VirtualGridProps { |
| 35 | + data: any[]; |
| 36 | + columns: VirtualGridColumn[]; |
| 37 | + rowHeight?: number; |
| 38 | + className?: string; |
| 39 | + headerClassName?: string; |
| 40 | + rowClassName?: string | ((row: any, index: number) => string); |
| 41 | + onRowClick?: (row: any, index: number) => void; |
| 42 | + overscan?: number; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Virtual scrolling grid component |
| 47 | + * |
| 48 | + * @example |
| 49 | + * ```tsx |
| 50 | + * <VirtualGrid |
| 51 | + * data={items} |
| 52 | + * columns={[ |
| 53 | + * { header: 'Name', accessorKey: 'name' }, |
| 54 | + * { header: 'Age', accessorKey: 'age' }, |
| 55 | + * ]} |
| 56 | + * rowHeight={40} |
| 57 | + * /> |
| 58 | + * ``` |
| 59 | + */ |
| 60 | +export const VirtualGrid: React.FC<VirtualGridProps> = ({ |
| 61 | + data, |
| 62 | + columns, |
| 63 | + rowHeight = 40, |
| 64 | + className = '', |
| 65 | + headerClassName = '', |
| 66 | + rowClassName, |
| 67 | + onRowClick, |
| 68 | + overscan = 5, |
| 69 | +}) => { |
| 70 | + const parentRef = useRef<HTMLDivElement>(null); |
| 71 | + |
| 72 | + const virtualizer = useVirtualizer({ |
| 73 | + count: data.length, |
| 74 | + getScrollElement: () => parentRef.current, |
| 75 | + estimateSize: () => rowHeight, |
| 76 | + overscan, |
| 77 | + }); |
| 78 | + |
| 79 | + const items = virtualizer.getVirtualItems(); |
| 80 | + |
| 81 | + return ( |
| 82 | + <div className={className}> |
| 83 | + {/* Header */} |
| 84 | + <div |
| 85 | + className={`grid border-b sticky top-0 bg-background z-10 ${headerClassName}`} |
| 86 | + style={{ |
| 87 | + gridTemplateColumns: columns |
| 88 | + .map((col) => col.width || '1fr') |
| 89 | + .join(' '), |
| 90 | + }} |
| 91 | + > |
| 92 | + {columns.map((column, index) => ( |
| 93 | + <div |
| 94 | + key={index} |
| 95 | + className={`px-4 py-2 font-semibold text-sm text-${column.align || 'left'}`} |
| 96 | + > |
| 97 | + {column.header} |
| 98 | + </div> |
| 99 | + ))} |
| 100 | + </div> |
| 101 | + |
| 102 | + {/* Virtual scrolling container */} |
| 103 | + <div |
| 104 | + ref={parentRef} |
| 105 | + className="h-[600px] overflow-auto" |
| 106 | + style={{ contain: 'strict' }} |
| 107 | + > |
| 108 | + <div |
| 109 | + style={{ |
| 110 | + height: `${virtualizer.getTotalSize()}px`, |
| 111 | + width: '100%', |
| 112 | + position: 'relative', |
| 113 | + }} |
| 114 | + > |
| 115 | + {items.map((virtualRow) => { |
| 116 | + const row = data[virtualRow.index]; |
| 117 | + const rowClasses = |
| 118 | + typeof rowClassName === 'function' |
| 119 | + ? rowClassName(row, virtualRow.index) |
| 120 | + : rowClassName || ''; |
| 121 | + |
| 122 | + return ( |
| 123 | + <div |
| 124 | + key={virtualRow.key} |
| 125 | + className={`grid border-b hover:bg-muted/50 cursor-pointer ${rowClasses}`} |
| 126 | + style={{ |
| 127 | + position: 'absolute', |
| 128 | + top: 0, |
| 129 | + left: 0, |
| 130 | + width: '100%', |
| 131 | + height: `${virtualRow.size}px`, |
| 132 | + transform: `translateY(${virtualRow.start}px)`, |
| 133 | + gridTemplateColumns: columns |
| 134 | + .map((col) => col.width || '1fr') |
| 135 | + .join(' '), |
| 136 | + }} |
| 137 | + onClick={() => onRowClick?.(row, virtualRow.index)} |
| 138 | + > |
| 139 | + {columns.map((column, colIndex) => { |
| 140 | + const value = row[column.accessorKey]; |
| 141 | + const cellContent = column.cell |
| 142 | + ? column.cell(value, row) |
| 143 | + : value; |
| 144 | + |
| 145 | + return ( |
| 146 | + <div |
| 147 | + key={colIndex} |
| 148 | + className={`px-4 py-2 text-sm flex items-center text-${column.align || 'left'}`} |
| 149 | + > |
| 150 | + {cellContent} |
| 151 | + </div> |
| 152 | + ); |
| 153 | + })} |
| 154 | + </div> |
| 155 | + ); |
| 156 | + })} |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + |
| 160 | + {/* Footer info */} |
| 161 | + <div className="px-4 py-2 text-xs text-muted-foreground border-t"> |
| 162 | + Showing {items.length} of {data.length} rows (virtual scrolling enabled) |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + ); |
| 166 | +}; |
0 commit comments