Skip to content

Commit 6a4ec37

Browse files
committed
fix: Last item too short to show
1 parent a46ec6b commit 6a4ec37

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/Overflow.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,19 @@ function Overflow<ItemType = any>(
130130
}
131131

132132
// ================================ Effect ================================
133+
function getItemWidth(index: number) {
134+
return itemWidths.get(getKey(mergedData[index], index));
135+
}
136+
133137
React.useLayoutEffect(() => {
134138
if (containerWidth && mergedRestWidth && mergedData) {
135139
let totalWidth = 0;
136140

137141
const len = mergedData.length;
142+
const lastIndex = len - 1;
138143

139144
for (let i = 0; i < len; i += 1) {
140-
const currentItemWidth = itemWidths.get(getKey(mergedData[i], i));
145+
const currentItemWidth = getItemWidth(i);
141146

142147
// Break since data not ready
143148
if (currentItemWidth === undefined) {
@@ -148,11 +153,21 @@ function Overflow<ItemType = any>(
148153
// Find best match
149154
totalWidth += currentItemWidth;
150155

151-
if (totalWidth + mergedRestWidth > containerWidth) {
156+
if (
157+
(i === lastIndex - 1 &&
158+
totalWidth + getItemWidth(lastIndex)! <= containerWidth) ||
159+
i === lastIndex
160+
) {
161+
// Additional check if match the end
162+
updateDisplayCount(lastIndex);
163+
break;
164+
} else if (totalWidth + mergedRestWidth > containerWidth) {
165+
// Can not hold all the content to show rest
152166
updateDisplayCount(i - 1);
153167
break;
154-
} else if (i === len - 1) {
155-
updateDisplayCount(len - 1);
168+
} else if (i === lastIndex) {
169+
// Reach the end
170+
updateDisplayCount(lastIndex);
156171
break;
157172
}
158173
}

tests/responsive.spec.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ describe('Overflow.Responsive', () => {
3939
wrapper.initSize(100, 20); // [0][1][2][3][4][+2](5)(6)
4040
expect(wrapper.findItems()).toHaveLength(6);
4141
[true, true, true, true, false, false].forEach((display, i) => {
42-
expect(wrapper.findItems().at(i).props().display).toBe(display);
42+
expect(
43+
wrapper
44+
.findItems()
45+
.at(i)
46+
.props().display,
47+
).toBe(display);
4348
});
4449
expect(wrapper.findRest()).toHaveLength(1);
4550
expect(wrapper.findRest().text()).toEqual('+ 2 ...');

0 commit comments

Comments
 (0)