@@ -7,6 +7,7 @@ import useFlattenRecords, { type FlattenData } from '../hooks/useFlattenRecords'
77import type { ColumnType , OnCustomizeScroll } from '../interface' ;
88import BodyLine from './BodyLine' ;
99import { GridContext , StaticContext } from './context' ;
10+ import Cell from '../Cell' ;
1011
1112export interface GridProps < RecordType = any > {
1213 data : RecordType [ ] ;
@@ -20,15 +21,23 @@ export interface GridRef {
2021const Grid = React . forwardRef < GridRef , GridProps > ( ( props , ref ) => {
2122 const { data, onScroll } = props ;
2223
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+ ] ) ;
3241 const { scrollY, scrollX, listItemHeight } = useContext ( StaticContext ) ;
3342
3443 // =========================== Ref ============================
@@ -62,10 +71,10 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
6271 const obj = { } as GridRef ;
6372
6473 Object . defineProperty ( obj , 'scrollLeft' , {
65- get : ( ) => listRef . current . getScrollInfo ( ) . x ,
74+ get : ( ) => listRef . current ? .getScrollInfo ( ) . x || 0 ,
6675
6776 set : ( value : number ) => {
68- listRef . current . scrollTo ( {
77+ listRef . current ? .scrollTo ( {
6978 left : value ,
7079 } ) ;
7180 } ,
@@ -180,32 +189,41 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
180189 // ========================== Render ==========================
181190 const tblPrefixCls = `${ prefixCls } -tbody` ;
182191
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 >
206222 </ div >
207- </ GridContext . Provider >
208- ) ;
223+ ) ;
224+ }
225+
226+ return < GridContext . Provider value = { gridContext } > { bodyContent } </ GridContext . Provider > ;
209227} ) ;
210228
211229const ResponseGrid = responseImmutable ( Grid ) ;
0 commit comments