Skip to content

Commit 582ae7a

Browse files
committed
refactor: rename related variables
1 parent 966c5e4 commit 582ae7a

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

examples/basic.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function renderItem(item: ItemType) {
3232
}
3333

3434
const Demo = () => {
35-
const [responsive, setResponsive] = React.useState(false);
35+
const [responsive, setResponsive] = React.useState(true);
3636
const [data, setData] = React.useState(createData(5));
3737

3838
return (

src/Item.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface ItemProps<ItemType> {
77
item?: ItemType;
88
className?: string;
99
renderItem?: (item: ItemType) => React.ReactNode;
10-
disabled?: boolean;
10+
responsive?: boolean;
1111
itemKey?: React.Key;
1212
registerSize: (key: React.Key, width: number) => void;
1313
children?: React.ReactNode;
@@ -20,7 +20,7 @@ export default function Item<ItemType>(props: ItemProps<ItemType>) {
2020
prefixCls,
2121
item,
2222
renderItem,
23-
disabled,
23+
responsive,
2424
registerSize,
2525
itemKey,
2626
className,
@@ -49,17 +49,17 @@ export default function Item<ItemType>(props: ItemProps<ItemType>) {
4949
className={classNames(prefixCls, className)}
5050
style={{
5151
opacity: display ? 1 : 0.2,
52-
// height: display ? undefined : 0,
53-
// overflowY: disabled ? undefined : 'hidden',
54-
order,
52+
height: display ? undefined : 0,
53+
overflowY: responsive ? 'hidden' : undefined,
54+
order: responsive ? order : undefined,
5555
pointerEvents: 'none',
5656
}}
5757
>
5858
{childNode}
5959
</div>
6060
);
6161

62-
if (!disabled) {
62+
if (responsive) {
6363
itemNode = (
6464
<ResizeObserver
6565
onResize={({ offsetWidth }) => {

src/Overflow.tsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,33 @@ function Overflow<ItemType = any>(
3030
itemWidth = 10,
3131
style,
3232
className,
33-
maxCount = RESPONSIVE,
33+
maxCount,
3434
} = props;
3535

3636
const createUseState = useBatchFrameState();
37-
const disabled = maxCount !== RESPONSIVE;
37+
// const disabled = maxCount !== RESPONSIVE;
3838

3939
const [containerWidth, setContainerWidth] = createUseState(0);
4040
const [itemWidths, setItemWidths] = createUseState(
4141
new Map<React.Key, number>(),
4242
);
43-
const [overflowWidth, setOverflowWidth] = createUseState(0);
43+
const [restWidth, setRestWidth] = createUseState(0);
4444

4545
const [displayCount, setDisplayCount] = React.useState(0);
4646
const [restReady, setRestReady] = React.useState(false);
4747

4848
const itemPrefixCls = `${prefixCls}-item`;
4949

50+
// ================================= Data =================================
51+
const isResponsive = maxCount === RESPONSIVE;
52+
53+
const mergedData = React.useMemo(() => {
54+
if (isResponsive) {
55+
return data.slice(0, Math.min(data.length, containerWidth / itemWidth));
56+
}
57+
return data;
58+
}, [data, itemWidth, containerWidth, maxCount]);
59+
5060
// ================================= Item =================================
5161
const getKey = React.useCallback(
5262
(item: ItemType, index: number) => {
@@ -89,18 +99,12 @@ function Overflow<ItemType = any>(
8999
}
90100

91101
function registerOverflowSize(_: React.Key, width: number) {
92-
setOverflowWidth(width);
102+
setRestWidth(width);
93103
}
94104

95-
// ================================= Data =================================
96-
const mergedData = React.useMemo(
97-
() => data.slice(0, Math.min(data.length, containerWidth / itemWidth)),
98-
[data, itemWidth, containerWidth],
99-
);
100-
101105
// ================================ Effect ================================
102106
React.useLayoutEffect(() => {
103-
if (containerWidth && overflowWidth && mergedData) {
107+
if (containerWidth && restWidth && mergedData) {
104108
let totalWidth = 0;
105109

106110
const len = mergedData.length;
@@ -117,7 +121,7 @@ function Overflow<ItemType = any>(
117121
// Find best match
118122
totalWidth += currentItemWidth;
119123

120-
if (totalWidth + overflowWidth > containerWidth) {
124+
if (totalWidth + restWidth > containerWidth) {
121125
updateDisplayCount(i - 1);
122126
break;
123127
} else if (i === len - 1) {
@@ -126,7 +130,7 @@ function Overflow<ItemType = any>(
126130
}
127131
}
128132
}
129-
}, [containerWidth, itemWidths, overflowWidth, getKey, mergedData]);
133+
}, [containerWidth, itemWidths, restWidth, getKey, mergedData]);
130134

131135
// ================================ Render ================================
132136
let overflowNode = (
@@ -143,7 +147,7 @@ function Overflow<ItemType = any>(
143147
renderItem={mergedRenderItem}
144148
itemKey={key}
145149
registerSize={registerSize}
146-
disabled={disabled}
150+
responsive={isResponsive}
147151
display={index <= displayCount}
148152
/>
149153
);
@@ -154,7 +158,7 @@ function Overflow<ItemType = any>(
154158
order={displayCount}
155159
prefixCls={itemPrefixCls}
156160
className={`${itemPrefixCls}-rest`}
157-
disabled={disabled}
161+
responsive={isResponsive}
158162
registerSize={registerOverflowSize}
159163
display={restReady}
160164
>
@@ -163,7 +167,7 @@ function Overflow<ItemType = any>(
163167
</div>
164168
);
165169

166-
if (!disabled) {
170+
if (isResponsive) {
167171
overflowNode = (
168172
<ResizeObserver onResize={onOverflowResize}>
169173
{overflowNode}

0 commit comments

Comments
 (0)