Skip to content

Commit ed69044

Browse files
authored
Merge c098457 into 214131c
2 parents 214131c + c098457 commit ed69044

File tree

14 files changed

+554
-111
lines changed

14 files changed

+554
-111
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,16 @@ import List from 'rc-virtual-list';
5151

5252
## List
5353

54-
| Prop | Description | Type | Default |
55-
| ---------- | ------------------------------------------------------- | ------------------------------------ | ------- |
56-
| children | Render props of item | (item, index, props) => ReactElement | - |
57-
| component | Customize List dom element | string \| Component | div |
58-
| data | Data list | Array | - |
59-
| disabled | Disable scroll check. Usually used on animation control | boolean | false |
60-
| height | List height | number | - |
61-
| itemHeight | Item minium height | number | - |
62-
| itemKey | Match key with item | string | - |
54+
| Prop | Description | Type | Default |
55+
| ---------------- | ------------------------------------------------------- | ------------------------------------ | ------- |
56+
| children | Render props of item | (item, index, props) => ReactElement | - |
57+
| component | Customize List dom element | string \| Component | div |
58+
| data | Data list | Array | - |
59+
| disabled | Disable scroll check. Usually used on animation control | boolean | false |
60+
| height | List height | number | - |
61+
| itemHeight | Item minium height | number | - |
62+
| itemKey | Match key with item | string | - |
63+
| horizontalScroll | enable horizontal scroll | boolean | - |
6364

6465
`children` provides additional `props` argument to support IE 11 scroll shaking.
6566
It will set `style` to `visibility: hidden` when measuring. You can ignore this if no requirement on IE.

docs/demo/horizontal-scroll.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## horizontal scroll
2+
3+
<code src="../../examples/horizontal-scroll.tsx">

examples/horizontal-scroll.tsx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as React from 'react';
2+
import List from '../src/List';
3+
4+
interface Item {
5+
id: number;
6+
height: number;
7+
}
8+
9+
const MyItem: React.ForwardRefRenderFunction<HTMLElement, Item> = ({ id, height }, ref) => {
10+
return (
11+
<span
12+
ref={ref}
13+
style={{
14+
border: '1px solid gray',
15+
padding: '0 16px',
16+
height,
17+
lineHeight: '30px',
18+
boxSizing: 'border-box',
19+
display: 'inline-block',
20+
}}
21+
>
22+
longtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtext
23+
</span>
24+
);
25+
};
26+
27+
const ForwardMyItem = React.forwardRef(MyItem);
28+
29+
const data: Item[] = [];
30+
for (let i = 0; i < 100; i += 1) {
31+
data.push({
32+
id: i,
33+
height: 30,
34+
});
35+
}
36+
37+
const Demo = () => {
38+
return (
39+
<React.StrictMode>
40+
<div>
41+
<h2>Dynamic Height</h2>
42+
43+
<List
44+
data={data}
45+
height={500}
46+
itemHeight={30}
47+
itemKey="id"
48+
horizontalScroll
49+
style={{
50+
border: '1px solid red',
51+
boxSizing: 'border-box',
52+
}}
53+
>
54+
{(item) => <ForwardMyItem {...item} />}
55+
</List>
56+
</div>
57+
</React.StrictMode>
58+
);
59+
};
60+
61+
export default Demo;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"dependencies": {
6666
"@babel/runtime": "^7.20.0",
6767
"classnames": "^2.2.6",
68-
"rc-resize-observer": "^1.0.0",
69-
"rc-util": "^5.15.0"
68+
"rc-resize-observer": "^1.3.1",
69+
"rc-util": "^5.34.1"
7070
}
7171
}

src/Filler.tsx

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,65 @@ interface FillerProps {
1111
/** Set offset of visible items. Should be the top of start item position */
1212
offset?: number;
1313

14+
horizontalScroll?: boolean;
15+
1416
children: React.ReactNode;
1517

18+
fillerOuterRef: React.MutableRefObject<HTMLDivElement>;
19+
1620
onInnerResize?: () => void;
1721

1822
innerProps?: InnerProps;
23+
24+
onWidthChange: (width: number) => void;
25+
26+
onScrollWidthChange: (scrollWidth: number) => void;
1927
}
2028

2129
/**
2230
* Fill component to provided the scroll content real height.
2331
*/
2432
const Filler = React.forwardRef(
2533
(
26-
{ height, offset, children, prefixCls, onInnerResize, innerProps }: FillerProps,
34+
{
35+
height,
36+
offset,
37+
children,
38+
prefixCls,
39+
horizontalScroll,
40+
fillerOuterRef,
41+
onInnerResize,
42+
innerProps,
43+
onWidthChange,
44+
onScrollWidthChange,
45+
}: FillerProps,
2746
ref: React.Ref<HTMLDivElement>,
2847
) => {
2948
let outerStyle: React.CSSProperties = {};
3049

3150
let innerStyle: React.CSSProperties = {
32-
display: 'flex',
51+
display: horizontalScroll ? 'inline-flex' : 'flex',
3352
flexDirection: 'column',
3453
};
3554

3655
if (offset !== undefined) {
37-
outerStyle = { height, position: 'relative', overflow: 'hidden' };
56+
outerStyle = {
57+
height,
58+
position: 'relative',
59+
overflow: 'hidden',
60+
};
3861

3962
innerStyle = {
4063
...innerStyle,
4164
transform: `translateY(${offset}px)`,
42-
position: 'absolute',
43-
left: 0,
44-
right: 0,
45-
top: 0,
4665
};
4766
}
4867

49-
return (
50-
<div style={outerStyle}>
68+
const outerContainer = (
69+
<div ref={fillerOuterRef} style={outerStyle}>
5170
<ResizeObserver
52-
onResize={({ offsetHeight }) => {
71+
onResize={({ offsetWidth, offsetHeight }) => {
72+
onScrollWidthChange(offsetWidth);
5373
if (offsetHeight && onInnerResize) {
5474
onInnerResize();
5575
}
@@ -68,6 +88,18 @@ const Filler = React.forwardRef(
6888
</ResizeObserver>
6989
</div>
7090
);
91+
92+
return horizontalScroll ? (
93+
<ResizeObserver
94+
onResize={({ offsetWidth }) => {
95+
onWidthChange(offsetWidth);
96+
}}
97+
>
98+
{outerContainer}
99+
</ResizeObserver>
100+
) : (
101+
outerContainer
102+
);
71103
},
72104
);
73105

0 commit comments

Comments
 (0)