Skip to content
This repository was archived by the owner on Aug 13, 2024. It is now read-only.

Commit 5fd8857

Browse files
committed
feat: exports list components
1 parent 6cfef44 commit 5fd8857

File tree

2 files changed

+67
-35
lines changed

2 files changed

+67
-35
lines changed

example/src/App.tsx

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import * as React from 'react';
22
import { useState } from 'react';
33

4-
import { FlatList, Pressable, StyleSheet, Text, View } from 'react-native';
5-
import { ScrollViewEnhancerView } from '@sendbird/react-native-scrollview-enhancer';
4+
import { Pressable, StyleSheet, Text, View } from 'react-native';
5+
import { FlatList } from '@sendbird/react-native-scrollview-enhancer';
66

77
let lastIdx = 0;
88
function messageFetcher(count: number) {
99
const response = new Array(count)
1010
.fill(0)
11-
.map((_, index) => {
12-
return {
13-
id: `${index}+${Date.now()}`,
14-
message: `Message ${lastIdx + index}`,
15-
};
16-
})
11+
.map((_, index) => ({
12+
id: `${index}+${Date.now()}`,
13+
message: `Message ${lastIdx + index}`,
14+
}))
1715
.reverse();
1816
lastIdx += count;
19-
console.log(lastIdx);
2017
return response;
2118
}
2219

@@ -28,39 +25,34 @@ export default function App() {
2825

2926
return (
3027
<View style={styles.container}>
31-
<ScrollViewEnhancerView
28+
<FlatList
29+
inverted
3230
style={styles.box}
3331
maintainVisibleContentPosition={{
3432
autoscrollToTopThreshold: 10,
3533
minIndexForVisible: 0,
3634
}}
37-
>
38-
<FlatList
39-
inverted
40-
style={styles.box}
41-
onLayout={(e) => console.log('flatlist', e.nativeEvent)}
42-
data={messages}
43-
keyExtractor={(item) => item.id}
44-
renderItem={({ item }) => {
45-
return (
46-
<View
47-
style={{
48-
width: '100%',
49-
backgroundColor: 'gray',
50-
marginBottom: 4,
51-
padding: 24,
52-
}}
53-
>
54-
<Text style={{ color: 'white' }}>{item.message}</Text>
55-
</View>
56-
);
57-
}}
58-
/>
59-
</ScrollViewEnhancerView>
35+
onLayout={(e) => console.log('flatlist', e.nativeEvent)}
36+
data={messages}
37+
keyExtractor={(item) => item.id}
38+
renderItem={({ item }) => {
39+
return (
40+
<View
41+
style={{
42+
width: '100%',
43+
backgroundColor: 'gray',
44+
marginBottom: 4,
45+
padding: 24,
46+
}}
47+
>
48+
<Text style={{ color: 'white' }}>{item.message}</Text>
49+
</View>
50+
);
51+
}}
52+
/>
6053
<Pressable
6154
style={{ width: 100, height: 100 }}
6255
onPress={() => {
63-
console.log('trigger');
6456
setMessages((prev) => [...messageFetcher(5), ...prev]);
6557
}}
6658
>

src/index.tsx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import React from 'react';
22
import {
3+
FlatList as RNFlatList,
34
Platform,
45
requireNativeComponent,
6+
ScrollView as RNScrollView,
57
ScrollViewProps,
8+
SectionList as RNSectionList,
9+
StyleProp,
610
UIManager,
711
ViewStyle,
812
} from 'react-native';
@@ -14,7 +18,8 @@ const LINKING_ERROR =
1418
'- You are not using Expo Go\n';
1519

1620
type ScrollViewEnhancerProps = React.PropsWithChildren<{
17-
style?: ViewStyle;
21+
style?: StyleProp<ViewStyle>;
22+
horizontal?: boolean | null;
1823
maintainVisibleContentPosition?: ScrollViewProps['maintainVisibleContentPosition'];
1924
}>;
2025

@@ -27,6 +32,41 @@ const NativeView =
2732
throw new Error(LINKING_ERROR);
2833
};
2934

35+
let _warningCalled = false;
36+
function warningOnHorizontalScroll(props: ScrollViewEnhancerProps) {
37+
if (props.horizontal && Platform.OS === 'android' && !_warningCalled) {
38+
_warningCalled = true;
39+
console.warn(
40+
'ScrollViewEnhancerView does not support horizontal direction'
41+
);
42+
}
43+
}
44+
3045
export const ScrollViewEnhancerView = (props: ScrollViewEnhancerProps) => {
46+
if (__DEV__) warningOnHorizontalScroll(props);
3147
return <NativeView {...props} />;
3248
};
49+
50+
export const enhanceScrollView = <T extends React.ComponentType<any>>(
51+
Component: T
52+
): T => {
53+
return ((props: any) => {
54+
if (Platform.OS === 'android') {
55+
return (
56+
<ScrollViewEnhancerView
57+
style={props.style}
58+
horizontal={props.horizontal}
59+
maintainVisibleContentPosition={props.maintainVisibleContentPosition}
60+
>
61+
<Component {...props} />
62+
</ScrollViewEnhancerView>
63+
);
64+
} else {
65+
return <Component {...props} />;
66+
}
67+
}) as T;
68+
};
69+
70+
export const ScrollView = enhanceScrollView(RNScrollView);
71+
export const FlatList = enhanceScrollView(RNFlatList);
72+
export const SectionList = enhanceScrollView(RNSectionList);

0 commit comments

Comments
 (0)