Skip to content

Commit

Permalink
feat: added pre-integrated VirtualizedList component
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhom committed May 27, 2021
1 parent bb8e202 commit 2d4d69d
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 4 deletions.
26 changes: 24 additions & 2 deletions example/src/components/contactList/ContactList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BottomSheetFlatList,
BottomSheetScrollView,
BottomSheetSectionList,
BottomSheetVirtualizedList,
BottomSheetView,
} from '@gorhom/bottom-sheet';
import {
Expand All @@ -15,13 +16,15 @@ import {
import ContactItem from '../contactItem';

export interface ContactListProps {
type: 'FlatList' | 'SectionList' | 'ScrollView' | 'View';
type: 'FlatList' | 'SectionList' | 'ScrollView' | 'View' | 'VirtualizedList';
count?: number;
style?: ViewStyle;
onItemPress?: () => void;
}

const keyExtractor = (item: any, index: number) => `${item.name}.${index}`;
const handleGetItem = (data: any[], index: number) => data[index];
const handleGetCount = (data: any[]) => data.length;

const ContactList = ({
type,
Expand All @@ -32,9 +35,10 @@ const ContactList = ({
// hooks
const { bottom: bottomSafeArea } = useSafeAreaInsets();

// variables
//#region variables
const sections = useMemo(() => createContactSectionsMockData(count), [count]);
const data = useMemo(() => createContactListMockData(count), [count]);
//#endregion

// styles
const contentContainerStyle = useMemo(
Expand Down Expand Up @@ -105,6 +109,24 @@ const ContactList = ({
focusHook={useFocusEffect}
/>
);
} else if (type === 'VirtualizedList') {
return (
<BottomSheetVirtualizedList
data={data}
keyExtractor={keyExtractor}
initialNumToRender={5}
getItem={handleGetItem}
getItemCount={handleGetCount}
bounces={true}
windowSize={10}
maxToRenderPerBatch={5}
renderItem={renderFlatListItem}
style={styles.container}
keyboardDismissMode="interactive"
contentContainerStyle={contentContainerStyle}
focusHook={useFocusEffect}
/>
);
} else if (type === 'ScrollView') {
return (
<BottomSheetScrollView
Expand Down
7 changes: 6 additions & 1 deletion example/src/screens/basic/BasicExamples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Button from '../../components/button';

interface ExampleScreenProps {
title: string;
type: 'FlatList' | 'SectionList' | 'ScrollView' | 'View';
type: 'FlatList' | 'SectionList' | 'ScrollView' | 'View' | 'VirtualizedList';
count?: number;
}

Expand Down Expand Up @@ -134,6 +134,11 @@ export const FlatListExampleScreen = createExampleScreen({
type: 'FlatList',
});

export const VirtualizedListExampleScreen = createExampleScreen({
title: 'VirtualizedList Example',
type: 'VirtualizedList',
});

export const ScrollViewExampleScreen = createExampleScreen({
title: 'Title',
type: 'ScrollView',
Expand Down
6 changes: 6 additions & 0 deletions example/src/screens/screens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export const screens = [
getScreen: () =>
require('./basic/BasicExamples').SectionListExampleScreen,
},
{
name: 'VirtualizedList',
slug: 'Basic/VirtualizedListExample',
getScreen: () =>
require('./basic/BasicExamples').VirtualizedListExampleScreen,
},
],
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { memo } from 'react';
import {
VirtualizedList as RNVirtualizedList,
VirtualizedListProps as RNVirtualizedListProps,
} from 'react-native';
import Animated from 'react-native-reanimated';
import { createBottomSheetScrollableComponent } from './createBottomSheetScrollableComponent';
import type {
BottomSheetVirtualizedListMethods,
BottomSheetVirtualizedListProps,
} from './types';

const AnimatedVirtualizedList =
Animated.createAnimatedComponent<RNVirtualizedListProps<any>>(
RNVirtualizedList
);

const BottomSheetVirtualizedListComponent =
createBottomSheetScrollableComponent<
BottomSheetVirtualizedListMethods,
BottomSheetVirtualizedListProps<any>
>(AnimatedVirtualizedList);

const BottomSheetVirtualizedList = memo(BottomSheetVirtualizedListComponent);
BottomSheetVirtualizedList.displayName = 'BottomSheetVirtualizedList';

export default BottomSheetVirtualizedList as <T>(
props: BottomSheetVirtualizedListProps<T>
) => ReturnType<typeof BottomSheetVirtualizedList>;
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function createBottomSheetScrollableComponent<T, P>(
marginBottom: animatedFooterHeight.value,
}));
const containerStyle = useMemo(
() => [style, containerAnimatedStyle],
() => [...('length' in style ? style : [style]), containerAnimatedStyle],
[style, containerAnimatedStyle]
);
//#endregion
Expand Down
2 changes: 2 additions & 0 deletions src/components/bottomSheetScrollable/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export { default as BottomSheetSectionList } from './BottomSheetSectionList';
export { default as BottomSheetFlatList } from './BottomSheetFlatList';
export { default as BottomSheetScrollView } from './BottomSheetScrollView';
export { default as BottomSheetVirtualizedList } from './BottomSheetVirtualizedList';

export type {
BottomSheetFlatListMethods,
BottomSheetScrollViewMethods,
BottomSheetSectionListMethods,
BottomSheetVirtualizedListMethods,
} from './types';
36 changes: 36 additions & 0 deletions src/components/bottomSheetScrollable/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ReactNode, Ref } from 'react';
import type {
VirtualizedListProps,
ScrollViewProps,
FlatListProps,
SectionListProps,
Expand Down Expand Up @@ -210,3 +211,38 @@ export interface BottomSheetSectionListMethods {
getScrollableNode(): NodeHandle | undefined;
}
//#endregion

//#region
export type BottomSheetVirtualizedListProps<T> = Omit<
Animated.AnimateProps<VirtualizedListProps<T>>,
'decelerationRate' | 'onScrollBeginDrag' | 'scrollEventThrottle'
> &
BottomSheetScrollableProps & {
ref?: Ref<BottomSheetVirtualizedListMethods>;
};

export interface BottomSheetVirtualizedListMethods {
scrollToEnd: (params?: { animated?: boolean }) => void;
scrollToIndex: (params: {
animated?: boolean;
index: number;
viewOffset?: number;
viewPosition?: number;
}) => void;
scrollToItem: (params: {
animated?: boolean;
item: ItemT;
viewPosition?: number;
}) => void;

/**
* Scroll to a specific content pixel offset in the list.
* Param `offset` expects the offset to scroll to. In case of horizontal is true, the
* offset is the x-value, in any other case the offset is the y-value.
* Param `animated` (true by default) defines whether the list should do an animation while scrolling.
*/
scrollToOffset: (params: { animated?: boolean; offset: number }) => void;

recordInteraction: () => void;
}
//#endregion
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
BottomSheetScrollView,
BottomSheetSectionList,
BottomSheetFlatList,
BottomSheetVirtualizedList,
} from './components/bottomSheetScrollable';
export { default as BottomSheetDraggableView } from './components/bottomSheetDraggableView';
export { default as BottomSheetView } from './components/view';
Expand All @@ -43,6 +44,7 @@ export type {
BottomSheetFlatListMethods,
BottomSheetScrollViewMethods,
BottomSheetSectionListMethods,
BottomSheetVirtualizedListMethods,
} from './components/bottomSheetScrollable';
//#endregion

Expand Down

0 comments on commit 2d4d69d

Please sign in to comment.