forked from hyochan/react-native-masonry-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
166 lines (156 loc) · 4.84 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import type {MutableRefObject, ReactElement} from 'react';
import React, {memo, useState} from 'react';
import type {
NativeScrollEvent,
RefreshControlProps,
ScrollViewProps,
StyleProp,
ViewStyle,
} from 'react-native';
import {RefreshControl, ScrollView, View} from 'react-native';
interface Props<T> extends Omit<ScrollViewProps, 'refreshControl'> {
innerRef?: MutableRefObject<ScrollView | undefined>;
loading?: boolean;
refreshing?: RefreshControlProps['refreshing'];
onRefresh?: RefreshControlProps['onRefresh'];
refreshControl?: boolean;
onEndReached?: () => void;
onEndReachedThreshold?: number;
style?: StyleProp<ViewStyle>;
data: T[];
renderItem: ({item, i}: {item: T; i: number}) => ReactElement;
LoadingView?: React.ComponentType<any> | React.ReactElement | null;
ListHeaderComponent?: React.ReactNode | null;
ListEmptyComponent?: React.ComponentType<any> | React.ReactElement | null;
ListFooterComponent?: React.ComponentType<any> | React.ReactElement | null;
ListHeaderComponentStyle?: StyleProp<ViewStyle>;
contentContainerStyle?: StyleProp<ViewStyle>;
containerStyle?: StyleProp<ViewStyle>;
numColumns?: number;
keyExtractor?: ((item: T | any, index: number) => string) | undefined;
refreshControlProps?: Omit<RefreshControlProps, 'onRefresh' | 'refreshing'>;
}
const isCloseToBottom = (
{layoutMeasurement, contentOffset, contentSize}: NativeScrollEvent,
onEndReachedThreshold: number,
): boolean => {
const paddingToBottom = contentSize.height * onEndReachedThreshold;
return (
Math.ceil(layoutMeasurement.height + contentOffset.y) >=
contentSize.height - paddingToBottom
);
};
function MasonryList<T>(props: Props<T>): ReactElement {
const [isRefreshing, setIsRefreshing] = useState<boolean>(false);
const {
refreshing,
data,
innerRef,
ListHeaderComponent,
ListEmptyComponent,
ListFooterComponent,
ListHeaderComponentStyle,
containerStyle,
contentContainerStyle,
renderItem,
onEndReachedThreshold,
onEndReached,
onRefresh,
loading,
LoadingView,
numColumns = 2,
horizontal,
onScroll,
removeClippedSubviews = false,
keyExtractor,
keyboardShouldPersistTaps = 'handled',
refreshControl = true,
refreshControlProps,
} = props;
const {style, ...propsWithoutStyle} = props;
return (
<ScrollView
{...propsWithoutStyle}
ref={innerRef}
style={[{flex: 1, alignSelf: 'stretch'}, containerStyle]}
contentContainerStyle={contentContainerStyle}
keyboardShouldPersistTaps={keyboardShouldPersistTaps}
removeClippedSubviews={removeClippedSubviews}
refreshControl={
refreshControl ? (
<RefreshControl
refreshing={!!(refreshing || isRefreshing)}
onRefresh={() => {
setIsRefreshing(true);
onRefresh?.();
setIsRefreshing(false);
}}
{...refreshControlProps}
/>
) : null
}
scrollEventThrottle={16}
onScroll={(e) => {
const nativeEvent: NativeScrollEvent = e.nativeEvent;
if (isCloseToBottom(nativeEvent, onEndReachedThreshold || 0.0)) {
onEndReached?.();
}
onScroll?.(e);
}}
>
<>
<View style={ListHeaderComponentStyle}>{ListHeaderComponent}</View>
{data.length === 0 && ListEmptyComponent ? (
React.isValidElement(ListEmptyComponent) ? (
ListEmptyComponent
) : (
// @ts-ignore
<ListEmptyComponent />
)
) : (
<View
style={[
{
flex: 1,
flexDirection: horizontal ? 'column' : 'row',
},
style,
]}
>
{Array.from(Array(numColumns), (_, num) => {
return (
<View
key={`masonry-column-${num}`}
style={{
flex: 1 / numColumns,
flexDirection: horizontal ? 'row' : 'column',
}}
>
{data
.map((el, i) => {
if (i % numColumns === num) {
return (
<View
key={
keyExtractor?.(el, i) || `masonry-row-${num}-${i}`
}
>
{renderItem({item: el, i})}
</View>
);
}
return null;
})
.filter((e) => !!e)}
</View>
);
})}
</View>
)}
{loading && LoadingView}
{ListFooterComponent}
</>
</ScrollView>
);
}
export default memo(MasonryList);