11"use client" ;
2- import React , { useCallback , useMemo , useRef , useState } from "react" ;
2+ import React , { memo , useCallback , useMemo , useRef , useState } from "react" ;
33import Box from "@mui/material/Box" ;
44import Table from "@mui/material/Table" ;
55import TableBody from "@mui/material/TableBody" ;
@@ -30,6 +30,24 @@ import { DataTableToolbar } from "./DataTableToolbar";
3030import { DataTableHeader } from "./DataTableHeader" ;
3131import type { ToolbarAction } from "./SplitActionButton" ;
3232
33+ // Static styles for virtualized row cells
34+ const checkboxCellStyle : React . CSSProperties = {
35+ position : "sticky" ,
36+ left : 0 ,
37+ zIndex : 1 ,
38+ } ;
39+
40+ const unpinnedCellStyle : React . CSSProperties = {
41+ overflow : "hidden" ,
42+ textOverflow : "ellipsis" ,
43+ whiteSpace : "nowrap" ,
44+ } ;
45+
46+ const paddingCellStyle : React . CSSProperties = {
47+ padding : 0 ,
48+ border : "none" ,
49+ } ;
50+
3351/**
3452 * Menu item
3553 */
@@ -76,7 +94,9 @@ export interface DataTableProps<T extends Record<string, unknown>> {
7694 *
7795 * @returns a DataTable component
7896 */
79- export function DataTable < T extends Record < string , unknown > > ( {
97+ export const DataTable = memo ( function DataTable <
98+ T extends Record < string , unknown > ,
99+ > ( {
80100 title,
81101 table,
82102 totalRows,
@@ -166,19 +186,16 @@ export function DataTable<T extends Record<string, unknown>>({
166186 [ table , checkboxWidth ] ,
167187 ) ;
168188
189+ const checkboxCellWithWidth = useMemo < React . CSSProperties > (
190+ ( ) => ( { ...checkboxCellStyle , width : checkboxWidth } ) ,
191+ [ checkboxWidth ] ,
192+ ) ;
193+
169194 const itemContent = useCallback (
170195 ( _index : number , row : Row < T > ) => (
171196 < >
172197 { ! disableCheckbox && (
173- < TableCell
174- padding = "checkbox"
175- style = { {
176- position : "sticky" ,
177- left : 0 ,
178- zIndex : 1 ,
179- width : checkboxWidth ,
180- } }
181- >
198+ < TableCell padding = "checkbox" style = { checkboxCellWithWidth } >
182199 < Checkbox
183200 size = "small"
184201 name = { `select-row-${ row . id } ` }
@@ -188,29 +205,33 @@ export function DataTable<T extends Record<string, unknown>>({
188205 />
189206 </ TableCell >
190207 ) }
191- { row . getVisibleCells ( ) . map ( ( cell ) => (
192- < TableCell
193- key = { cell . id }
194- style = { {
195- position : cell . column . getIsPinned ( ) ? "sticky" : "static" ,
196- left :
197- cell . column . getIsPinned ( ) === "left"
198- ? getLeftOffsetForColumn ( cell . column )
199- : undefined ,
200- right : cell . column . getIsPinned ( ) === "right" ? 0 : undefined ,
201- zIndex : cell . column . getIsPinned ( ) ? 1 : 0 ,
202- width : cell . column . getSize ( ) ,
203- overflow : "hidden" ,
204- textOverflow : "ellipsis" ,
205- whiteSpace : "nowrap" ,
206- } }
207- >
208- { flexRender ( cell . column . columnDef . cell , cell . getContext ( ) ) }
209- </ TableCell >
210- ) ) }
208+ { row . getVisibleCells ( ) . map ( ( cell ) => {
209+ const isPinned = cell . column . getIsPinned ( ) ;
210+ const style : React . CSSProperties = isPinned
211+ ? {
212+ ...unpinnedCellStyle ,
213+ position : "sticky" ,
214+ left :
215+ isPinned === "left"
216+ ? getLeftOffsetForColumn ( cell . column )
217+ : undefined ,
218+ right : isPinned === "right" ? 0 : undefined ,
219+ zIndex : 1 ,
220+ width : cell . column . getSize ( ) ,
221+ }
222+ : {
223+ ...unpinnedCellStyle ,
224+ width : cell . column . getSize ( ) ,
225+ } ;
226+ return (
227+ < TableCell key = { cell . id } style = { style } >
228+ { flexRender ( cell . column . columnDef . cell , cell . getContext ( ) ) }
229+ </ TableCell >
230+ ) ;
231+ } ) }
211232 </ >
212233 ) ,
213- [ disableCheckbox , checkboxWidth , getLeftOffsetForColumn ] ,
234+ [ disableCheckbox , checkboxCellWithWidth , getLeftOffsetForColumn ] ,
214235 ) ;
215236
216237 const selectedRows = table . getSelectedRowModel ( ) . rows ;
@@ -377,9 +398,8 @@ export function DataTable<T extends Record<string, unknown>>({
377398 < tr >
378399 < td
379400 style = { {
401+ ...paddingCellStyle ,
380402 height : rowVirtualizer . getVirtualItems ( ) [ 0 ] . start ,
381- padding : 0 ,
382- border : "none" ,
383403 } }
384404 />
385405 </ tr >
@@ -408,11 +428,10 @@ export function DataTable<T extends Record<string, unknown>>({
408428 < tr >
409429 < td
410430 style = { {
431+ ...paddingCellStyle ,
411432 height :
412433 rowVirtualizer . getTotalSize ( ) -
413434 ( rowVirtualizer . getVirtualItems ( ) . at ( - 1 ) ?. end ?? 0 ) ,
414- padding : 0 ,
415- border : "none" ,
416435 } }
417436 />
418437 </ tr >
@@ -464,4 +483,6 @@ export function DataTable<T extends Record<string, unknown>>({
464483 ) }
465484 </ Box >
466485 ) ;
467- }
486+ } ) as < T extends Record < string , unknown > > (
487+ props : DataTableProps < T > ,
488+ ) => React . ReactElement ;
0 commit comments