Plus · Chat
Chat
Used to quickly build conversation content
When to use
The
Chat
component can be used in scenarios such as regular conversations or AI conversations.The rendering of the conversation content is based on the MarkdownRender component, which supports Markdown and MDX. It allows for common rich text features such as images, tables, links, bold text, code blocks, and more. More complex and customized document writing and display requirements can be achieved using JSX.
Demos
How to import
Chat is supported starting from version v2.63.0.
Basic usage
By setting
chats
, onChatsChange
, and onMessageSend
, you can achieve basic conversation display and interaction.Conversations involve multiple participants and multiple rounds of interaction. Role information, including names and avatars, can be passed through the
roleConfig
parameter. For detailed parameter information, refer to RoleConfig.The prompt text of the upload button can be set through
uploadTipProps
. For details, please refer to Tooltip.Dialogue is a scene involving multiple parties and multiple rounds of interaction. Role information (including name, avatar, etc.) can be passed in through
roleConfig
, and the specific parameter details are RoleConfig.Use the
align
attribute to set the layout of the dialog, supporting left and right layout (leftRight
, default) and left alignment (leftAlign
).Chat status
The chats type is
Message[]
, where each Message
contains various information about the conversation, such as role, content, attachment, status, unique identifier (id), creation time (createAt), and more. For detailed information, please refer to Message. The conversation style may vary depending on the different status values.Dynamic update chats
For the case of receiving Server-Sent Event data from the backend, the obtained data can be used to update the
chats
, and the conversation content will be updated in real-time.The
showStopGenerate
parameter can be used to determine whether to display the stop generation button, with a default value of false
. The logic for stopping the generation can be handled in the onStopGenerator
function.Clear context
Displaying the clear context button in the input box can be enabled through
showClearContext
, which defaults to false
.
The context can also be cleared by calling the clearContext
method through ref.Custom rendering dialog box
Pass in custom rendering configuration through
chatBoxRenderConfig
, the chatBoxRenderConfig type is as followsCustom render avatar and Title through
renderChatBoxAvatar
and renderChatBoxTitle
。When hovering over a conversation, the conversation action area will be displayed. You can customize the rendering of the action area using
renderChatBoxAction
.You can customize the content of the action area using
renderChatBoxContent
.Use
renderFullChatBox
to custom render the entire chat boxCustom render InputArea
The rendering input box can be customized through
renderInputArea
, the parameters are as followsExample of
detailProps
Other Example:
Hint
The prompt area content can be set through
hints
. After clicking the prompt content, the prompt content will become the new user input content and trigger the onHintClick
callback.Custom render Hint
Customize the content of the prompt area through
renderHintBox
, the parameters are as followsExample:
API
PROPERTIES | INSTRUCTIONS | TYPE | DEFAULT |
---|---|---|---|
align | Dialog layout, supports leftRight ,leftAlign | string | leftRight |
bottomSlot | bottom slot for chat | React.ReactNode | - |
chatBoxRenderConfig | chatBox rendering configuration | ChatBoxRenderConfig | - |
chats | Controlled conversation list | Message | - |
className | Custom class name | string | - |
customMarkDownComponents | custom markdown render, transparently passed to MarkdownRender for conversation content rendering | MDXProps['components'] | - |
enableUpload | Whether to enable uploading, supported since v2.76.0, supports boolean type and object type. When the boolean type is passed in, the upload behavior of dragging, clicking the upload button, and pasting in the input box will be controlled at the same time. When the object is passed in, it can be set separately. When the object type is passed in, the unset items default to true. | boolean | { pasteUpload: boolean, clickUpload: boolean, dragUpload: boolean } | true |
hints | prompt information | string | - |
hintCls | hint style | string | - |
hintStyle | hint style | CSSProperties | - |
inputBoxStyle | Input box style | CSSProperties | - |
inputBoxCls | Input box className | string | - |
sendHotKey | Keyboard shortcut for sending content, supports enter | shift+enter . The former will send the message in the input box when you press enter alone. When the shift and enter keys are pressed at the same time, it will only wrap the line and not send it. The latter is the opposite | string | enter |
markdownRenderProps | This parameter will be passed to the MarkdownRender component used for dialog rendering. For details, see MarkdownRenderProps | MarkdownRenderProps | - |
mode | Conversation mode, support bubble | noBubble | userBubble | string | bubble |
roleConfig | Role information configuration, see RoleConfig | RoleConfig | - |
renderDivider | Custom render divider, supported since v2.67.0 | (message?: Message) => ReactNode | - |
renderHintBox | Custom rendering prompt information | (props: {content: string; index: number,onHintClick: () => void}) => React.ReactNode | - |
onChatsChange | Triggered when the conversation list changes | (chats: Message[]) => void | - |
onClear | Triggered when context message is cleared | () => void | - |
onHintClick | Triggered when the prompt message is clicked | (hint: string) => void | - |
onInputChange | Triggered when input area information changes | (props: { value?: string, attachment?: FileItem[] }) => void; | - |
onMessageBadFeedback | Triggered when the message is negatively fed back | (message: Message) => void | - |
onMessageCopy | Triggered when copying a message | (message: Message) => void | - |
onMessageDelete | Triggered when a message is deleted | (message: Message) => void | - |
onMessageGoodFeedback | Triggered when the message is fed back positively | (message: Message) => void | - |
onMessageReset | Triggered when message is reset | (message: Message) => void | - |
onMessageSend | Triggered when sending a message | (content: string, attachment?: FileItem[]) => void | - |
onStopGenerator | Fires when the stop generation button is clicked | (message: Message) => void | - |
placeholder | Input box placeholder | string | - |
renderInputArea | Custom rendering input box | (props: RenderInputAreaProps) => React.ReactNode | - |
showClearContext | Whether to display the clear context button | boolean | false |
showStopGenerate | Whether to display the stop generation button | boolean | false |
topSlot | top slot for chat | React.ReactNode | - |
uploadProps | Upload component properties, refer to details Upload | UploadProps | - |
uploadTipProps | Upload component prompt attribute, refer to details Tooltip | TooltipProps | - |
RoleConfig
PROPERTIES | INSTRUCTIONS | TYPE | DEFAULT |
---|---|---|---|
user | User information | Metadata | - |
assistant | Assistant information | Metadata | - |
system | System information | Metadata | - |
Metadata
PROPERTIES | INSTRUCTIONS | TYPE | DEFAULT |
---|---|---|---|
name | name | string | - |
avatar | avatar | string | - |
color | Avatar background color, same as the color parameter of Avatar component, support amber 、 blue 、 cyan 、 green 、 grey 、 indigo 、 light-blue 、 light-green 、 lime 、 orange 、 pink 、 purple 、 red 、 teal 、 violet 、 yellow | string | grey |
Message
PROPERTIES | INSTRUCTIONS | TYPE | DEFAULT |
---|---|---|---|
role | role | string | - |
name | name | string | - |
id | Uniquely identifies | string| number | - |
content | all content | string | Content[] |
parentId | parent Uniquely identifies | string | - |
createAt | creation time | number | - |
status | Information status, loading | incomplete | complete | error | string | complete |
Content
PROPERTIES | INSTRUCTIONS | TYPE | DEFAULT |
---|---|---|---|
type | type, suport text | image_url | file_url | string | - |
text | Content data when type is text | string | - |
image_url | Content data when type is image_url | { url: string } | - |
file_url | Content data when type is file_url | { url: string; name: string; size: string; type: string } | - |
Methods
METHOD | INSTRUCTIONS |
---|---|
resetMessage | Reset message |
scrollToBottom(animation: boolean) | Scroll to the bottom, if animation is true, there will be animation, otherwise there will be no animation. |
clearContext | clear context |
sendMessage(content: string, attachment: FileItem[]) | send message with content and attachment |