Skip to content

Commit 0c4b56d

Browse files
authored
fix(chatbot): fix item render content key (#259)
1 parent b06f188 commit 0c4b56d

5 files changed

Lines changed: 26 additions & 9 deletions

File tree

src/chat-message/chat-item.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,23 @@ export default class ChatItem extends Component<ChatMessageProps> {
181181
const elementKey = `${id}-${index}`;
182182
// 系统消息渲染
183183
if (role === 'system') {
184-
return <div className={`${getClassPrefix()}-chat__text--${role}`}>{content.data}</div>;
184+
return (
185+
<div key={elementKey} className={`${getClassPrefix()}-chat__text--${role}`}>
186+
{content.data}
187+
</div>
188+
);
185189
}
186190
// 用户消息渲染
187191
if (role === 'user') {
188192
if (isAttachmentContent(content)) {
189-
return renderAttachments({ content: content.data }, this);
193+
return renderAttachments({ key: elementKey, content: content.data }, this);
190194
}
191195
if (isTextContent(content) || isMarkdownContent(content)) {
192-
return <div className={`${getClassPrefix()}-chat__text--${role}`}>{content.data}</div>;
196+
return (
197+
<div key={elementKey} className={`${getClassPrefix()}-chat__text--${role}`}>
198+
{content.data}
199+
</div>
200+
);
193201
}
194202
return <slot key={elementKey} name={`${content?.slotName || `${content.type}-${index}`}`}></slot>;
195203
}
@@ -198,6 +206,7 @@ export default class ChatItem extends Component<ChatMessageProps> {
198206
if (role === 'assistant') {
199207
if (isSearchContent(content)) {
200208
return renderSearch({
209+
key: elementKey,
201210
content: content.data,
202211
status: content.status,
203212
useCollapse: chatContentProps?.search?.useCollapse,
@@ -208,13 +217,15 @@ export default class ChatItem extends Component<ChatMessageProps> {
208217

209218
if (isSuggestionContent(content)) {
210219
return renderSuggestion({
220+
key: elementKey,
211221
content: content.data,
212222
handlePromptClick: (data) => this.handleClickAction('suggestion', data),
213223
});
214224
}
215225
if (isThinkingContent(content)) {
216226
// 思考
217227
return renderThinking({
228+
key: elementKey,
218229
content: content.data,
219230
status: content.status,
220231
animation,

src/chat-message/content/attachment-content.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import styles from '../style/chat-item.less';
1111
const className = `${getClassPrefix()}-chat__item`;
1212

1313
export type TdChatAttachmentContentProps = {
14+
key?: string;
1415
content?: AttachmentItem[];
1516
onFileClick?: (event: CustomEvent<TdAttachmentItem>) => void;
1617
};
1718

1819
// 纯函数渲染器
19-
export const renderAttachments = ({ content }: TdChatAttachmentContentProps, self: any) => (
20-
<div className={`${className}__attachments`}>
20+
export const renderAttachments = ({ key, content }: TdChatAttachmentContentProps, self: any) => (
21+
<div key={key} className={`${className}__attachments`}>
2122
<t-attachments
2223
items={content}
2324
removable={false}

src/chat-message/content/search-content.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type SearchContent = {
1717
references?: ReferenceItem[];
1818
};
1919
export type TdChatSearchContentProps = {
20+
key?: string;
2021
content?: SearchContent;
2122
status?: ChatMessageStatus | ((currentStatus: ChatMessageStatus | undefined) => ChatMessageStatus);
2223
onChange?: (value: CollapseValue) => void;
@@ -26,6 +27,7 @@ export type TdChatSearchContentProps = {
2627

2728
// 纯函数渲染器
2829
export const renderSearch = ({
30+
key,
2931
content,
3032
status,
3133
useCollapse = true,
@@ -51,7 +53,7 @@ export const renderSearch = ({
5153
);
5254

5355
return (
54-
<div className={`${className}__search__wrapper`}>
56+
<div key={key} className={`${className}__search__wrapper`}>
5557
{useCollapse ? (
5658
<t-collapse
5759
className={`${className}__search`}

src/chat-message/content/suggestion-content.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import styles from '../style/chat-item.less';
1111
const className = `${getClassPrefix()}-chat__item`;
1212

1313
export type TdChatSuggestionContentProps = {
14+
key?: string;
1415
content?: SuggestionItem[];
1516
handlePromptClick?: ({ event, content }: { event: MouseEvent; content: SuggestionItem }) => void;
1617
};
1718

1819
// 纯函数渲染器
19-
export const renderSuggestion = ({ content, handlePromptClick }: TdChatSuggestionContentProps) => (
20-
<div className={`${className}__suggestion`}>
20+
export const renderSuggestion = ({ key, content, handlePromptClick }: TdChatSuggestionContentProps) => (
21+
<div key={key} className={`${className}__suggestion`}>
2122
{content.map(
2223
(s, i) =>
2324
s?.title && (

src/chat-message/content/thinking-content.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import styles from '../style/chat-item.less';
1717
const className = `${getClassPrefix()}-chat__item`;
1818

1919
type TdChatThinkBaseProps = {
20+
key?: string;
2021
content?: {
2122
text?: string;
2223
title?: string;
@@ -35,6 +36,7 @@ export interface IRenderThinking extends TdChatThinkBaseProps {
3536

3637
// 纯函数渲染器(无shadow root)
3738
export const renderThinking = ({
39+
key,
3840
content,
3941
status,
4042
maxHeight,
@@ -58,7 +60,7 @@ export const renderThinking = ({
5860
return (
5961
<t-collapse
6062
// collapsed做key可在非受控更新时触发展开收起
61-
key={onChange ? undefined : collapsed}
63+
key={onChange ? key : `${collapsed}-${key}`}
6264
className={`${className}__think ${layoutClass}`}
6365
expandIconPlacement={'right'}
6466
value={onChange ? defaultCollapsed : undefined}

0 commit comments

Comments
 (0)