Skip to content

Commit 850a35b

Browse files
committed
fix: make sure leading and trailing views respect message alignment
1 parent aefe5bf commit 850a35b

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

package/src/components/Message/MessageItemView/MessageContent.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const useReplyStyles = () => {
5757

5858
export type MessageContentPropsWithContext = Pick<
5959
MessageContextValue,
60+
| 'alignment'
6061
| 'goToMessage'
6162
| 'groupStyles'
6263
| 'isMyMessage'
@@ -121,6 +122,7 @@ export type MessageContentPropsWithContext = Pick<
121122
const MessageContentWithContext = (props: MessageContentPropsWithContext) => {
122123
const {
123124
additionalPressableProps,
125+
alignment,
124126
Attachment,
125127
backgroundColor,
126128
enableMessageGroupingByUser,
@@ -371,7 +373,13 @@ const MessageContentWithContext = (props: MessageContentPropsWithContext) => {
371373
>
372374
{MessageContentTopView ? <MessageContentTopView /> : null}
373375
{hasContentSideViews ? (
374-
<View style={styles.contentRow}>
376+
<View
377+
style={[
378+
styles.contentRow,
379+
alignment === 'right' ? styles.rightAlignContentRow : undefined,
380+
]}
381+
testID='message-content-row'
382+
>
375383
{MessageContentLeadingView ? <MessageContentLeadingView /> : null}
376384
<View style={styles.contentBody}>{contentBody}</View>
377385
{MessageContentTrailingView ? <MessageContentTrailingView /> : null}
@@ -391,6 +399,7 @@ const areEqual = (
391399
nextProps: MessageContentPropsWithContext,
392400
) => {
393401
const {
402+
alignment: prevAlignment,
394403
backgroundColor: prevBackgroundColor,
395404
preventPress: prevPreventPress,
396405
goToMessage: prevGoToMessage,
@@ -403,6 +412,7 @@ const areEqual = (
403412
t: prevT,
404413
} = prevProps;
405414
const {
415+
alignment: nextAlignment,
406416
backgroundColor: nextBackgroundColor,
407417
preventPress: nextPreventPress,
408418
goToMessage: nextGoToMessage,
@@ -418,6 +428,10 @@ const areEqual = (
418428
return false;
419429
}
420430

431+
if (prevAlignment !== nextAlignment) {
432+
return false;
433+
}
434+
421435
if (prevPreventPress !== nextPreventPress) {
422436
return false;
423437
}
@@ -547,6 +561,7 @@ export type MessageContentProps = Partial<
547561
*/
548562
export const MessageContent = (props: MessageContentProps) => {
549563
const {
564+
alignment,
550565
goToMessage,
551566
groupStyles,
552567
isMessageAIGenerated,
@@ -617,6 +632,7 @@ export const MessageContent = (props: MessageContentProps) => {
617632
<MemoizedMessageContent
618633
{...{
619634
additionalPressableProps,
635+
alignment,
620636
Attachment,
621637
enableMessageGroupingByUser,
622638
FileAttachmentGroup,
@@ -668,6 +684,9 @@ const styles = StyleSheet.create({
668684
contentRow: {
669685
flexDirection: 'row',
670686
},
687+
rightAlignContentRow: {
688+
flexDirection: 'row-reverse',
689+
},
671690
leftAlignContent: {
672691
justifyContent: 'flex-start',
673692
},

package/src/components/Message/MessageItemView/__tests__/MessageContent.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,56 @@ describe('MessageContent', () => {
199199
});
200200
});
201201

202+
it('orders leading and trailing content views based on alignment', async () => {
203+
const otherUser = generateUser({ id: 'other-user' });
204+
const leftAlignedMessage = generateMessage({ user: otherUser });
205+
const rightAlignedMessage = generateMessage({ user });
206+
207+
const { rerender } = render(
208+
<ChannelsStateProvider>
209+
<Chat client={chatClient}>
210+
<Channel
211+
channel={channel}
212+
MessageContentLeadingView={() => <View testID='message-content-leading-view' />}
213+
MessageContentTrailingView={() => <View testID='message-content-trailing-view' />}
214+
>
215+
<Message groupStyles={['bottom']} message={leftAlignedMessage} />
216+
</Channel>
217+
</Chat>
218+
</ChannelsStateProvider>,
219+
);
220+
221+
await waitFor(() => {
222+
expect(screen.getByTestId('message-content-leading-view')).toBeTruthy();
223+
expect(screen.getByTestId('message-content-trailing-view')).toBeTruthy();
224+
});
225+
226+
let contentRowStyle = StyleSheet.flatten(screen.getByTestId('message-content-row').props.style);
227+
expect(contentRowStyle?.flexDirection).toBe('row');
228+
229+
rerender(
230+
<ChannelsStateProvider>
231+
<Chat client={chatClient}>
232+
<Channel
233+
channel={channel}
234+
MessageContentLeadingView={() => <View testID='message-content-leading-view' />}
235+
MessageContentTrailingView={() => <View testID='message-content-trailing-view' />}
236+
>
237+
<Message groupStyles={['bottom']} message={rightAlignedMessage} />
238+
</Channel>
239+
</Chat>
240+
</ChannelsStateProvider>,
241+
);
242+
243+
await waitFor(() => {
244+
expect(screen.getByTestId('message-content-leading-view')).toBeTruthy();
245+
expect(screen.getByTestId('message-content-trailing-view')).toBeTruthy();
246+
});
247+
248+
contentRowStyle = StyleSheet.flatten(screen.getByTestId('message-content-row').props.style);
249+
expect(contentRowStyle?.flexDirection).toBe('row-reverse');
250+
});
251+
202252
it('renders a time component when MessageFooter does not exist', async () => {
203253
const user = generateUser();
204254
const message = generateMessage({ user });

0 commit comments

Comments
 (0)