Skip to content

Commit 0a79caa

Browse files
committed
feat: Auto order of rest Item
1 parent 7958475 commit 0a79caa

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

assets/index.less

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
// overflow: hidden;
88

99
&-item {
10-
margin-left: 0.5em;
11-
margin-right: 1em;
1210
background: rgba(0, 255, 0, 0.2);
13-
padding: 4px 8px;
14-
border: 1px solid red;
11+
box-shadow: 0 0 1px black;
12+
flex: none;
1513
}
1614
}

examples/basic.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,26 @@ interface ItemType {
1111
function createData(count: number): ItemType[] {
1212
const data: ItemType[] = new Array(count).fill(undefined).map((_, index) => ({
1313
value: index,
14-
label: `Label${index}`,
14+
label: `Label ${index}`,
1515
}));
1616

1717
return data;
1818
}
1919

20+
function renderItem(item: ItemType) {
21+
return (
22+
<div
23+
style={{
24+
margin: '0 16px 0 8px',
25+
padding: '4px 8px',
26+
background: 'rgba(255, 0, 0, 0.2)',
27+
}}
28+
>
29+
{item.label}
30+
</div>
31+
);
32+
}
33+
2034
const Demo = () => {
2135
const [data, setData] = React.useState(createData(5));
2236

@@ -44,7 +58,7 @@ const Demo = () => {
4458
marginTop: 32,
4559
}}
4660
>
47-
<Overflow<ItemType> data={data} renderItem={(item) => item.label} />
61+
<Overflow<ItemType> data={data} renderItem={renderItem} />
4862
</div>
4963
</div>
5064
);

src/Item.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as React from 'react';
22
import classNames from 'classnames';
33
import ResizeObserver from 'rc-resize-observer';
4-
import { getFullWidth } from './util';
54

65
export interface ItemProps<ItemType> {
76
prefixCls: string;
@@ -13,6 +12,7 @@ export interface ItemProps<ItemType> {
1312
registerSize: (key: React.Key, width: number) => void;
1413
children?: React.ReactNode;
1514
display?: boolean;
15+
order: number;
1616
}
1717

1818
export default function Item<ItemType>(props: ItemProps<ItemType>) {
@@ -26,6 +26,7 @@ export default function Item<ItemType>(props: ItemProps<ItemType>) {
2626
className,
2727
children,
2828
display = true,
29+
order,
2930
} = props;
3031

3132
// ================================ Effect ================================
@@ -46,7 +47,7 @@ export default function Item<ItemType>(props: ItemProps<ItemType>) {
4647
let itemNode = (
4748
<div
4849
className={classNames(prefixCls, className)}
49-
style={{ opacity: display ? 1 : 0.2 }}
50+
style={{ opacity: display ? 1 : 0.2, order }}
5051
>
5152
{childNode}
5253
</div>
@@ -55,9 +56,8 @@ export default function Item<ItemType>(props: ItemProps<ItemType>) {
5556
if (!disabled) {
5657
itemNode = (
5758
<ResizeObserver
58-
onResize={(_, element) => {
59-
const width = getFullWidth(element);
60-
internalRegisterSize(width);
59+
onResize={({ offsetWidth }) => {
60+
internalRegisterSize(offsetWidth);
6161
}}
6262
>
6363
{itemNode}

src/Overflow.tsx

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ function Overflow<ItemType = any>(
6161
// ================================= Size =================================
6262
function onOverflowResize(_: object, element: HTMLElement) {
6363
setContainerWidth(element.clientWidth);
64-
console.log('~~~???>>>', element.clientWidth, element);
6564
}
6665

6766
function registerSize(key: React.Key, width: number) {
@@ -83,7 +82,6 @@ function Overflow<ItemType = any>(
8382

8483
// ================================ Effect ================================
8584
React.useLayoutEffect(() => {
86-
console.log('Effect >>>', containerWidth, itemWidths, overflowWidth);
8785
if (containerWidth && overflowWidth && data) {
8886
let totalWidth = 0;
8987

@@ -92,16 +90,6 @@ function Overflow<ItemType = any>(
9290
for (let i = 0; i < len; i += 1) {
9391
const itemWidth = itemWidths.get(getKey(data[i], i)) || 0;
9492
totalWidth += itemWidth;
95-
console.log(
96-
i,
97-
'>>>',
98-
totalWidth,
99-
itemWidth,
100-
overflowWidth,
101-
'|',
102-
totalWidth + overflowWidth,
103-
containerWidth,
104-
);
10593

10694
if (totalWidth + overflowWidth > containerWidth) {
10795
setDisplayCount(i - 1);
@@ -122,6 +110,7 @@ function Overflow<ItemType = any>(
122110

123111
return (
124112
<Item<ItemType>
113+
order={index}
125114
key={key}
126115
item={item}
127116
prefixCls={itemPrefixCls}
@@ -134,9 +123,11 @@ function Overflow<ItemType = any>(
134123
);
135124
})}
136125

126+
{/* Rest Count Item */}
137127
<Item
128+
order={displayCount}
138129
prefixCls={itemPrefixCls}
139-
className={`${itemPrefixCls}-overflow`}
130+
className={`${itemPrefixCls}-rest`}
140131
disabled={disabled}
141132
registerSize={registerOverflowSize}
142133
>

0 commit comments

Comments
 (0)