Skip to content

Commit b5becb7

Browse files
committed
chore: Add overflow calculation
1 parent b0ebd21 commit b5becb7

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

examples/basic.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ interface ItemType {
88
label: string;
99
}
1010

11-
const originConsole = console.log;
12-
1311
function createData(count: number): ItemType[] {
1412
const data: ItemType[] = new Array(count).fill(undefined).map((_, index) => ({
1513
value: index,

src/Item.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface ItemProps<ItemType> {
1212
itemKey?: React.Key;
1313
registerSize: (key: React.Key, width: number) => void;
1414
children?: React.ReactNode;
15+
display?: boolean;
1516
}
1617

1718
export default function Item<ItemType>(props: ItemProps<ItemType>) {
@@ -24,6 +25,7 @@ export default function Item<ItemType>(props: ItemProps<ItemType>) {
2425
itemKey,
2526
className,
2627
children,
28+
display = true,
2729
} = props;
2830

2931
// ================================ Effect ================================
@@ -42,7 +44,12 @@ export default function Item<ItemType>(props: ItemProps<ItemType>) {
4244
const childNode = item !== undefined ? renderItem!(item) : children;
4345

4446
let itemNode = (
45-
<div className={classNames(prefixCls, className)}>{childNode}</div>
47+
<div
48+
className={classNames(prefixCls, className)}
49+
style={{ opacity: display ? 1 : 0.2 }}
50+
>
51+
{childNode}
52+
</div>
4653
);
4754

4855
if (!disabled) {

src/Overflow.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ function Overflow<ItemType = any>(
3939
);
4040
const [overflowWidth, setOverflowWidth] = createUseState(0);
4141

42+
const [displayCount, setDisplayCount] = React.useState(0);
43+
4244
const itemPrefixCls = `${prefixCls}-item`;
4345

4446
// ================================= Item =================================
@@ -79,6 +81,27 @@ function Overflow<ItemType = any>(
7981
setOverflowWidth(width);
8082
}
8183

84+
// ================================ Effect ================================
85+
React.useLayoutEffect(() => {
86+
console.log('Effect >>>', containerWidth, itemWidths, overflowWidth);
87+
if (containerWidth && overflowWidth && data) {
88+
let totalWidth = 0;
89+
90+
const len = data.length;
91+
92+
for (let i = 0; i < len; i += 1) {
93+
const itemWidth = itemWidths.get(getKey(data[i], i)) || 0;
94+
totalWidth += itemWidth;
95+
96+
if (totalWidth + overflowWidth > containerWidth) {
97+
setDisplayCount(i - 1);
98+
} else if (i === len - 1) {
99+
setDisplayCount(len - 1);
100+
}
101+
}
102+
}
103+
}, [containerWidth, itemWidths, overflowWidth, getKey, data]);
104+
82105
// ================================ Render ================================
83106
let overflowNode = (
84107
<div className={classNames(prefixCls, className)} style={style} ref={ref}>
@@ -94,6 +117,7 @@ function Overflow<ItemType = any>(
94117
itemKey={key}
95118
registerSize={registerSize}
96119
disabled={disabled}
120+
display={index <= displayCount}
97121
/>
98122
);
99123
})}

0 commit comments

Comments
 (0)