Skip to content

Commit e360dca

Browse files
committed
chore: RM MessageChanel
1 parent 91cd6b7 commit e360dca

File tree

4 files changed

+20
-50
lines changed

4 files changed

+20
-50
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"dependencies": {
4444
"@babel/runtime": "^7.11.1",
4545
"classnames": "^2.2.1",
46-
"rc-resize-observer": "^0.3.0"
46+
"rc-resize-observer": "^0.3.0",
47+
"rc-util": "^5.5.1"
4748
},
4849
"devDependencies": {
4950
"@types/classnames": "^2.2.9",

src/Overflow.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import classNames from 'classnames';
33
import ResizeObserver from 'rc-resize-observer';
44
import Item from './Item';
5-
import { useBatchState } from './hooks/useBatchState';
5+
import { useBatchFrameState } from './hooks/useBatchFrameState';
66

77
export interface OverflowProps<ItemType> {
88
prefixCls?: string;
@@ -30,7 +30,7 @@ function Overflow<ItemType = any>(
3030
maxCount = 'responsive',
3131
} = props;
3232

33-
const createUseState = useBatchState();
33+
const createUseState = useBatchFrameState();
3434

3535
const [containerWidth, setContainerWidth] = createUseState(0);
3636
const [itemWidths, setItemWidths] = createUseState(
@@ -39,6 +39,7 @@ function Overflow<ItemType = any>(
3939
const [overflowWidth, setOverflowWidth] = createUseState(0);
4040

4141
const [displayCount, setDisplayCount] = React.useState(0);
42+
const [restReady, setRestReady] = React.useState(false);
4243

4344
const itemPrefixCls = `${prefixCls}-item`;
4445

@@ -58,6 +59,13 @@ function Overflow<ItemType = any>(
5859
[renderItem],
5960
);
6061

62+
function updateDisplayCount(count: number, notReady?: boolean) {
63+
setDisplayCount(count);
64+
if (!notReady) {
65+
setRestReady(count < data.length - 1);
66+
}
67+
}
68+
6169
// ================================= Size =================================
6270
function onOverflowResize(_: object, element: HTMLElement) {
6371
setContainerWidth(element.clientWidth);
@@ -92,18 +100,18 @@ function Overflow<ItemType = any>(
92100

93101
// Break since data not ready
94102
if (itemWidth === undefined) {
95-
setDisplayCount(i - 1);
103+
updateDisplayCount(i - 1, true);
96104
break;
97105
}
98106

99107
// Find best match
100108
totalWidth += itemWidth;
101109

102110
if (totalWidth + overflowWidth > containerWidth) {
103-
setDisplayCount(i - 1);
111+
updateDisplayCount(i - 1);
104112
break;
105113
} else if (i === len - 1) {
106-
setDisplayCount(len - 1);
114+
updateDisplayCount(len - 1);
107115
break;
108116
}
109117
}
@@ -138,6 +146,7 @@ function Overflow<ItemType = any>(
138146
className={`${itemPrefixCls}-rest`}
139147
disabled={disabled}
140148
registerSize={registerOverflowSize}
149+
display={restReady}
141150
>
142151
Overflow
143152
</Item>

src/hooks/useBatchState.tsx renamed to src/hooks/useBatchFrameState.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { useRef, useState } from 'react';
2-
import { beforeFrame, cancelBeforeFrame } from '../util';
2+
import raf from 'rc-util/lib/raf';
33

44
/**
55
* State generate. Return a `setState` but it will flush all state with one render to save perf.
66
* This is not a realization of `unstable_batchedUpdates`.
77
*/
8-
export function useBatchState() {
8+
export function useBatchFrameState() {
99
const [, forceUpdate] = useState({});
1010
const statesRef = useRef<any[]>([]);
1111
let walkingIndex = 0;
@@ -29,10 +29,10 @@ export function useBatchState() {
2929
statesRef.current[myIndex] =
3030
typeof val === 'function' ? val(statesRef.current[myIndex]) : val;
3131

32-
cancelBeforeFrame(beforeFrameId);
32+
raf.cancel(beforeFrameId);
3333

3434
// Flush with batch
35-
beforeFrameId = beforeFrame(() => {
35+
beforeFrameId = raf(() => {
3636
forceUpdate({});
3737
});
3838
}

src/util.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)