Skip to content

Commit d803bab

Browse files
authored
Inverted list prop (#414)
1 parent 5b5c2ac commit d803bab

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

src/components/MessageList/MessageList.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ export type MessageListProps<
163163
* used in MessageList. Its footer instead of header, since message list is inverted.
164164
*
165165
*/
166+
FooterComponent?: React.ReactElement;
166167
HeaderComponent?: React.ReactElement;
168+
/** Whether or not the FlatList is inverted. Defaults to true */
169+
inverted?: boolean;
167170
/**
168171
* Custom UI component to display a message in MessageList component
169172
* Default component (accepts the same props): [MessageSimple](https://getstream.github.io/stream-chat-react-native/#messagesimple)
@@ -246,7 +249,9 @@ export const MessageList = <
246249
DateSeparator = DefaultDateSeparator,
247250
disableWhileEditing = true,
248251
dismissKeyboardOnMessageTouch = true,
252+
FooterComponent,
249253
HeaderComponent,
254+
inverted = true,
250255
Message: MessageFromProps,
251256
MessageNotification = DefaultMessageNotification,
252257
MessageSystem = DefaultMessageSystem,
@@ -278,6 +283,7 @@ export const MessageList = <
278283
const { t } = useTranslationContext();
279284

280285
const messageList = useMessageList<At, Ch, Co, Ev, Me, Re, Us>({
286+
inverted,
281287
noGroupByUser,
282288
threadList,
283289
});
@@ -417,10 +423,11 @@ export const MessageList = <
417423
data={messageList}
418424
/** Disables the MessageList UI. Which means, message actions, reactions won't work. */
419425
extraData={disabled}
420-
inverted
426+
inverted={inverted}
421427
keyboardShouldPersistTaps='always'
422428
keyExtractor={keyExtractor}
423-
ListFooterComponent={HeaderComponent}
429+
ListFooterComponent={FooterComponent}
430+
ListHeaderComponent={HeaderComponent}
424431
maintainVisibleContentPosition={{
425432
autoscrollToTopThreshold: 10,
426433
minIndexForVisible: 1,

src/components/MessageList/hooks/useMessageList.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type {
2525
} from '../../../types/types';
2626

2727
export type UseMessageListParams = {
28+
inverted?: boolean;
2829
noGroupByUser?: boolean;
2930
threadList?: boolean;
3031
};
@@ -40,7 +41,7 @@ export const useMessageList = <
4041
>(
4142
params: UseMessageListParams,
4243
): InsertDatesResponse<At, Ch, Co, Ev, Me, Re, Us> => {
43-
const { noGroupByUser, threadList } = params;
44+
const { inverted, noGroupByUser, threadList } = params;
4445
const { read } = useChannelContext<At, Ch, Co, Ev, Me, Re, Us>();
4546
const { messages } = useMessagesContext<At, Ch, Co, Ev, Me, Re, Us>();
4647
const { threadMessages } = useThreadContext<At, Ch, Co, Ev, Me, Re, Us>();
@@ -62,17 +63,16 @@ export const useMessageList = <
6263
readList,
6364
);
6465

65-
return messagesWithDates
66-
.map((msg) => ({
67-
...msg,
68-
groupStyles:
69-
!isDateSeparator<At, Ch, Co, Ev, Me, Re, Us>(msg) && msg.id
70-
? messageGroupStyles[msg.id] || []
71-
: [],
72-
readBy:
73-
!isDateSeparator<At, Ch, Co, Ev, Me, Re, Us>(msg) && msg.id
74-
? readData[msg.id] || []
75-
: [],
76-
}))
77-
.reverse();
66+
const messagesWithDatesList = messagesWithDates.map((msg) => ({
67+
...msg,
68+
groupStyles:
69+
!isDateSeparator<At, Ch, Co, Ev, Me, Re, Us>(msg) && msg.id
70+
? messageGroupStyles[msg.id] || []
71+
: [],
72+
readBy:
73+
!isDateSeparator<At, Ch, Co, Ev, Me, Re, Us>(msg) && msg.id
74+
? readData[msg.id] || []
75+
: [],
76+
}));
77+
return inverted ? messagesWithDatesList.reverse() : messagesWithDatesList;
7878
};

src/components/Thread/Thread.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export const Thread = <
168168

169169
const Message = MessageFromProps || MessageFromContext;
170170

171-
const headerComponent = (
171+
const footerComponent = (
172172
<>
173173
<DefaultMessage<At, Ch, Co, Ev, Me, Re, Us>
174174
groupStyles={['single']}
@@ -186,7 +186,7 @@ export const Thread = <
186186
return (
187187
<React.Fragment key={`thread-${thread.id}-${channel?.cid || ''}`}>
188188
<MessageList<At, Ch, Co, Ev, Me, Re, Us>
189-
HeaderComponent={headerComponent}
189+
FooterComponent={footerComponent}
190190
Message={Message}
191191
threadList
192192
{...additionalMessageListProps}

0 commit comments

Comments
 (0)