我希望聊天窗口在我发送文本时向下滚动。
实际上发送的消息不会触发滚动,所以它们是隐藏的。
这是一个示例(我正在使用 MS Fluent UI for React 库的聊天组件)
我找到了这篇文章,但我无法整合它(不知道在哪里声明了 endMessage )在我的情况下它的价值是undefined
.
import React from "react";
import { Avatar, Chat, Divider, Input } from "@fluentui/react-northstar";
import { AcceptIcon } from "@fluentui/react-icons-northstar";
const janeAvatar = {
image: "public/images/avatar/small/ade.jpg",
status: {
color: "green",
icon: <AcceptIcon />
}
};
const ChatExample = () => {
const items = [
{
message: (
<Chat.Message
content="Hello"
author="John Doe"
timestamp="Yesterday, 10:15 PM"
mine
/>
),
contentPosition: "end",
attached: "top",
key: "message-id-1"
},
{
message: (
<Chat.Message
content="I'm back!"
author="John Doe"
timestamp="Yesterday, 10:15 PM"
mine
/>
),
contentPosition: "end",
attached: true,
key: "message-id-2"
},
{
message: (
<Chat.Message
content={{
content: (
<div>
What do you think about <a href="#">www.goodFood.com</a>?
</div>
)
}}
author="John Doe"
timestamp="Yesterday, 10:15 PM"
mine
/>
),
contentPosition: "end",
attached: "bottom",
key: "message-id-3"
},
{
gutter: <Avatar {...janeAvatar} />,
message: (
<Chat.Message
content="Hi"
author="Jane Doe"
timestamp="Yesterday, 10:15 PM"
/>
),
attached: "top",
key: "message-id-4"
},
{
gutter: <Avatar {...janeAvatar} />,
message: (
<Chat.Message
content="Looks good!"
author="Jane Doe"
timestamp="Yesterday, 10:15 PM"
/>
),
attached: true,
key: "message-id-5"
},
{
gutter: <Avatar {...janeAvatar} />,
message: (
<Chat.Message
content={
<div>
I also like <a href="#">www.goodFood2.com</a>.
</div>
}
author="Jane Doe"
timestamp="Yesterday, 10:15 PM"
/>
),
attached: "bottom",
key: "message-id-6"
},
{
message: (
<Chat.Message
content="Would you like to grab lunch there?"
author="John Doe"
timestamp="Yesterday, 10:16 PM"
mine
/>
),
contentPosition: "end",
key: "message-id-7"
},
{
gutter: <Avatar {...janeAvatar} />,
message: (
<Chat.Message
content="Sure! Let's try it."
author="Jane Doe"
timestamp="Yesterday, 10:15 PM"
/>
),
key: "message-id-8"
},
{
children: <Divider content="Today" color="brand" important />,
key: "message-id-9"
},
{
message: (
<Chat.Message
content="Ok, let's go."
author="John Doe"
timestamp="Today, 11:15 PM"
mine
/>
),
contentPosition: "end",
key: "message-id-10"
}
];
const [inputValue, setInputValue] = React.useState("");
const [itemsChat, setItemsChat] = React.useState(items);
const chatStyle = {
height: 500,
overflow: "scroll"
};
const handleKeyDown = event => {
if (event.key == "Enter") {
console.log(event);
let elm = {
message: (
<Chat.Message
content={inputValue}
author="John Doe"
timestamp="Yesterday, 10:15 PM"
mine
/>
),
contentPosition: "end",
attached: "top",
key: "message-id-1"
};
setItemsChat(itemsChat => [...itemsChat, elm]);
setInputValue("");
}
};
return (
<div>
<Chat style={chatStyle} items={itemsChat} />
<Input
fluid
placeholder="Inverted color input..."
value={inputValue}
onKeyDown={event => handleKeyDown(event)}
onChange={event => setInputValue(event.target.value)}
/>
</div>
);
};
export default ChatExample;