Skip to content

Commit 191e1af

Browse files
authored
Merge pull request #4 from react-component/fix-rest
fix: suffix calculation
2 parents d70bd15 + 77cf201 commit 191e1af

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/Overflow.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ function Overflow<ItemType = any>(
155155
const len = mergedData.length;
156156
const lastIndex = len - 1;
157157

158+
// When data count change to 0, reset this since not loop will reach
159+
if (!len) {
160+
updateDisplayCount(0);
161+
setSuffixFixedStart(null);
162+
return;
163+
}
164+
158165
for (let i = 0; i < len; i += 1) {
159166
const currentItemWidth = getItemWidth(i);
160167

@@ -179,7 +186,7 @@ function Overflow<ItemType = any>(
179186
// Can not hold all the content to show rest
180187
updateDisplayCount(i - 1);
181188
setSuffixFixedStart(
182-
totalWidth - currentItemWidth - suffixWidth + mergedRestWidth,
189+
totalWidth - currentItemWidth - suffixWidth + restWidth,
183190
);
184191
break;
185192
} else if (i === lastIndex) {
@@ -194,14 +201,7 @@ function Overflow<ItemType = any>(
194201
setSuffixFixedStart(null);
195202
}
196203
}
197-
}, [
198-
containerWidth,
199-
itemWidths,
200-
mergedRestWidth,
201-
suffixWidth,
202-
getKey,
203-
mergedData,
204-
]);
204+
}, [containerWidth, itemWidths, restWidth, suffixWidth, getKey, mergedData]);
205205

206206
// ================================ Render ================================
207207
const displayRest = restReady && !!omittedItems.length;
@@ -238,7 +238,8 @@ function Overflow<ItemType = any>(
238238
{/* Rest Count Item */}
239239
{showRest ? (
240240
<Item
241-
order={displayRest ? displayCount : mergedData.length}
241+
// When not show, order should be the last
242+
order={displayRest ? displayCount : Number.MAX_SAFE_INTEGER}
242243
prefixCls={itemPrefixCls}
243244
className={`${itemPrefixCls}-rest`}
244245
responsive={isResponsive}

tests/responsive.spec.tsx

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

189194
expect(wrapper.findSuffix().props().style.position).toBeFalsy();
190195
});
196+
197+
it('long to short should keep correct position', () => {
198+
const wrapper = mount(
199+
<Overflow<ItemType>
200+
data={getData(3)}
201+
itemKey="key"
202+
renderItem={renderItem}
203+
maxCount="responsive"
204+
suffix="Bamboo"
205+
/>,
206+
);
207+
208+
wrapper.initSize(20, 20);
209+
wrapper.setProps({ data: [] });
210+
211+
expect(wrapper.findRest().props().order).toEqual(Number.MAX_SAFE_INTEGER);
212+
expect(wrapper.findSuffix().props().style.position).toBeFalsy();
213+
});
191214
});
192215
});

0 commit comments

Comments
 (0)