1
- import * as React from 'react' ;
2
- import { useState , useMemo , useCallback } from 'react' ;
3
1
import classNames from 'classnames' ;
4
2
import ResizeObserver from 'rc-resize-observer' ;
5
3
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect' ;
4
+ import * as React from 'react' ;
5
+ import { useCallback , useMemo , useState } from 'react' ;
6
6
import Item from './Item' ;
7
- import useEffectState , { useBatcher } from './hooks/useEffectState' ;
8
7
import type { ComponentType } from './RawItem' ;
9
8
import RawItem from './RawItem' ;
10
9
import { OverflowContext } from './context' ;
10
+ import useEffectState , { useBatcher } from './hooks/useEffectState' ;
11
11
12
12
const RESPONSIVE = 'responsive' as const ;
13
13
const INVALIDATE = 'invalidate' as const ;
@@ -16,7 +16,7 @@ export { OverflowContext } from './context';
16
16
17
17
export type { ComponentType } from './RawItem' ;
18
18
19
- export interface OverflowProps < ItemType > extends React . HTMLAttributes < any > {
19
+ export interface OverflowProps < ItemType > extends Omit < React . HTMLAttributes < any > , 'prefix' > {
20
20
prefixCls ?: string ;
21
21
className ?: string ;
22
22
style ?: React . CSSProperties ;
@@ -33,6 +33,7 @@ export interface OverflowProps<ItemType> extends React.HTMLAttributes<any> {
33
33
| ( ( omittedItems : ItemType [ ] ) => React . ReactNode ) ;
34
34
/** @private Do not use in your production. Render raw node that need wrap Item by developer self */
35
35
renderRawRest ?: ( omittedItems : ItemType [ ] ) => React . ReactElement ;
36
+ prefix ?: React . ReactNode ;
36
37
suffix ?: React . ReactNode ;
37
38
component ?: ComponentType ;
38
39
itemComponent ?: ComponentType ;
@@ -65,6 +66,7 @@ function Overflow<ItemType = any>(
65
66
maxCount,
66
67
renderRest,
67
68
renderRawRest,
69
+ prefix,
68
70
suffix,
69
71
component : Component = 'div' ,
70
72
itemComponent,
@@ -96,6 +98,11 @@ function Overflow<ItemType = any>(
96
98
0 ,
97
99
) ;
98
100
101
+ const [ prefixWidth , setPrefixWidth ] = useEffectState < number > (
102
+ notifyEffectUpdate ,
103
+ 0 ,
104
+ ) ;
105
+
99
106
const [ suffixWidth , setSuffixWidth ] = useEffectState < number > (
100
107
notifyEffectUpdate ,
101
108
0 ,
@@ -223,6 +230,10 @@ function Overflow<ItemType = any>(
223
230
setPrevRestWidth ( restWidth ) ;
224
231
}
225
232
233
+ function registerPrefixSize ( _ : React . Key , width : number | null ) {
234
+ setPrefixWidth ( width ! ) ;
235
+ }
236
+
226
237
function registerSuffixSize ( _ : React . Key , width : number | null ) {
227
238
setSuffixWidth ( width ! ) ;
228
239
}
@@ -238,7 +249,7 @@ function Overflow<ItemType = any>(
238
249
typeof mergedRestWidth === 'number' &&
239
250
mergedData
240
251
) {
241
- let totalWidth = suffixWidth ;
252
+ let totalWidth = prefixWidth + suffixWidth ;
242
253
243
254
const len = mergedData . length ;
244
255
const lastIndex = len - 1 ;
@@ -286,14 +297,15 @@ function Overflow<ItemType = any>(
286
297
}
287
298
}
288
299
289
- if ( suffix && getItemWidth ( 0 ) + suffixWidth > mergedContainerWidth ) {
300
+ if ( suffix && getItemWidth ( 0 ) + suffixWidth + prefixWidth > mergedContainerWidth ) {
290
301
setSuffixFixedStart ( null ) ;
291
302
}
292
303
}
293
304
} , [
294
305
mergedContainerWidth ,
295
306
itemWidths ,
296
307
restWidth ,
308
+ prefixWidth ,
297
309
suffixWidth ,
298
310
getKey ,
299
311
mergedData ,
@@ -366,26 +378,26 @@ function Overflow<ItemType = any>(
366
378
367
379
const mergedRenderRest = renderRest || defaultRenderRest ;
368
380
369
- const restNode = renderRawRest ? (
370
- < OverflowContext . Provider
371
- value = { {
372
- ...itemSharedProps ,
373
- ...restContextProps ,
374
- } }
375
- >
376
- { renderRawRest ( omittedItems ) }
377
- </ OverflowContext . Provider >
378
- ) : (
379
- < Item
380
- { ...itemSharedProps }
381
- // When not show, order should be the last
382
- { ...restContextProps }
383
- >
384
- { typeof mergedRenderRest === 'function'
385
- ? mergedRenderRest ( omittedItems )
386
- : mergedRenderRest }
387
- </ Item >
388
- ) ;
381
+ const restNode = renderRawRest ? (
382
+ < OverflowContext . Provider
383
+ value = { {
384
+ ...itemSharedProps ,
385
+ ...restContextProps ,
386
+ } }
387
+ >
388
+ { renderRawRest ( omittedItems ) }
389
+ </ OverflowContext . Provider >
390
+ ) : (
391
+ < Item
392
+ { ...itemSharedProps }
393
+ // When not show, order should be the last
394
+ { ...restContextProps }
395
+ >
396
+ { typeof mergedRenderRest === 'function'
397
+ ? mergedRenderRest ( omittedItems )
398
+ : mergedRenderRest }
399
+ </ Item >
400
+ ) ;
389
401
390
402
const overflowNode = (
391
403
< Component
@@ -394,6 +406,20 @@ function Overflow<ItemType = any>(
394
406
ref = { ref }
395
407
{ ...restProps }
396
408
>
409
+ { /* Prefix Node */ }
410
+ { prefix && (
411
+ < Item
412
+ { ...itemSharedProps }
413
+ responsive = { isResponsive }
414
+ responsiveDisabled = { ! shouldResponsive }
415
+ order = { mergedDisplayCount }
416
+ className = { `${ itemPrefixCls } -prefix` }
417
+ registerSize = { registerPrefixSize }
418
+ display
419
+ >
420
+ { prefix }
421
+ </ Item >
422
+ ) }
397
423
{ mergedData . map ( internalRenderItemNode ) }
398
424
399
425
{ /* Rest Count Item */ }
@@ -421,7 +447,9 @@ function Overflow<ItemType = any>(
421
447
< ResizeObserver onResize = { onOverflowResize } disabled = { ! shouldResponsive } >
422
448
{ overflowNode }
423
449
</ ResizeObserver >
424
- ) : overflowNode ;
450
+ ) : (
451
+ overflowNode
452
+ ) ;
425
453
}
426
454
427
455
const ForwardOverflow = React . forwardRef ( Overflow ) ;
0 commit comments