Skip to content

Commit ec60400

Browse files
authored
fix: Last one also take in count (#13)
* fix: Last one also take in count * test: Fix test case * test: Update test case
1 parent e1c0a44 commit ec60400

File tree

2 files changed

+69
-42
lines changed

2 files changed

+69
-42
lines changed

src/Overflow.tsx

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ import Item from './Item';
66
import { useBatchFrameState } from './hooks/useBatchFrameState';
77
import RawItem from './RawItem';
88

9-
export const OverflowContext = React.createContext<{
10-
prefixCls: string;
11-
responsive: boolean;
12-
order: number;
13-
registerSize: (key: React.Key, width: number | null) => void;
14-
display: boolean;
9+
export const OverflowContext =
10+
React.createContext<{
11+
prefixCls: string;
12+
responsive: boolean;
13+
order: number;
14+
registerSize: (key: React.Key, width: number | null) => void;
15+
display: boolean;
1516

16-
invalidate: boolean;
17+
invalidate: boolean;
1718

18-
// Item Usage
19-
item?: any;
20-
itemKey?: React.Key;
19+
// Item Usage
20+
item?: any;
21+
itemKey?: React.Key;
2122

22-
// Rest Usage
23-
className?: string;
24-
}>(null);
23+
// Rest Usage
24+
className?: string;
25+
}>(null);
2526

2627
const RESPONSIVE = 'responsive' as const;
2728
const INVALIDATE = 'invalidate' as const;
@@ -90,7 +91,7 @@ function Overflow<ItemType = any>(
9091

9192
const createUseState = useBatchFrameState();
9293

93-
const fullySSR = ssr === 'full'
94+
const fullySSR = ssr === 'full';
9495

9596
const [containerWidth, setContainerWidth] = createUseState<number>(null);
9697
const mergedContainerWidth = containerWidth || 0;
@@ -106,13 +107,13 @@ function Overflow<ItemType = any>(
106107
const [suffixFixedStart, setSuffixFixedStart] = useState<number>(null);
107108

108109
const [displayCount, setDisplayCount] = useState(null);
109-
const mergedDisplayCount = React.useMemo(() => {
110-
if (displayCount === null && fullySSR) {
111-
return Number.MAX_SAFE_INTEGER;
112-
}
110+
const mergedDisplayCount = React.useMemo(() => {
111+
if (displayCount === null && fullySSR) {
112+
return Number.MAX_SAFE_INTEGER;
113+
}
113114

114-
return displayCount || 0;
115-
}, [displayCount, containerWidth]);
115+
return displayCount || 0;
116+
}, [displayCount, containerWidth]);
116117

117118
const [restReady, setRestReady] = useState(false);
118119

@@ -241,8 +242,11 @@ function Overflow<ItemType = any>(
241242
totalWidth += currentItemWidth;
242243

243244
if (
244-
i === lastIndex - 1 &&
245-
totalWidth + getItemWidth(lastIndex)! <= mergedContainerWidth
245+
// Only one means `totalWidth` is the final width
246+
(lastIndex === 0 && totalWidth <= mergedContainerWidth) ||
247+
// Last two width will be the final width
248+
(i === lastIndex - 1 &&
249+
totalWidth + getItemWidth(lastIndex)! <= mergedContainerWidth)
246250
) {
247251
// Additional check if match the end
248252
updateDisplayCount(lastIndex);
@@ -255,11 +259,6 @@ function Overflow<ItemType = any>(
255259
totalWidth - currentItemWidth - suffixWidth + restWidth,
256260
);
257261
break;
258-
} else if (i === lastIndex) {
259-
// Reach the end
260-
updateDisplayCount(lastIndex);
261-
setSuffixFixedStart(totalWidth - suffixWidth);
262-
break;
263262
}
264263
}
265264

@@ -425,9 +424,9 @@ type FilledOverflowType = ForwardOverflowType & {
425424

426425
ForwardOverflow.displayName = 'Overflow';
427426

428-
((ForwardOverflow as unknown) as FilledOverflowType).Item = RawItem;
429-
((ForwardOverflow as unknown) as FilledOverflowType).RESPONSIVE = RESPONSIVE;
430-
((ForwardOverflow as unknown) as FilledOverflowType).INVALIDATE = INVALIDATE;
427+
(ForwardOverflow as unknown as FilledOverflowType).Item = RawItem;
428+
(ForwardOverflow as unknown as FilledOverflowType).RESPONSIVE = RESPONSIVE;
429+
(ForwardOverflow as unknown as FilledOverflowType).INVALIDATE = INVALIDATE;
431430

432431
// Convert to generic type
433-
export default (ForwardOverflow as unknown) as FilledOverflowType;
432+
export default ForwardOverflow as unknown as FilledOverflowType;

tests/responsive.spec.tsx

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +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(
44-
wrapper
45-
.findItems()
46-
.at(i)
47-
.props().display,
48-
).toBe(display);
43+
expect(wrapper.findItems().at(i).props().display).toBe(display);
4944
});
5045
expect(wrapper.findRest()).toHaveLength(1);
5146
expect(wrapper.findRest().text()).toEqual('+ 2 ...');
5247
expect(
53-
wrapper
54-
.findItems()
55-
.find('div')
56-
.last()
57-
.prop('aria-hidden'),
48+
wrapper.findItems().find('div').last().prop('aria-hidden'),
5849
).toBeTruthy();
5950
});
6051

@@ -73,6 +64,21 @@ describe('Overflow.Responsive', () => {
7364
expect(wrapper.findRest().props().display).toBeFalsy();
7465
});
7566

67+
it('just fit', () => {
68+
const wrapper = mount(
69+
<Overflow<ItemType>
70+
data={getData(1)}
71+
itemKey="key"
72+
renderItem={renderItem}
73+
maxCount="responsive"
74+
/>,
75+
);
76+
wrapper.initSize(20, 20);
77+
78+
expect(wrapper.findItems()).toHaveLength(1);
79+
expect(wrapper.findRest().props().display).toBeFalsy();
80+
});
81+
7682
it('remove to clean up', () => {
7783
const data = getData(6);
7884

@@ -219,4 +225,26 @@ describe('Overflow.Responsive', () => {
219225
expect(wrapper.findSuffix().props().style.position).toBeFalsy();
220226
});
221227
});
228+
229+
it('render rest directly', () => {
230+
const wrapper = mount(
231+
<Overflow<ItemType>
232+
data={getData(10)}
233+
itemKey="key"
234+
renderItem={renderItem}
235+
maxCount="responsive"
236+
renderRawRest={omitItems => {
237+
return (
238+
<Overflow.Item component="span" className="custom-rest">
239+
{omitItems.length}
240+
</Overflow.Item>
241+
);
242+
}}
243+
/>,
244+
);
245+
246+
wrapper.initSize(100, 20);
247+
248+
expect(wrapper.find('span.custom-rest').text()).toEqual('6');
249+
});
222250
});

0 commit comments

Comments
 (0)