11import { type Table as TanstackTable , flexRender } from '@tanstack/react-table' ;
22import Link from 'next/link' ;
3+ import { useRouter } from 'next/navigation' ;
34import type * as React from 'react' ;
45
56import { getCommonPinningStyles } from '@/lib/data-table' ;
@@ -10,8 +11,8 @@ import { DataTablePagination } from './data-table-pagination';
1011interface DataTableProps < TData > extends React . ComponentProps < 'div' > {
1112 table : TanstackTable < TData > ;
1213 actionBar ?: React . ReactNode ;
13- getRowId : ( row : TData ) => string ;
14- rowClickBasePath : string ;
14+ getRowId ? : ( row : TData ) => string ;
15+ rowClickBasePath ? : string ;
1516 tableId ?: string ;
1617 onRowClick ?: ( row : TData ) => void ;
1718}
@@ -27,13 +28,21 @@ export function DataTable<TData>({
2728 onRowClick,
2829 ...props
2930} : DataTableProps < TData > ) {
31+ const router = useRouter ( ) ;
32+
3033 const handleRowClick = ( row : TData ) => {
3134 if ( onRowClick ) {
3235 onRowClick ( row ) ;
3336 }
37+ // This part of the handler will now only be used for non-link rows
38+ if ( getRowId && rowClickBasePath ) {
39+ const id = getRowId ( row ) ;
40+ router . push ( `${ rowClickBasePath } /${ id } ` ) ;
41+ }
3442 } ;
3543
3644 const filteredRows = table . getFilteredRowModel ( ) . rows ;
45+ const canBeLinks = getRowId && rowClickBasePath ;
3746
3847 return (
3948 < div className = { cn ( 'space-y-4' , className ) } { ...props } >
@@ -67,17 +76,17 @@ export function DataTable<TData>({
6776 </ TableHeader >
6877 < TableBody >
6978 { filteredRows . length ? (
70- filteredRows . map ( ( row ) => {
71- const id = getRowId ( row . original ) ;
72- const href = `${ rowClickBasePath } /${ id } ` ;
79+ filteredRows . map ( ( row ) => (
80+ < TableRow
81+ key = { row . id }
82+ data-state = { row . getIsSelected ( ) && 'selected' }
83+ className = { cn ( ( getRowId || onRowClick ) && 'hover:bg-muted/50 cursor-pointer' ) }
84+ onClick = { ! canBeLinks ? ( ) => handleRowClick ( row . original ) : undefined }
85+ >
86+ { row . getVisibleCells ( ) . map ( ( cell , index ) => {
87+ const href = canBeLinks ? `${ rowClickBasePath } /${ getRowId ( row . original ) } ` : '' ;
7388
74- return (
75- < TableRow
76- key = { row . id }
77- data-state = { row . getIsSelected ( ) && 'selected' }
78- className = "hover:bg-muted/50"
79- >
80- { row . getVisibleCells ( ) . map ( ( cell , index ) => (
89+ return (
8190 < TableCell
8291 key = { cell . id }
8392 className = { cn (
@@ -90,19 +99,23 @@ export function DataTable<TData>({
9099 } ) ,
91100 } }
92101 >
93- < Link
94- href = { href }
95- onClick = { ( ) => handleRowClick ( row . original ) }
96- className = "block w-full h-full"
97- style = { { textDecoration : 'none' , color : 'inherit' } }
98- >
99- { flexRender ( cell . column . columnDef . cell , cell . getContext ( ) ) }
100- </ Link >
102+ { canBeLinks ? (
103+ < Link
104+ href = { href }
105+ onClick = { ( ) => onRowClick && onRowClick ( row . original ) }
106+ className = "block"
107+ style = { { color : 'inherit' , textDecoration : 'none' } }
108+ >
109+ { flexRender ( cell . column . columnDef . cell , cell . getContext ( ) ) }
110+ </ Link >
111+ ) : (
112+ flexRender ( cell . column . columnDef . cell , cell . getContext ( ) )
113+ ) }
101114 </ TableCell >
102- ) ) }
103- </ TableRow >
104- ) ;
105- } )
115+ ) ;
116+ } ) }
117+ </ TableRow >
118+ ) )
106119 ) : (
107120 < TableRow >
108121 < TableCell
0 commit comments