@@ -50,6 +50,7 @@ import {
50
50
RowClassName ,
51
51
CustomizeComponent ,
52
52
ColumnType ,
53
+ CustomizeScrollBody ,
53
54
} from './interface' ;
54
55
import TableContext from './context/TableContext' ;
55
56
import BodyContext from './context/BodyContext' ;
@@ -68,6 +69,9 @@ import { findAllChildrenKeys, renderExpandIcon } from './utils/expandUtil';
68
69
// Used for conditions cache
69
70
const EMPTY_DATA = [ ] ;
70
71
72
+ // Used for customize scroll
73
+ const EMPTY_SCROLL_TARGET = { } ;
74
+
71
75
export const INTERNAL_HOOKS = 'rc-table-internal-hook' ;
72
76
73
77
export interface TableProps < RecordType extends DefaultRecordType >
@@ -98,7 +102,7 @@ export interface TableProps<RecordType extends DefaultRecordType>
98
102
// Customize
99
103
id ?: string ;
100
104
showHeader ?: boolean ;
101
- components ?: TableComponents ;
105
+ components ?: TableComponents < RecordType > ;
102
106
onRow ?: GetComponentProps < RecordType > ;
103
107
onHeaderRow ?: GetComponentProps < ColumnType < RecordType > [ ] > ;
104
108
emptyText ?: React . ReactNode | ( ( ) => React . ReactNode ) ;
@@ -188,13 +192,15 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
188
192
}
189
193
190
194
// ==================== Customize =====================
191
- const mergedComponents = React . useMemo ( ( ) => mergeObject < TableComponents > ( components , { } ) , [
192
- components ,
193
- ] ) ;
195
+ const mergedComponents = React . useMemo (
196
+ ( ) => mergeObject < TableComponents < RecordType > > ( components , { } ) ,
197
+ [ components ] ,
198
+ ) ;
194
199
195
200
const getComponent = React . useCallback < GetComponent > (
196
201
( path , defaultComponent ) =>
197
- getPathValue < CustomizeComponent , TableComponents > ( mergedComponents , path ) || defaultComponent ,
202
+ getPathValue < CustomizeComponent , TableComponents < RecordType >> ( mergedComponents , path ) ||
203
+ defaultComponent ,
198
204
[ mergedComponents ] ,
199
205
) ;
200
206
@@ -354,18 +360,25 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
354
360
/* eslint-enable */
355
361
}
356
362
357
- const onScroll : React . UIEventHandler < HTMLDivElement > = ( { currentTarget } ) => {
358
- const { scrollLeft, scrollWidth, clientWidth } = currentTarget ;
363
+ const onScroll = ( {
364
+ currentTarget,
365
+ scrollLeft,
366
+ } : React . UIEvent < HTMLDivElement > & { scrollLeft ?: number } ) => {
367
+ const mergedScrollLeft = typeof scrollLeft === 'number' ? scrollLeft : currentTarget . scrollLeft ;
359
368
360
- if ( ! getScrollTarget ( ) || getScrollTarget ( ) === currentTarget ) {
361
- setScrollTarget ( currentTarget ) ;
369
+ const compareTarget = currentTarget || EMPTY_SCROLL_TARGET ;
370
+ if ( ! getScrollTarget ( ) || getScrollTarget ( ) === compareTarget ) {
371
+ setScrollTarget ( compareTarget ) ;
362
372
363
- forceScroll ( scrollLeft , scrollHeaderRef . current ) ;
364
- forceScroll ( scrollLeft , scrollBodyRef . current ) ;
373
+ forceScroll ( mergedScrollLeft , scrollHeaderRef . current ) ;
374
+ forceScroll ( mergedScrollLeft , scrollBodyRef . current ) ;
365
375
}
366
376
367
- setPingedLeft ( scrollLeft > 0 ) ;
368
- setPingedRight ( scrollLeft < scrollWidth - clientWidth ) ;
377
+ if ( currentTarget ) {
378
+ const { scrollWidth, clientWidth } = currentTarget ;
379
+ setPingedLeft ( mergedScrollLeft > 0 ) ;
380
+ setPingedRight ( mergedScrollLeft < scrollWidth - clientWidth ) ;
381
+ }
369
382
} ;
370
383
371
384
const triggerOnScroll = ( ) => {
@@ -451,26 +464,40 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
451
464
) ;
452
465
453
466
const footerTable = summary && < Footer > { summary ( mergedData ) } </ Footer > ;
467
+ const customizeScrollBody = getComponent ( [ 'body' ] ) as CustomizeScrollBody < RecordType > ;
468
+
469
+ if (
470
+ process . env . NODE_ENV !== 'production' &&
471
+ typeof customizeScrollBody === 'function' &&
472
+ ! fixHeader
473
+ ) {
474
+ warning ( false , '`components.body` with render props is only work on `scroll.y`.' ) ;
475
+ }
454
476
455
477
if ( fixHeader ) {
456
- groupTableNode = (
457
- < >
458
- { /* Header Table */ }
459
- { showHeader !== false && (
460
- < div
461
- style = { {
462
- ...scrollXStyle ,
463
- marginBottom : fixColumn ? - scrollbarSize : null ,
464
- } }
465
- onScroll = { onScroll }
466
- ref = { scrollHeaderRef }
467
- className = { classNames ( `${ prefixCls } -header` ) }
468
- >
469
- < FixedHeader { ...headerProps } { ...columnContext } />
470
- </ div >
471
- ) }
478
+ let bodyContent : React . ReactNode ;
472
479
473
- { /* Body Table */ }
480
+ if ( typeof customizeScrollBody === 'function' ) {
481
+ bodyContent = customizeScrollBody ( mergedData , {
482
+ scrollbarSize,
483
+ ref : scrollBodyRef ,
484
+ onScroll,
485
+ } ) ;
486
+ headerProps . colWidths = flattenColumns . map ( ( { width } , index ) => {
487
+ const colWidth = index === columns . length - 1 ? ( width as number ) - scrollbarSize : width ;
488
+
489
+ if ( typeof colWidth === 'number' && ! Number . isNaN ( colWidth ) ) {
490
+ return colWidth ;
491
+ }
492
+ warning (
493
+ false ,
494
+ 'When use `components.body` with render props. Each column should have a fixed value.' ,
495
+ ) ;
496
+
497
+ return 0 ;
498
+ } ) as number [ ] ;
499
+ } else {
500
+ bodyContent = (
474
501
< div
475
502
style = { {
476
503
...scrollXStyle ,
@@ -491,6 +518,28 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
491
518
{ footerTable }
492
519
</ TableComponent >
493
520
</ div >
521
+ ) ;
522
+ }
523
+
524
+ groupTableNode = (
525
+ < >
526
+ { /* Header Table */ }
527
+ { showHeader !== false && (
528
+ < div
529
+ style = { {
530
+ ...scrollXStyle ,
531
+ marginBottom : fixColumn ? - scrollbarSize : null ,
532
+ } }
533
+ onScroll = { onScroll }
534
+ ref = { scrollHeaderRef }
535
+ className = { classNames ( `${ prefixCls } -header` ) }
536
+ >
537
+ < FixedHeader { ...headerProps } { ...columnContext } />
538
+ </ div >
539
+ ) }
540
+
541
+ { /* Body Table */ }
542
+ { bodyContent }
494
543
</ >
495
544
) ;
496
545
} else {
0 commit comments