@@ -14,17 +14,7 @@ import type {
14
14
TriggerEventHandler ,
15
15
} from '../../interface' ;
16
16
import { INTERNAL_COL_DEFINE } from '../../utils/legacyUtil' ;
17
-
18
- function parseColWidth ( totalWidth : number , width : string | number = '' ) {
19
- if ( typeof width === 'number' ) {
20
- return width ;
21
- }
22
-
23
- if ( width . endsWith ( '%' ) ) {
24
- return ( totalWidth * parseFloat ( width ) ) / 100 ;
25
- }
26
- return null ;
27
- }
17
+ import useWidthColumns from './useWidthColumns' ;
28
18
29
19
export function convertChildrenToColumns < RecordType > (
30
20
children : React . ReactNode ,
@@ -272,66 +262,21 @@ function useColumns<RecordType>(
272
262
273
263
// ========================== Flatten =========================
274
264
const flattenColumns = React . useMemo ( ( ) => {
275
- let tmpColumns = mergedColumns ;
276
-
277
265
if ( direction === 'rtl' ) {
278
- tmpColumns = revertForRtl ( flatColumns ( mergedColumns ) ) ;
279
- } else {
280
- tmpColumns = flatColumns ( mergedColumns ) ;
266
+ return revertForRtl ( flatColumns ( mergedColumns ) ) ;
281
267
}
282
-
283
- // Fill width if needed
284
- if ( scrollWidth && scrollWidth > 0 ) {
285
- let totalWidth = 0 ;
286
- let missWidthCount = 0 ;
287
-
288
- // collect not given width column
289
- tmpColumns . forEach ( ( col : any ) => {
290
- const colWidth = parseColWidth ( scrollWidth , col . width ) ;
291
-
292
- if ( colWidth ) {
293
- totalWidth += colWidth ;
294
- } else {
295
- missWidthCount += 1 ;
296
- }
297
- } ) ;
298
-
299
- // Fill width
300
- let restWidth = scrollWidth - totalWidth ;
301
- let restCount = missWidthCount ;
302
- const avgWidth = restWidth / missWidthCount ;
303
-
304
- tmpColumns = tmpColumns . map ( ( col : any ) => {
305
- const clone = {
306
- ...col ,
307
- } ;
308
-
309
- const colWidth = parseColWidth ( scrollWidth , clone . width ) ;
310
-
311
- if ( colWidth ) {
312
- clone . width = colWidth ;
313
- } else {
314
- const colAvgWidth = Math . floor ( avgWidth ) ;
315
-
316
- clone . width = restCount === 1 ? restWidth : colAvgWidth ;
317
-
318
- restWidth -= colAvgWidth ;
319
- restCount -= 1 ;
320
- }
321
-
322
- return clone ;
323
- } ) ;
324
- }
325
-
326
- return tmpColumns ;
268
+ return flatColumns ( mergedColumns ) ;
327
269
} , [ mergedColumns , direction , scrollWidth ] ) ;
328
270
329
271
// Only check out of production since it's waste for each render
330
272
if ( process . env . NODE_ENV !== 'production' ) {
331
273
warningFixed ( direction === 'rtl' ? flattenColumns . slice ( ) . reverse ( ) : flattenColumns ) ;
332
274
}
333
275
334
- return [ mergedColumns , flattenColumns ] ;
276
+ // ========================= FillWidth ========================
277
+ const filledColumns = useWidthColumns ( flattenColumns , scrollWidth ) ;
278
+
279
+ return [ mergedColumns , filledColumns ] ;
335
280
}
336
281
337
282
export default useColumns ;
0 commit comments