@@ -26,13 +26,16 @@ import {
26
26
IconButton ,
27
27
Tooltip ,
28
28
FeedbackProps ,
29
+ vars ,
29
30
} from ".." ;
30
31
import {
31
- cellContainer ,
32
+ cellContainerRecipe ,
32
33
columnHeader ,
33
34
lastLeftStickyColumn ,
35
+ rowContainer ,
34
36
sectionHeader ,
35
37
sectionHeaderContainer ,
38
+ selectedRowBackgroundColor ,
36
39
sortIconContainer ,
37
40
stickyColumnHeader ,
38
41
table ,
@@ -42,6 +45,7 @@ import { useLayoutEffect, useMemo, useState, CSSProperties, useEffect } from "re
42
45
import { IconHelp , IconInfo } from "../Icons" ;
43
46
import { match , __ } from "ts-pattern" ;
44
47
import { useBentoConfig } from "../BentoConfigContext" ;
48
+ import { assignInlineVars } from "@vanilla-extract/dynamic" ;
45
49
46
50
type SortFn < C extends ReadonlyArray < ColumnType < string , { } , any > > > = (
47
51
a : Row < RowType < C > > ,
@@ -80,6 +84,7 @@ type Props<C extends ReadonlyArray<ColumnType<string, {}, any>>> = {
80
84
initialSorting ?: Array < SortingRule < C > > ;
81
85
stickyHeaders ?: boolean ;
82
86
height ?: { custom : string | number } ;
87
+ onRowPress ?: ( row : Row < RowType < C > > ) => void ;
83
88
} & SortingProps < C > ;
84
89
85
90
/**
@@ -113,6 +118,7 @@ export function Table<C extends ReadonlyArray<ColumnType<string, {}, any>>>({
113
118
initialSorting,
114
119
stickyHeaders,
115
120
height,
121
+ onRowPress,
116
122
} : Props < C > ) {
117
123
const config = useBentoConfig ( ) . table ;
118
124
const customOrderByFn = useMemo (
@@ -273,7 +279,11 @@ export function Table<C extends ReadonlyArray<ColumnType<string, {}, any>>>({
273
279
. map ( ( { gridWidth = "fit-content" } ) => gridWidthStyle ( gridWidth ) )
274
280
. join ( " " ) ;
275
281
276
- function renderCells < D extends Record < string , unknown > > ( cells : Array < Cell < D > > , rowIndex : number ) {
282
+ function renderCells < D extends Record < string , unknown > > (
283
+ cells : Array < Cell < D > > ,
284
+ rowIndex : number ,
285
+ interactiveRow : boolean
286
+ ) {
277
287
return cells . map ( ( cell , index ) => (
278
288
< CellContainer
279
289
{ ...cell . getCellProps ( ) }
@@ -282,6 +292,7 @@ export function Table<C extends ReadonlyArray<ColumnType<string, {}, any>>>({
282
292
style = { stickyLeftColumnStyle [ cell . column . id ] }
283
293
first = { index === 0 }
284
294
last = { ( index + 1 ) % columns . length === 0 }
295
+ interactiveRow = { interactiveRow }
285
296
>
286
297
{ cell . render ( "Cell" ) }
287
298
</ CellContainer >
@@ -321,18 +332,45 @@ export function Table<C extends ReadonlyArray<ColumnType<string, {}, any>>>({
321
332
/> ,
322
333
...row . leafRows . map ( ( row , index ) => {
323
334
prepareRow ( row ) ;
324
- return renderCells ( row . cells , index ) ;
335
+ return renderCells ( row . cells , index , false ) ;
325
336
} ) ,
326
337
] ;
327
338
} else {
328
339
prepareRow ( row ) ;
329
- return [ renderCells ( row . cells , index ) ] ;
340
+ return (
341
+ < RowContainer key = { index } row = { row } onPress = { onRowPress } >
342
+ { renderCells ( row . cells , index , onRowPress !== undefined ) }
343
+ </ RowContainer >
344
+ ) ;
330
345
}
331
346
} ) }
332
347
</ Box >
333
348
) ;
334
349
}
335
350
351
+ function RowContainer < C extends ReadonlyArray < ColumnType < string , { } , any > > > ( {
352
+ row,
353
+ children,
354
+ onPress,
355
+ } : {
356
+ row : Row < RowType < C > > ;
357
+ onPress : ( ( row : Row < RowType < C > > ) => void ) | undefined ;
358
+ children : Children ;
359
+ } ) {
360
+ const config = useBentoConfig ( ) . table ;
361
+ return (
362
+ < Box
363
+ className = { rowContainer }
364
+ style = { assignInlineVars ( {
365
+ [ selectedRowBackgroundColor ] : vars . backgroundColor [ config . selectedRowBackgroundColor ] ,
366
+ } ) }
367
+ onClick = { ( ) => onPress ?.( row ) }
368
+ >
369
+ { children }
370
+ </ Box >
371
+ ) ;
372
+ }
373
+
336
374
function ColumnHeader < D extends Record < string , unknown > > ( {
337
375
column,
338
376
style,
@@ -484,6 +522,7 @@ function CellContainer({
484
522
lastLeftSticky,
485
523
first,
486
524
last,
525
+ interactiveRow,
487
526
...props
488
527
} : {
489
528
children : any ;
@@ -492,14 +531,15 @@ function CellContainer({
492
531
lastLeftSticky : boolean ;
493
532
first : boolean ;
494
533
last : boolean ;
534
+ interactiveRow : boolean ;
495
535
} & TableCellProps ) {
496
536
const tableConfig = useBentoConfig ( ) . table ;
497
537
498
538
return (
499
539
< Box className = { lastLeftSticky && lastLeftStickyColumn } style = { style } >
500
540
< Box
501
541
background = { index % 2 === 0 ? tableConfig . evenRowsBackgroundColor : "backgroundPrimary" }
502
- className = { cellContainer }
542
+ className = { cellContainerRecipe ( { interactiveRow } ) }
503
543
paddingLeft = { first ? tableConfig . boundaryPadding : undefined }
504
544
paddingRight = { last ? tableConfig . boundaryPadding : undefined }
505
545
{ ...props }
0 commit comments