Skip to content

Commit 924ab85

Browse files
zydemailmimaoxiaoanlyyao
authored
为聊天消息添加用户头像与昵称自定义插槽功能 (#4093)
* feat: 为聊天消息添加用户头像与昵称自定义插槽功能 * docs(sse): 更新SSE流式接口为微信小程序wx.request示例 * feat: 名称slot示例 * feat: 插槽位置调整 * docs: 更新 chat-message 组件及插槽配置说明 * chore: 更新 common 子模块提交版本 * docs: regenerate api docs --------- Co-authored-by: nekowu <[email protected]> Co-authored-by: anlyyao <[email protected]>
1 parent da39bd1 commit 924ab85

File tree

12 files changed

+68
-87
lines changed

12 files changed

+68
-87
lines changed

packages/pro-components/chat/chat-message/README.en-US.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ longpress | `(detail: { id: string })` | \-
2929

3030
name | \-
3131
avatar | \-
32-
datetime | \-
32+
content | \-

packages/pro-components/chat/chat-message/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ isComponent: true
3030
{{ base }}
3131

3232
#### 可配置昵称、头像、对齐方式
33-
33+
支持`avatar``name`插槽自定义
3434
{{ configure }}
3535

3636
#### 配置消息属性
3737

38-
支持`avatar``name``datetime``content`插槽自定义, `content`插槽使用建议:渲染聊天消息统一用 `t-chat-content`;仅在需要“单独使用 Markdown 组件”时使用 `t-chat-markdown`。也支持别的 markdown 渲染组件,选择其他 markdown 渲染库由用户自行安装。
38+
支持`content`插槽自定义, `content`插槽使用建议:渲染聊天消息统一用 `t-chat-content`;仅在需要“单独使用 Markdown 组件”时使用 `t-chat-markdown`。也支持别的 markdown 渲染组件,选择其他 markdown 渲染库由用户自行安装。
3939

4040
{{ content }}
4141

@@ -86,5 +86,5 @@ longpress | `(detail: { id: string })` | \-
8686
名称 | 描述
8787
-- | --
8888
avatar | 自定义 `avatar` 显示内容
89-
datetime | 自定义 `datetime` 显示内容
89+
content | 自定义消息内容
9090
name | 自定义 `name` 显示内容

packages/pro-components/chat/chat-message/_example/configure/index.wxml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@
2121
placement="right"
2222
></t-chat-message>
2323
</view>
24+
<view class="chat-example-block">
25+
<t-chat-message content="{{message.content}}" role="{{message.role}}" datetime="16:38">
26+
<view slot="name" class="name-block">
27+
<image src="https://tdesign.gtimg.com/site/chat-avatar.png" />
28+
<view>Canxuan</view>
29+
</view>
30+
</t-chat-message>
31+
</view>
2432
</view>

packages/pro-components/chat/chat-message/_example/configure/index.wxss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,15 @@
88
background-color: var(--td-bg-color-container);
99
padding: 32rpx 32rpx 0 32rpx;
1010
}
11+
12+
.name-block {
13+
display: flex;
14+
align-items: center;
15+
margin-right: 16rpx;
16+
}
17+
18+
.name-block image {
19+
margin-right: 16rpx;
20+
width: 40rpx;
21+
height: 40rpx;
22+
}

packages/pro-components/chat/chat-message/_example/content/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Component({
1919
},
2020
pic1: {
2121
role: 'user',
22+
name: '张三',
23+
avatar: 'https://tdesign.gtimg.com/site/avatar.jpg',
2224
content: [
2325
{
2426
type: 'attachment',

packages/pro-components/chat/chat-message/_example/content/index.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"component": true,
33
"usingComponents": {
44
"t-chat-message": "tdesign-miniprogram/chat-message/chat-message",
5-
"t-chat-actionbar": "tdesign-miniprogram/chat-actionbar/chat-actionbar"
5+
"t-chat-actionbar": "tdesign-miniprogram/chat-actionbar/chat-actionbar",
6+
"t-avatar": "tdesign-miniprogram/avatar/avatar"
67
}
7-
}
8+
}

packages/pro-components/chat/chat-message/_example/content/index.wxml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
<view class="chat-example">
22
<!-- 附件消息 -->
3-
<view class="chat-example-block">
4-
<t-chat-message
5-
content="{{pic1.content}}"
6-
role="{{pic1.role}}"
7-
chatContentProps="{{chatContentProps}}"
8-
></t-chat-message>
9-
</view>
103
<view class="chat-example-block">
114
<t-chat-message
125
content="{{pic2.content}}"

packages/pro-components/chat/chat-message/chat-message.less

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
}
1919

2020
.@{attachments} + .@{chat-content}:not(:empty),
21-
.@{chat-thinking} + .@{chat-content}:not(:empty){
21+
.@{chat-thinking} + .@{chat-content}:not(:empty) {
2222
margin-top: 24rpx;
2323
}
2424

@@ -54,6 +54,10 @@
5454
&__header {
5555
.@{chat-message}__avatar {
5656
padding-top: calc(@chat-message-height + @spacer);
57+
58+
&:empty {
59+
padding-top: 0;
60+
}
5761
}
5862
}
5963

@@ -115,6 +119,10 @@
115119
.@{chat-message}__avatar {
116120
margin-left: @spacer-1;
117121
margin-right: 0;
122+
123+
&:empty {
124+
margin-left: 0;
125+
}
118126
}
119127
}
120128

@@ -126,6 +134,11 @@
126134
margin-right: @spacer-1;
127135
padding-top: calc(@chat-message-height + @spacer);
128136

137+
&:empty {
138+
padding-top: 0;
139+
margin-right: 0;
140+
}
141+
129142
&-image {
130143
width: @spacer-4;
131144
height: @spacer-4;
@@ -157,6 +170,11 @@
157170
color: @text-color-placeholder;
158171
// padding-left: @spacer-2;
159172
margin-bottom: @spacer;
173+
174+
&:empty {
175+
height: 0;
176+
margin-bottom: 0;
177+
}
160178
}
161179

162180
// 名称样式

packages/pro-components/chat/chat-message/chat-message.wxml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<wxs src="../../../components/common/utils.wxs" module="_" />
22

33
<view class="class {{chatItemClass}}" style="{{_._style([style, customStyle])}}" bindlongpress="handleLongPress">
4-
<view wx:if="{{showAvatar}}" class="{{classPrefix}}__avatar">
4+
<view class="{{classPrefix}}__avatar">
5+
<!-- 头像插槽 -->
6+
<slot name="avatar" />
57
<block wx:if="{{avatar}}">
68
<image src="{{avatar}}" class="{{classPrefix}}__avatar-image"></image>
79
</block>
810
</view>
911
<view class="{{contentClasses}}">
10-
<view wx:if="{{showName || showDateTime}}" class="{{classPrefix}}__base">
12+
<view class="{{classPrefix}}__base">
13+
<!-- 昵称插槽 -->
14+
<slot name="name" />
1115
<text wx:if="{{showName}}" class="{{classPrefix}}__name">{{name}}</text>
1216
<text wx:if="{{datetime}}" class="{{classPrefix}}__time">{{datetime}}</text>
1317
</view>

packages/pro-components/chat/chat-message/props.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const props: TdChatMessageProps = {
3131
/** 对话单元的时间配置 */
3232
datetime: {
3333
type: String,
34+
value: '',
3435
},
3536
/** 自定义的昵称 */
3637
name: {

0 commit comments

Comments
 (0)