Skip to content

Commit c5707b6

Browse files
authored
chore: Always use virtual when scrollWidth is set (#229)
* chore: scrollWidth make it always in virtual * test: add test case
1 parent 9ae4bcb commit c5707b6

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

examples/horizontal-scroll.tsx

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,29 @@ const MyItem: React.ForwardRefRenderFunction<
7070

7171
const ForwardMyItem = React.forwardRef(MyItem);
7272

73-
const data: Item[] = [];
74-
for (let i = 0; i < 10000; i += 1) {
75-
data.push({
76-
id: `id_${i}`,
77-
height: 30 + Math.random() * 10,
78-
});
73+
function getData(count: number) {
74+
const data: Item[] = [];
75+
for (let i = 0; i < count; i += 1) {
76+
data.push({
77+
id: `id_${i}`,
78+
height: Math.round(30 + Math.random() * 10),
79+
});
80+
}
81+
return data;
7982
}
8083

8184
const Demo = () => {
8285
const [rtl, setRTL] = React.useState(false);
86+
const [count, setCount] = React.useState('1000');
87+
const [data, setData] = React.useState<Item[]>([]);
88+
89+
React.useEffect(() => {
90+
const num = Number(count);
91+
if (!Number.isNaN(num)) {
92+
setData(getData(num));
93+
}
94+
}, [count]);
95+
8396
return (
8497
<React.StrictMode>
8598
<div>
@@ -91,8 +104,19 @@ const Demo = () => {
91104
RTL: {String(rtl)}
92105
</button>
93106

107+
<input
108+
type="number"
109+
value={count}
110+
onChange={(e) => {
111+
const num = e.target.value;
112+
113+
setCount(num);
114+
}}
115+
/>
116+
94117
<div style={{ width: 500, margin: 64 }}>
95118
<List
119+
fullHeight={false}
96120
direction={rtl ? 'rtl' : 'ltr'}
97121
data={data}
98122
height={300}

src/List.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export interface ListProps<T> extends Omit<React.HTMLAttributes<any>, 'children'
6060
* By default `scrollWidth` is same as container.
6161
* When set this, it will show the horizontal scrollbar and
6262
* `scrollWidth` will be used as the real width instead of container width.
63+
* When set, `virtual` will always be enabled.
6364
*/
6465
scrollWidth?: number;
6566

@@ -106,7 +107,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
106107

107108
// ================================= MISC =================================
108109
const useVirtual = !!(virtual !== false && height && itemHeight);
109-
const inVirtual = useVirtual && data && itemHeight * data.length > height;
110+
const inVirtual = useVirtual && data && (itemHeight * data.length > height || !!scrollWidth);
110111
const isRTL = direction === 'rtl';
111112

112113
const mergedClassName = classNames(prefixCls, { [`${prefixCls}-rtl`]: isRTL }, className);

tests/scrollWidth.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,24 @@ describe('List.scrollWidth', () => {
152152
expect(onVirtualScroll).toHaveBeenCalledWith({ x: 123, y: 0 });
153153
});
154154

155+
it('trigger event when less count', async () => {
156+
const onVirtualScroll = jest.fn();
157+
158+
const { container } = await genList({
159+
itemHeight: ITEM_HEIGHT,
160+
height: 100,
161+
data: genData(1),
162+
scrollWidth: 1000,
163+
onVirtualScroll,
164+
});
165+
166+
// Wheel
167+
fireEvent.wheel(container.querySelector('.rc-virtual-list-holder')!, {
168+
deltaX: 123,
169+
});
170+
expect(onVirtualScroll).toHaveBeenCalledWith({ x: 123, y: 0 });
171+
});
172+
155173
it('shift wheel', async () => {
156174
const onVirtualScroll = jest.fn();
157175

0 commit comments

Comments
 (0)