Skip to content

Commit 3d852a1

Browse files
committed
feat: Support responsive ommit items
1 parent 66db52a commit 3d852a1

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

examples/basic.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function renderRest(items: ItemType[]) {
4646
}
4747

4848
const Demo = () => {
49-
const [responsive, setResponsive] = React.useState(false);
49+
const [responsive, setResponsive] = React.useState(true);
5050
const [data, setData] = React.useState(createData(5));
5151

5252
return (

src/Overflow.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,27 @@ function Overflow<ItemType = any>(
5858
// ================================= Data =================================
5959
const isResponsive = maxCount === RESPONSIVE;
6060

61-
const [visibleItems, omittedItems] = React.useMemo(() => {
62-
const len = data.length;
61+
// When is `responsive`, we will always render rest node to get the real width of it for calculation
62+
const showRest = isResponsive || data.length > maxCount!;
63+
64+
const mergedData = React.useMemo(() => {
6365
let items = data;
6466

6567
if (isResponsive) {
66-
items = data.slice(0, Math.min(len, containerWidth / itemWidth));
68+
items = data.slice(0, Math.min(data.length, containerWidth / itemWidth));
6769
} else if (typeof maxCount === 'number') {
6870
items = data.slice(0, maxCount);
6971
}
7072

71-
return [items, data.slice(items.length, len)];
72-
}, [data, itemWidth, containerWidth, maxCount]);
73+
return items;
74+
}, [data, itemWidth, containerWidth, maxCount, isResponsive]);
7375

74-
// When is `responsive`, we will always render rest node to get the real width of it for calculation
75-
const showRest = isResponsive || data.length > maxCount!;
76+
const omittedItems = React.useMemo(() => {
77+
if (isResponsive) {
78+
return data.slice(displayCount + 1);
79+
}
80+
return data.slice(mergedData.length);
81+
}, [data, mergedData, isResponsive, displayCount]);
7682

7783
// ================================= Item =================================
7884
const getKey = React.useCallback(
@@ -121,13 +127,13 @@ function Overflow<ItemType = any>(
121127

122128
// ================================ Effect ================================
123129
React.useLayoutEffect(() => {
124-
if (containerWidth && restWidth && visibleItems) {
130+
if (containerWidth && restWidth && mergedData) {
125131
let totalWidth = 0;
126132

127-
const len = visibleItems.length;
133+
const len = mergedData.length;
128134

129135
for (let i = 0; i < len; i += 1) {
130-
const currentItemWidth = itemWidths.get(getKey(visibleItems[i], i));
136+
const currentItemWidth = itemWidths.get(getKey(mergedData[i], i));
131137

132138
// Break since data not ready
133139
if (currentItemWidth === undefined) {
@@ -147,12 +153,12 @@ function Overflow<ItemType = any>(
147153
}
148154
}
149155
}
150-
}, [containerWidth, itemWidths, restWidth, getKey, visibleItems]);
156+
}, [containerWidth, itemWidths, restWidth, getKey, mergedData]);
151157

152158
// ================================ Render ================================
153159
let overflowNode = (
154160
<div className={classNames(prefixCls, className)} style={style} ref={ref}>
155-
{visibleItems.map((item, index) => {
161+
{mergedData.map((item, index) => {
156162
const key = getKey(item, index);
157163

158164
return (

0 commit comments

Comments
 (0)