Skip to content

Commit

Permalink
fix: [MessageContainer] scrollToEnd support force
Browse files Browse the repository at this point in the history
  • Loading branch information
akai committed Jun 29, 2022
1 parent c4e0058 commit ea8566b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
5 changes: 4 additions & 1 deletion demo/src/demo/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export default () => {
const msgRef = React.useRef(null);

window.appendMsg = appendMsg;
window.msgRef = msgRef;

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

setTyping(true);
setTimeout(() => {
setTyping(true);
}, 10);

// 模拟回复消息
setTimeout(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export const Chat = React.forwardRef<HTMLDivElement, ChatProps>((props, ref) =>

function handleInputFocus(e: React.FocusEvent<HTMLTextAreaElement>) {
if (messagesRef && messagesRef.current) {
messagesRef.current.scrollToEnd({ animated: false });
messagesRef.current.scrollToEnd({ animated: false, force: true });
}
if (onInputFocus) {
onInputFocus(e);
Expand Down
17 changes: 12 additions & 5 deletions src/components/MessageContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,23 @@ export const MessageContainer = React.forwardRef<MessageContainerHandle, Message

const [showBackBottom, setShowBackBottom] = useState(false);
const [newCount, setNewCount] = useState(0);
const newCountRef = useRef(0);
const showBackBottomtRef = useRef(showBackBottom);
const newCountRef = useRef(newCount);
const messagesRef = useRef<HTMLDivElement>(null);
const scrollerRef = useRef<PullToRefreshHandle>(null);
const bottomRef = useRef<HTMLDivElement>(null);
const lastMessage = messages[messages.length - 1];

const scrollToEnd = useCallback((opts?: ScrollToEndOptions) => {
if (scrollerRef.current) {
scrollerRef.current.scrollToEnd(opts);
if (!showBackBottomtRef.current || (opts && opts.force)) {
scrollerRef.current.scrollToEnd(opts);
}
}
}, []);

const handleBackBottomClick = () => {
scrollToEnd({ animated: false });
scrollToEnd({ animated: false, force: true });
setNewCount(0);
setShowBackBottom(false);

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

useEffect(() => {
showBackBottomtRef.current = showBackBottom;
}, [showBackBottom]);

useEffect(() => {
const scroller = scrollerRef.current;
const wrapper = scroller && scroller.wrapperRef.current;
Expand All @@ -106,10 +113,10 @@ export const MessageContainer = React.forwardRef<MessageContainerHandle, Message
}

if (lastMessage.position === 'right') {
scrollToEnd();
scrollToEnd({ force: true });
} else if (isNearBottom(wrapper, 1)) {
const animated = !!wrapper.scrollTop;
scrollToEnd({ animated });
scrollToEnd({ animated, force: true });
} else {
setNewCount((c) => c + 1);
setShowBackBottom(true);
Expand Down
1 change: 1 addition & 0 deletions src/components/PullToRefresh/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface PullToRefreshProps {

export interface ScrollToEndOptions {
animated?: boolean;
force?: boolean;
}

interface PTRScrollToOptions extends ScrollToEndOptions {
Expand Down

0 comments on commit ea8566b

Please sign in to comment.