Skip to content

Commit

Permalink
Feat rn masonry example (#79)
Browse files Browse the repository at this point in the history
* feat: update

* feat: MasonryList react native example

* feat: MasonryList react native example

* feat: bump expo 0.52

* feat: lock initNumberToRender = 0

* fix: snap jump issue

* fix: snpm jump issue
  • Loading branch information
ryuever authored Dec 4, 2024
1 parent 0ac8d78 commit ab267a2
Show file tree
Hide file tree
Showing 26 changed files with 1,701 additions and 2,019 deletions.
3 changes: 2 additions & 1 deletion examples/ReactNativeListPlayground/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"favicon": "./assets/images/favicon.png"
},
"plugins": [
"expo-router"
"expo-router",
"expo-font"
],
"experiments": {
"typedRoutes": true
Expand Down
51 changes: 51 additions & 0 deletions examples/ReactNativeListPlayground/app/(tabs)/List.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useCallback, useMemo, useRef } from 'react';
import { List, ScrollView } from '@infinite-list/react-native';
import {
// NativeScrollEvent,
// NativeSyntheticEvent,
Text,
View,
} from 'react-native';

const buildData = (count: number, startIndex = 0) =>
new Array(count).fill(1).map((v, index) => ({
key: index + startIndex,
value: index + startIndex,
}));

export default () => {
const data = useMemo(() => buildData(10000), []);
const scrollViewRef = useRef<ScrollView>(null);

const renderItem = useCallback((props: { item }) => {
const { item } = props;
return (
<View style={{ height: 80, width: '100%', backgroundColor: '#fff' }}>
<Text>{item.value}</Text>
</View>
);
}, []);

const keyExtractor = useCallback((item) => {
return item.key;
}, []);

return (
<ScrollView
ref={scrollViewRef}
// onScroll={onScroll}
contentContainerStyle={{
backgroundColor: '#fff',
}}
>
<List
data={data}
renderItem={renderItem}
test="3"
id="basic"
keyExtractor={keyExtractor}
containerRef={scrollViewRef}
/>
</ScrollView>
);
};
57 changes: 57 additions & 0 deletions examples/ReactNativeListPlayground/app/(tabs)/MasonryList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { useCallback, useMemo, useRef } from 'react';
import { ScrollView as NativeScrollView } from 'react-native';
import { MasonryList, ScrollView } from '@infinite-list/react-native';
import { Text, View } from 'react-native';

const buildData = (count: number, startIndex = 0) =>
new Array(count).fill(1).map((v, index) => ({
key: index + startIndex,
value: index + startIndex,
}));

export default () => {
const data = useMemo(() => buildData(10000), []);
const scrollViewRef = useRef<NativeScrollView>(null);

const renderItem = useCallback((props: { item }) => {
const { item, itemMeta } = props;
const index = itemMeta.getIndexInfo().index;
const totalIndex = itemMeta.getIndexInfo().indexInTotal;

return (
<View
style={{
height: totalIndex % 3 ? 50 : 75,
flex: 1,
width: '100%',
backgroundColor: index % 2 ? '#fff' : '#eee',
}}
>
<Text>{item.value}</Text>
</View>
);
}, []);

const keyExtractor = useCallback((item) => {
return item.key;
}, []);

return (
<ScrollView
ref={scrollViewRef}
contentContainerStyle={{
backgroundColor: '#fff',
}}
>
<MasonryList
data={data}
renderItem={renderItem}
id="basic"
recyclerBufferSize={100}
recyclerReservedBufferPerBatch={50}
keyExtractor={keyExtractor}
containerRef={scrollViewRef}
/>
</ScrollView>
);
};
160 changes: 84 additions & 76 deletions examples/ReactNativeListPlayground/app/(tabs)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,82 +84,90 @@
// },
// });

import { useCallback, useMemo, useRef } from 'react';
import { List, ScrollView } from '@infinite-list/react-native';
import {
// NativeScrollEvent,
// NativeSyntheticEvent,
Text,
View,
} from 'react-native';

const buildData = (count: number, startIndex = 0) =>
new Array(count).fill(1).map((v, index) => ({
key: index + startIndex,
value: index + startIndex,
}));
// import { useCallback, useMemo, useRef } from 'react';
// import { List, ScrollView } from '@infinite-list/react-native';
// import {
// // NativeScrollEvent,
// // NativeSyntheticEvent,
// Text,
// View,
// } from 'react-native';

// const buildData = (count: number, startIndex = 0) =>
// new Array(count).fill(1).map((v, index) => ({
// key: index + startIndex,
// value: index + startIndex,
// }));

// export default () => {
// const data = useMemo(() => buildData(10000), []);
// const scrollViewRef = useRef<ScrollView>(null);

// const renderItem = useCallback((props: { item }) => {
// const { item } = props;
// return (
// <View style={{ height: 80, width: '100%', backgroundColor: '#fff' }}>
// <Text>{item.value}</Text>
// </View>
// );
// }, []);

// const keyExtractor = useCallback((item) => {
// return item.key;
// }, []);

// // const subscriptions = useMemo<{
// // [key: string]: Function[];
// // }>(() => {
// // return {};
// // }, []);

// // const events = useMemo(() => {
// // return {
// // addEventListener: (eventName: string, fn: Function) => {
// // if (!subscriptions[eventName]) subscriptions[eventName] = [];
// // subscriptions[eventName] = ([] as Function[]).concat(
// // subscriptions[eventName],
// // fn
// // );
// // },
// // };
// // }, []);

// // const onScroll = useCallback(
// // (event: NativeSyntheticEvent<NativeScrollEvent>) => {
// // const cbs = subscriptions['onScroll'];
// // if (cbs?.length) {
// // cbs.forEach((cb) => cb(event));
// // }
// // },
// // []
// // );

// return (
// <ScrollView
// ref={scrollViewRef}
// // onScroll={onScroll}
// contentContainerStyle={{
// backgroundColor: '#fff',
// }}
// >
// <List
// data={data}
// renderItem={renderItem}
// test="3"
// id="basic"
// keyExtractor={keyExtractor}
// containerRef={scrollViewRef}
// />
// </ScrollView>
// );
// };

import List from './List';
import MasonryList from './MasonryList';
import { Text } from 'react-native';

export default () => {
const data = useMemo(() => buildData(10000), []);
const scrollViewRef = useRef<ScrollView>(null);

const renderItem = useCallback((props: { item }) => {
const { item } = props;
return (
<View style={{ height: 80, width: '100%', backgroundColor: '#fff' }}>
<Text>{item.value}</Text>
</View>
);
}, []);

const keyExtractor = useCallback((item) => {
return item.key;
}, []);

// const subscriptions = useMemo<{
// [key: string]: Function[];
// }>(() => {
// return {};
// }, []);

// const events = useMemo(() => {
// return {
// addEventListener: (eventName: string, fn: Function) => {
// if (!subscriptions[eventName]) subscriptions[eventName] = [];
// subscriptions[eventName] = ([] as Function[]).concat(
// subscriptions[eventName],
// fn
// );
// },
// };
// }, []);

// const onScroll = useCallback(
// (event: NativeSyntheticEvent<NativeScrollEvent>) => {
// const cbs = subscriptions['onScroll'];
// if (cbs?.length) {
// cbs.forEach((cb) => cb(event));
// }
// },
// []
// );

return (
<ScrollView
ref={scrollViewRef}
// onScroll={onScroll}
contentContainerStyle={{
backgroundColor: '#fff',
}}
>
<List
data={data}
renderItem={renderItem}
test="3"
id="basic"
keyExtractor={keyExtractor}
containerRef={scrollViewRef}
/>
</ScrollView>
);
return <MasonryList />;
};
8 changes: 6 additions & 2 deletions examples/ReactNativeListPlayground/app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { DarkTheme, DefaultTheme, ThemeProvider } from '@react-navigation/native';
import {
DarkTheme,
DefaultTheme,
ThemeProvider,
} from '@react-navigation/native';
import { useFonts } from 'expo-font';
import { Stack } from 'expo-router';
import * as SplashScreen from 'expo-splash-screen';
Expand All @@ -13,7 +17,7 @@ SplashScreen.preventAutoHideAsync();
export default function RootLayout() {
const colorScheme = useColorScheme();
const [loaded] = useFonts({
SpaceMono: require('../assets/fonts/SpaceMono-Regular.ttf'),
SpaceMono: require('../assets/fonts/SpaceMono-Regular.ttf'), // eslint-disable-line
});

useEffect(() => {
Expand Down
34 changes: 16 additions & 18 deletions examples/ReactNativeListPlayground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,29 @@
},
"dependencies": {
"@expo/vector-icons": "^14.0.2",
"@react-navigation/native": "^6.0.2",
"expo": "~51.0.28",
"expo-constants": "~16.0.2",
"expo-font": "~12.0.9",
"expo-linking": "~6.3.1",
"expo-router": "~3.5.23",
"expo-splash-screen": "~0.27.5",
"expo-status-bar": "~1.12.1",
"expo-system-ui": "~3.0.7",
"expo-web-browser": "~13.0.3",

"react-native-gesture-handler": "~2.16.1",
"react-native-reanimated": "~3.10.1",
"react-native-safe-area-context": "4.10.5",
"react-native-screens": "3.31.1",
"@react-navigation/native": "^7.0.13",
"expo": "^52.0.11",
"expo-constants": "~17.0.3",
"expo-font": "~13.0.1",
"expo-linking": "~7.0.3",
"expo-router": "~4.0.9",
"expo-splash-screen": "~0.29.13",
"expo-status-bar": "~2.0.0",
"expo-system-ui": "~4.0.4",
"expo-web-browser": "~14.0.1",
"react-native-gesture-handler": "~2.20.2",
"react-native-reanimated": "~3.16.1",
"react-native-safe-area-context": "4.12.0",
"react-native-screens": "~4.1.0",
"react-native-web": "~0.19.10"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@types/jest": "^29.5.12",
"@types/react": "~18.2.45",
"@types/react": "~18.3.12",
"@types/react-test-renderer": "^18.0.7",
"jest": "^29.2.1",
"jest-expo": "~51.0.3",
"jest-expo": "~52.0.2",
"react-test-renderer": "18.2.0",
"typescript": "~5.3.3"
},
Expand All @@ -48,6 +47,5 @@
"react-dom": "18.2.0",
"react-native": "0.74.5"
},

"private": true
}
6 changes: 3 additions & 3 deletions examples/ReactNativeListPlayground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"compilerOptions": {
"strict": true,
"paths": {
"@/*": [
"./*"
],
"@infinite-list/react-native": ["../../packages/react-native/src"],
// "@/*": [
// "./*"
// ],
}
},
"include": [
Expand Down
Loading

0 comments on commit ab267a2

Please sign in to comment.