@@ -8,7 +8,6 @@ import useMount from './useMount';
8
8
import useUpdateLayoutEffect from './useUpdateLayoutEffect' ;
9
9
import isNumberOrTrue from './isNumberOrTrue' ;
10
10
import { RowDataType , ElementOffset } from '../@types/common' ;
11
- import debounce from 'lodash/debounce' ;
12
11
13
12
interface TableDimensionProps < Row , Key > {
14
13
data ?: readonly Row [ ] ;
@@ -257,13 +256,21 @@ const useTableDimension = <Row extends RowDataType, Key>(props: TableDimensionPr
257
256
calculateTableHeight ( entries [ 0 ] . contentRect . height ) ;
258
257
} ) ;
259
258
containerResizeObserver . current . observe ( tableRef ?. current ?. parentNode as Element ) ;
260
- const changeTableWidthWhenResize = debounce ( entries => {
261
- const { width } = entries [ 0 ] . contentRect ;
262
- // bordered table width is 1px larger than the container width. fix: #405 #404
263
- const widthWithBorder = width + 2 ;
264
-
265
- calculateTableWidth ( bordered ? widthWithBorder : width ) ;
266
- } , 20 ) ;
259
+ let idleCallbackId : null | number = null ;
260
+ const changeTableWidthWhenResize = function ( entries ) {
261
+ if ( idleCallbackId ) {
262
+ window . cancelIdleCallback ( idleCallbackId ) ;
263
+ }
264
+ idleCallbackId = window . requestIdleCallback ( deadline => {
265
+ // if idle time >= 10ms, then we can judge other tasks have completed
266
+ if ( deadline . timeRemaining ( ) >= 10 ) {
267
+ idleCallbackId = null ;
268
+ calculateTableWidth ( entries [ 0 ] . contentRect . width ) ;
269
+ } else {
270
+ changeTableWidthWhenResize ( entries ) ;
271
+ }
272
+ } ) ;
273
+ } ;
267
274
resizeObserver . current = new ResizeObserver ( changeTableWidthWhenResize ) ;
268
275
resizeObserver . current . observe ( tableRef ?. current as Element ) ;
269
276
0 commit comments