@@ -5,6 +5,7 @@ import Table, { INTERNAL_COL_DEFINE } from '../src';
5
5
import BodyRow from '../src/Body/BodyRow' ;
6
6
import Cell from '../src/Cell' ;
7
7
import { INTERNAL_HOOKS } from '../src/constant' ;
8
+ import { VariableSizeGrid as Grid } from "react-window" ;
8
9
9
10
describe ( 'Table.Basic' , ( ) => {
10
11
const data = [
@@ -1216,4 +1217,116 @@ describe('Table.Basic', () => {
1216
1217
1217
1218
expect ( wrapper . render ( ) ) . toMatchSnapshot ( ) ;
1218
1219
} ) ;
1220
+
1221
+ it ( 'using both column children and component body simultaneously' , ( ) => {
1222
+ const width = 150 ;
1223
+ const noChildColLen = 4 ;
1224
+ const ChildColLen = 4 ;
1225
+ const buildChildDataIndex = ( n ) => `col${ n } ` ;
1226
+ const columns = Array . from ( { length : noChildColLen } , ( _ , i ) => ( {
1227
+ title : `第 ${ i } 列` ,
1228
+ dataIndex : buildChildDataIndex ( i ) ,
1229
+ width,
1230
+ } ) ) . concat ( Array . from ( { length : ChildColLen } , ( _ , i ) => ( {
1231
+ title : `第 ${ i } 分组` ,
1232
+ dataIndex : `parentCol${ i } ` ,
1233
+ width : width * 2 ,
1234
+ children : [
1235
+ {
1236
+ title : `第 ${ noChildColLen + i } 列` ,
1237
+ dataIndex : buildChildDataIndex ( noChildColLen + i ) ,
1238
+ width,
1239
+ } ,
1240
+ {
1241
+ title : `第 ${ noChildColLen + 1 + i } 列` ,
1242
+ dataIndex : buildChildDataIndex ( noChildColLen + 1 + i ) ,
1243
+ width,
1244
+ } ,
1245
+ ]
1246
+ } ) ) ) ;
1247
+ const data = Array . from ( { length : 10000 } , ( _ , r ) => {
1248
+ const colLen = noChildColLen + ChildColLen * 2 ;
1249
+ const record = { } ;
1250
+ for ( let c = 0 ; c < colLen ; c ++ ) {
1251
+ record [ buildChildDataIndex ( c ) ] = `r${ r } , c${ c } `
1252
+ }
1253
+ return record ;
1254
+ } )
1255
+ const Demo = ( props ) => {
1256
+ const gridRef = React . useRef ( ) ;
1257
+ const [ connectObject ] = React . useState ( ( ) => {
1258
+ const obj = { } ;
1259
+ Object . defineProperty ( obj , "scrollLeft" , {
1260
+ get : ( ) => {
1261
+ if ( gridRef . current ) {
1262
+ return gridRef . current ?. state ?. scrollLeft ;
1263
+ }
1264
+ return null ;
1265
+ } ,
1266
+ set : ( scrollLeft ) => {
1267
+ if ( gridRef . current ) {
1268
+ gridRef . current . scrollTo ( { scrollLeft } ) ;
1269
+ }
1270
+ }
1271
+ } ) ;
1272
+
1273
+ return obj ;
1274
+ } ) ;
1275
+
1276
+ React . useEffect ( ( ) => {
1277
+ gridRef . current . resetAfterIndices ( {
1278
+ columnIndex : 0 ,
1279
+ shouldForceUpdate : false
1280
+ } ) ;
1281
+ } , [ ] ) ;
1282
+
1283
+ const renderVirtualList = ( rawData , { scrollbarSize, ref, onScroll } ) => {
1284
+ ref . current = connectObject ;
1285
+ return (
1286
+ < Grid
1287
+ ref = { gridRef }
1288
+ className = "virtual-grid"
1289
+ columnCount = { columns . length }
1290
+ columnWidth = { ( index ) => {
1291
+ const { width } = columns [ index ] ;
1292
+ return index === columns . length - 1
1293
+ ? width - scrollbarSize - 1
1294
+ : width ;
1295
+ } }
1296
+ height = { 300 }
1297
+ rowCount = { rawData . length }
1298
+ rowHeight = { ( ) => 50 }
1299
+ width = { 800 }
1300
+ onScroll = { ( { scrollLeft } ) => {
1301
+ onScroll ( { scrollLeft } ) ;
1302
+ } }
1303
+ >
1304
+ { ( { columnIndex, rowIndex, style } ) => (
1305
+ < div
1306
+ className = { `virtual-cell ${ columnIndex === columns . length - 1 ? 'virtual-cell-last' : '' } ` }
1307
+ style = { style }
1308
+ >
1309
+ r{ rowIndex } , c{ columnIndex }
1310
+ </ div >
1311
+ ) }
1312
+ </ Grid >
1313
+ ) ;
1314
+ } ;
1315
+
1316
+ return (
1317
+ < Table
1318
+ style = { { width : 800 } }
1319
+ tableLayout = "fixed"
1320
+ columns = { props . columns }
1321
+ data = { props . data }
1322
+ scroll = { { y : 300 , x : 300 } }
1323
+ components = { {
1324
+ body : renderVirtualList
1325
+ } }
1326
+ />
1327
+ ) ;
1328
+ } ;
1329
+ const wrapper = mount ( < Demo columns = { columns } data = { data } /> ) ;
1330
+ expect ( wrapper . find ( 'col' ) . at ( noChildColLen + ChildColLen * 2 - 1 ) . props ( ) . style . width + wrapper . find ( 'col' ) . last ( ) . props ( ) . style . width ) . toEqual ( width ) ;
1331
+ } )
1219
1332
} ) ;
0 commit comments