Skip to content

Commit 0c7a216

Browse files
Moved inline footer and header component to separate default InlineLoadingIndicator component
1 parent c44384a commit 0c7a216

File tree

2 files changed

+58
-34
lines changed

2 files changed

+58
-34
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react';
2+
import { ActivityIndicator, StyleSheet, View } from 'react-native';
3+
import { useTheme } from '../../contexts/themeContext/ThemeContext';
4+
5+
const styles = StyleSheet.create({
6+
activityIndicatorContainer: {
7+
padding: 10,
8+
width: '100%',
9+
},
10+
});
11+
12+
export type InlineLoadingIndicatorProps = {
13+
loadingMore: boolean;
14+
};
15+
16+
export const InlineLoadingIndicator: React.FC<InlineLoadingIndicatorProps> = ({
17+
loadingMore,
18+
}) => {
19+
const { theme } = useTheme();
20+
21+
const {
22+
colors: { accent_blue },
23+
} = theme;
24+
25+
if (loadingMore) {
26+
return (
27+
<View style={styles.activityIndicatorContainer}>
28+
<ActivityIndicator color={accent_blue} size='small' />
29+
</View>
30+
);
31+
}
32+
33+
return null;
34+
};

src/components/MessageList/MessageList.tsx

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { useEffect, useMemo, useRef, useState } from 'react';
22
import {
3-
ActivityIndicator,
43
FlatListProps,
54
FlatList as FlatListType,
65
Platform,
@@ -64,12 +63,12 @@ import type {
6463
DefaultUserType,
6564
UnknownType,
6665
} from '../../types/types';
66+
import {
67+
InlineLoadingIndicator,
68+
InlineLoadingIndicatorProps,
69+
} from './InlineLoadingIndicator';
6770

6871
const styles = StyleSheet.create({
69-
activityIndicatorContainer: {
70-
padding: 10,
71-
width: '100%',
72-
},
7372
container: {
7473
alignItems: 'center',
7574
flex: 1,
@@ -180,19 +179,23 @@ type MessageListPropsWithContext<
180179
FlatListProps<MessageType<At, Ch, Co, Ev, Me, Re, Us>>
181180
>;
182181
/**
183-
* UI component for footer of message list. By default message list doesn't have any footer.
182+
* UI component for footer of message list. By default message list will use `InlineLoadingIndicator`
183+
* as FooterComponent. You have access to prop `loadingMore` on this component,
184+
* if you want to implement your own inline loading indicator.
185+
*
184186
* This is a [ListHeaderComponent](https://facebook.github.io/react-native/docs/flatlist#listheadercomponent) of FlatList
185187
* used in MessageList. Should be used for header by default if inverted is true or defaulted
186-
*
187188
*/
188-
FooterComponent?: React.ReactElement;
189+
FooterComponent?: React.ComponentType<InlineLoadingIndicatorProps>;
189190
/**
190-
* UI component for header of message list. By default message list doesn't have any header.
191+
* UI component for header of message list. By default message list will use `InlineLoadingIndicator`
192+
* as HeaderComponent. You have access to prop `loadingMore` on this component,
193+
* if you want to implement your own inline loading indicator.
194+
*
191195
* This is a [ListFooterComponent](https://facebook.github.io/react-native/docs/flatlist#listheadercomponent) of FlatList
192196
* used in MessageList. Should be used for header if inverted is false
193-
*
194197
*/
195-
HeaderComponent?: React.ReactElement;
198+
HeaderComponent?: React.ComponentType<InlineLoadingIndicatorProps>;
196199
/** Whether or not the FlatList is inverted. Defaults to true */
197200
inverted?: boolean;
198201
/** Turn off grouping of messages by user */
@@ -256,8 +259,8 @@ const MessageListWithContext = <
256259
disableTypingIndicator,
257260
EmptyStateIndicator,
258261
FlatList,
259-
FooterComponent,
260-
HeaderComponent,
262+
FooterComponent = InlineLoadingIndicator,
263+
HeaderComponent = InlineLoadingIndicator,
261264
initialScrollToFirstUnreadMessage,
262265
InlineUnreadIndicator,
263266
inverted = true,
@@ -297,7 +300,7 @@ const MessageListWithContext = <
297300
const { theme } = useTheme();
298301

299302
const {
300-
colors: { accent_blue, white_snow },
303+
colors: { white_snow },
301304
messageList: { container, listContainer },
302305
} = theme;
303306

@@ -846,28 +849,15 @@ const MessageListWithContext = <
846849
)}
847850
</View>
848851
}
849-
ListFooterComponent={() =>
850-
FooterComponent ? (
851-
FooterComponent
852-
) : onEndReachedInProgress ? (
853-
<View style={[styles.activityIndicatorContainer]}>
854-
<ActivityIndicator color={accent_blue} size='small' />
855-
</View>
856-
) : null
857-
}
858-
// TODO: Scrolling doesn't work perfectly with this loading indicator. Investigate and fix.
859-
ListHeaderComponent={() =>
860-
HeaderComponent ? (
861-
HeaderComponent
862-
) : onStartReachedInProgress ? (
863-
<View style={[styles.activityIndicatorContainer]}>
864-
<ActivityIndicator color={accent_blue} size='small' />
865-
</View>
866-
) : null
867-
}
852+
ListFooterComponent={() => (
853+
<FooterComponent loadingMore={onEndReachedInProgress} />
854+
)}
855+
ListHeaderComponent={() => (
856+
<HeaderComponent loadingMore={onStartReachedInProgress} />
857+
)}
868858
maintainVisibleContentPosition={{
869859
autoscrollToTopThreshold: autoscrollToTop ? 10 : undefined,
870-
minIndexForVisible: 0,
860+
minIndexForVisible: 1,
871861
}}
872862
onScroll={handleScroll}
873863
onScrollBeginDrag={onScrollBeginDrag}

0 commit comments

Comments
 (0)