@@ -7,6 +7,7 @@ import useFlattenRecords, { type FlattenData } from '../hooks/useFlattenRecords'
7
7
import type { ColumnType , OnCustomizeScroll } from '../interface' ;
8
8
import BodyLine from './BodyLine' ;
9
9
import { GridContext , StaticContext } from './context' ;
10
+ import Cell from '../Cell' ;
10
11
11
12
export interface GridProps < RecordType = any > {
12
13
data : RecordType [ ] ;
@@ -20,15 +21,23 @@ export interface GridRef {
20
21
const Grid = React . forwardRef < GridRef , GridProps > ( ( props , ref ) => {
21
22
const { data, onScroll } = props ;
22
23
23
- const { flattenColumns, onColumnResize, getRowKey, expandedKeys, prefixCls, childrenColumnName } =
24
- useContext ( TableContext , [
25
- 'flattenColumns' ,
26
- 'onColumnResize' ,
27
- 'getRowKey' ,
28
- 'prefixCls' ,
29
- 'expandedKeys' ,
30
- 'childrenColumnName' ,
31
- ] ) ;
24
+ const {
25
+ flattenColumns,
26
+ onColumnResize,
27
+ getRowKey,
28
+ expandedKeys,
29
+ prefixCls,
30
+ childrenColumnName,
31
+ emptyNode,
32
+ } = useContext ( TableContext , [
33
+ 'flattenColumns' ,
34
+ 'onColumnResize' ,
35
+ 'getRowKey' ,
36
+ 'prefixCls' ,
37
+ 'expandedKeys' ,
38
+ 'childrenColumnName' ,
39
+ 'emptyNode' ,
40
+ ] ) ;
32
41
const { scrollY, scrollX, listItemHeight } = useContext ( StaticContext ) ;
33
42
34
43
// =========================== Ref ============================
@@ -62,10 +71,10 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
62
71
const obj = { } as GridRef ;
63
72
64
73
Object . defineProperty ( obj , 'scrollLeft' , {
65
- get : ( ) => listRef . current . getScrollInfo ( ) . x ,
74
+ get : ( ) => listRef . current ? .getScrollInfo ( ) . x || 0 ,
66
75
67
76
set : ( value : number ) => {
68
- listRef . current . scrollTo ( {
77
+ listRef . current ? .scrollTo ( {
69
78
left : value ,
70
79
} ) ;
71
80
} ,
@@ -180,32 +189,41 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
180
189
// ========================== Render ==========================
181
190
const tblPrefixCls = `${ prefixCls } -tbody` ;
182
191
183
- return (
184
- < GridContext . Provider value = { gridContext } >
185
- < div >
186
- < VirtualList < FlattenData < any > >
187
- ref = { listRef }
188
- className = { classNames ( tblPrefixCls , `${ tblPrefixCls } -virtual` ) }
189
- height = { scrollY }
190
- itemHeight = { listItemHeight || 24 }
191
- data = { flattenData }
192
- itemKey = { item => getRowKey ( item . record ) }
193
- scrollWidth = { scrollX }
194
- onVirtualScroll = { ( { x } ) => {
195
- onScroll ( {
196
- scrollLeft : x ,
197
- } ) ;
198
- } }
199
- extraRender = { extraRender }
200
- >
201
- { ( item , index , itemProps ) => {
202
- const rowKey = getRowKey ( item . record , index ) ;
203
- return < BodyLine data = { item } rowKey = { rowKey } index = { index } { ...itemProps } /> ;
204
- } }
205
- </ VirtualList >
192
+ let bodyContent : React . ReactNode ;
193
+ if ( flattenData . length ) {
194
+ bodyContent = (
195
+ < VirtualList < FlattenData < any > >
196
+ ref = { listRef }
197
+ className = { classNames ( tblPrefixCls , `${ tblPrefixCls } -virtual` ) }
198
+ height = { scrollY }
199
+ itemHeight = { listItemHeight || 24 }
200
+ data = { flattenData }
201
+ itemKey = { item => getRowKey ( item . record ) }
202
+ scrollWidth = { scrollX }
203
+ onVirtualScroll = { ( { x } ) => {
204
+ onScroll ( {
205
+ scrollLeft : x ,
206
+ } ) ;
207
+ } }
208
+ extraRender = { extraRender }
209
+ >
210
+ { ( item , index , itemProps ) => {
211
+ const rowKey = getRowKey ( item . record , index ) ;
212
+ return < BodyLine data = { item } rowKey = { rowKey } index = { index } { ...itemProps } /> ;
213
+ } }
214
+ </ VirtualList >
215
+ ) ;
216
+ } else {
217
+ bodyContent = (
218
+ < div className = { classNames ( `${ prefixCls } -placeholder` ) } >
219
+ < Cell component = "div" prefixCls = { prefixCls } >
220
+ { emptyNode }
221
+ </ Cell >
206
222
</ div >
207
- </ GridContext . Provider >
208
- ) ;
223
+ ) ;
224
+ }
225
+
226
+ return < GridContext . Provider value = { gridContext } > { bodyContent } </ GridContext . Provider > ;
209
227
} ) ;
210
228
211
229
const ResponseGrid = responseImmutable ( Grid ) ;
0 commit comments