Skip to content

Commit ea8566b

Browse files
committed
fix: [MessageContainer] scrollToEnd support force
1 parent c4e0058 commit ea8566b

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

demo/src/demo/Chat.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export default () => {
140140
const msgRef = React.useRef(null);
141141

142142
window.appendMsg = appendMsg;
143+
window.msgRef = msgRef;
143144

144145
// 发送回调
145146
function handleSend(type: string, val: string) {
@@ -151,7 +152,9 @@ export default () => {
151152
position: 'right',
152153
});
153154

154-
setTyping(true);
155+
setTimeout(() => {
156+
setTyping(true);
157+
}, 10);
155158

156159
// 模拟回复消息
157160
setTimeout(() => {

src/components/Chat/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export const Chat = React.forwardRef<HTMLDivElement, ChatProps>((props, ref) =>
184184

185185
function handleInputFocus(e: React.FocusEvent<HTMLTextAreaElement>) {
186186
if (messagesRef && messagesRef.current) {
187-
messagesRef.current.scrollToEnd({ animated: false });
187+
messagesRef.current.scrollToEnd({ animated: false, force: true });
188188
}
189189
if (onInputFocus) {
190190
onInputFocus(e);

src/components/MessageContainer/index.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,23 @@ export const MessageContainer = React.forwardRef<MessageContainerHandle, Message
4444

4545
const [showBackBottom, setShowBackBottom] = useState(false);
4646
const [newCount, setNewCount] = useState(0);
47-
const newCountRef = useRef(0);
47+
const showBackBottomtRef = useRef(showBackBottom);
48+
const newCountRef = useRef(newCount);
4849
const messagesRef = useRef<HTMLDivElement>(null);
4950
const scrollerRef = useRef<PullToRefreshHandle>(null);
5051
const bottomRef = useRef<HTMLDivElement>(null);
5152
const lastMessage = messages[messages.length - 1];
5253

5354
const scrollToEnd = useCallback((opts?: ScrollToEndOptions) => {
5455
if (scrollerRef.current) {
55-
scrollerRef.current.scrollToEnd(opts);
56+
if (!showBackBottomtRef.current || (opts && opts.force)) {
57+
scrollerRef.current.scrollToEnd(opts);
58+
}
5659
}
5760
}, []);
5861

5962
const handleBackBottomClick = () => {
60-
scrollToEnd({ animated: false });
63+
scrollToEnd({ animated: false, force: true });
6164
setNewCount(0);
6265
setShowBackBottom(false);
6366

@@ -97,6 +100,10 @@ export const MessageContainer = React.forwardRef<MessageContainerHandle, Message
97100
newCountRef.current = newCount;
98101
}, [newCount]);
99102

103+
useEffect(() => {
104+
showBackBottomtRef.current = showBackBottom;
105+
}, [showBackBottom]);
106+
100107
useEffect(() => {
101108
const scroller = scrollerRef.current;
102109
const wrapper = scroller && scroller.wrapperRef.current;
@@ -106,10 +113,10 @@ export const MessageContainer = React.forwardRef<MessageContainerHandle, Message
106113
}
107114

108115
if (lastMessage.position === 'right') {
109-
scrollToEnd();
116+
scrollToEnd({ force: true });
110117
} else if (isNearBottom(wrapper, 1)) {
111118
const animated = !!wrapper.scrollTop;
112-
scrollToEnd({ animated });
119+
scrollToEnd({ animated, force: true });
113120
} else {
114121
setNewCount((c) => c + 1);
115122
setShowBackBottom(true);

src/components/PullToRefresh/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface PullToRefreshProps {
2626

2727
export interface ScrollToEndOptions {
2828
animated?: boolean;
29+
force?: boolean;
2930
}
3031

3132
interface PTRScrollToOptions extends ScrollToEndOptions {

0 commit comments

Comments
 (0)