Skip to content

Commit 320b2ec

Browse files
committed
fix: [MessageContainer] scrollToEnd when focus
1 parent 8f2bbb4 commit 320b2ec

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/components/Composer/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { SendButton } from './SendButton';
1212
import { Action } from './Action';
1313
import toggleClass from '../../utils/toggleClass';
1414

15-
const FOCUSING_CLASS = 'S--focusing';
15+
export const CLASS_NAME_FOCUSING = 'S--focusing';
1616

1717
export type InputType = 'voice' | 'text';
1818

@@ -132,7 +132,7 @@ export const Composer = React.forwardRef<ComposerHandle, ComposerProps>((props,
132132
const handleInputFocus = useCallback(
133133
(e: React.FocusEvent<HTMLTextAreaElement>) => {
134134
clearTimeout(blurTimer.current);
135-
toggleClass(FOCUSING_CLASS, true);
135+
toggleClass(CLASS_NAME_FOCUSING, true);
136136
focused.current = true;
137137

138138
if (onFocus) {
@@ -145,7 +145,7 @@ export const Composer = React.forwardRef<ComposerHandle, ComposerProps>((props,
145145
const handleInputBlur = useCallback(
146146
(e: React.FocusEvent<HTMLTextAreaElement>) => {
147147
blurTimer.current = setTimeout(() => {
148-
toggleClass(FOCUSING_CLASS, false);
148+
toggleClass(CLASS_NAME_FOCUSING, false);
149149
focused.current = false;
150150
}, 0);
151151

src/components/MessageContainer/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, { useState, useEffect, useRef, useCallback, useImperativeHandle }
33
import { PullToRefresh, PullToRefreshHandle, ScrollToEndOptions } from '../PullToRefresh';
44
import { Message, MessageProps } from '../Message';
55
import { BackBottom } from '../BackBottom';
6+
import { CLASS_NAME_FOCUSING } from '../Composer';
67
import canUse from '../../utils/canUse';
78
import throttle from '../../utils/throttle';
89
import getToBottom from '../../utils/getToBottom';
@@ -48,7 +49,6 @@ export const MessageContainer = React.forwardRef<MessageContainerHandle, Message
4849
const newCountRef = useRef(newCount);
4950
const messagesRef = useRef<HTMLDivElement>(null);
5051
const scrollerRef = useRef<PullToRefreshHandle>(null);
51-
const bottomRef = useRef<HTMLDivElement>(null);
5252
const lastMessage = messages[messages.length - 1];
5353

5454
const scrollToEnd = useCallback((opts?: ScrollToEndOptions) => {
@@ -112,7 +112,11 @@ export const MessageContainer = React.forwardRef<MessageContainerHandle, Message
112112
return;
113113
}
114114

115-
if (lastMessage.position === 'right') {
115+
if (
116+
lastMessage.position === 'right' ||
117+
document.body.classList.contains(CLASS_NAME_FOCUSING)
118+
) {
119+
// 自己发的消息 或 输入框聚焦,则滚动到底部
116120
scrollToEnd({ force: true });
117121
} else if (isNearBottom(wrapper, 1)) {
118122
const animated = !!wrapper.scrollTop;
@@ -177,7 +181,6 @@ export const MessageContainer = React.forwardRef<MessageContainerHandle, Message
177181
{messages.map((msg) => (
178182
<Message {...msg} renderMessageContent={renderMessageContent} key={msg._id} />
179183
))}
180-
<div ref={bottomRef} />
181184
</div>
182185
</PullToRefresh>
183186
{showBackBottom && (

0 commit comments

Comments
 (0)