Skip to content

Commit 13db4a1

Browse files
baozjjbaozhangjieuyarntdesign-bot
authored
fix(notification): 修复 NotifyPlugin.close 错误触发 onCloseBtnClick 的问题 (#5958)
* fix(notification): 修复 NotifyPlugin.close 错误触发 onCloseBtnClick 的问题 onCloseBtnClick 事件本应仅在用户手动点击关闭按钮时触发。但在之前的实现中,通过代码调用 NotifyPlugin.close() 方法同样会错误地触发该事件。 这会导致开发者在 onCloseBtnClick 回调中编写的、仅针对用户行为的逻辑被意外执行,从而引发潜在的 Bug。 新增一个 onClose 事件,将程序化关闭和用户手动关闭的逻辑进行解耦。 现在,NotifyPlugin.close() 只会触发内部的 onClose 来销毁组件,确保了 onCloseBtnClick 行为的正确性。 BREAKING CHANGE: 调用 NotifyPlugin.close() 将不再触发 onCloseBtnClick 事件。 * refactor(notification): 移除演示代码 * chore: complete docs change * chore: stash changelog [ci skip] * chore: typo --------- Co-authored-by: baozhangjie <[email protected]> Co-authored-by: Uyarn <[email protected]> Co-authored-by: tdesign-bot <[email protected]>
1 parent b4eae07 commit 13db4a1

File tree

7 files changed

+32
-2
lines changed

7 files changed

+32
-2
lines changed

packages/components/notification/notification-list.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ export default defineComponent({
7373
}
7474
return remove(index);
7575
},
76+
onClose: () => {
77+
if (item.onClose) {
78+
item.onClose();
79+
}
80+
return remove(index);
81+
},
7682
};
7783
};
7884

packages/components/notification/notification.en-US.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ footer | String / Slot / Function | - | Typescript:`string \| TNode`。[see mo
1414
icon | Boolean / Slot / Function | true | Typescript:`boolean \| TNode`[see more ts definition](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/common.ts) | N
1515
theme | String | info | options: info/success/warning/error。Typescript:`NotificationThemeList` `type NotificationThemeList = 'info' \| 'success' \| 'warning' \| 'error'`[see more ts definition](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/notification/type.ts) | N
1616
title | String / Slot / Function | - | Typescript:`string \| TNode`[see more ts definition](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/common.ts) | N
17+
onClose | Function | | Typescript::`() => void`<br/> the callback event of NotificationPlugin.close | N
1718
onCloseBtnClick | Function | | Typescript:`(context: { e: MouseEvent }) => void`<br/> | N
1819
onDurationEnd | Function | | Typescript:`() => void`<br/> | N
1920

2021
### Notification Events
2122

2223
name | params | description
2324
-- | -- | --
25+
close | \- | the callback event of NotificationPlugin.close
2426
close-btn-click | `(context: { e: MouseEvent })` | \-
2527
duration-end | \- | \-
2628

packages/components/notification/notification.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ footer | String / Slot / Function | - | 用于自定义底部内容。TS 类型
2727
icon | Boolean / Slot / Function | true | 用于自定义消息通知前面的图标,优先级大于 theme 设定的图标。值为 false 则不显示图标,值为 true 显示 theme 设定图标。TS 类型:`boolean \| TNode`[通用类型定义](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/common.ts) | N
2828
theme | String | info | 消息类型。可选项:info/success/warning/error。TS 类型:`NotificationThemeList` `type NotificationThemeList = 'info' \| 'success' \| 'warning' \| 'error'`[详细类型定义](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/notification/type.ts) | N
2929
title | String / Slot / Function | - | 标题。TS 类型:`string \| TNode`[通用类型定义](https://github.com/Tencent/tdesign-vue-next/blob/develop/packages/components/common.ts) | N
30+
onClose | Function | | TS 类型:`() => void`<br/>调用 NotificationPlugin.close 的事件回调 | N
3031
onCloseBtnClick | Function | | TS 类型:`(context: { e: MouseEvent }) => void`<br/>点击关闭按钮时触发 | N
3132
onDurationEnd | Function | | TS 类型:`() => void`<br/>计时结束时触发 | N
3233

3334
### Notification Events
3435

3536
名称 | 参数 | 描述
3637
-- | -- | --
38+
close | \- | 调用 NotificationPlugin.close 的事件回调
3739
close-btn-click | `(context: { e: MouseEvent })` | 点击关闭按钮时触发
3840
duration-end | \- | 计时结束时触发
3941

packages/components/notification/notification.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,20 @@ export default defineComponent({
3030
const timer = ref(null);
3131
const notificationRef = ref(null);
3232

33-
const close = (e?: MouseEvent) => {
33+
const handleCloseBtnClick = (e?: MouseEvent) => {
3434
const dom = notificationRef.value as HTMLElement;
3535
fadeOut(dom, props.placement, () => {
3636
props.onCloseBtnClick?.({ e });
3737
});
3838
};
3939

40+
const close = () => {
41+
const dom = notificationRef.value as HTMLElement;
42+
fadeOut(dom, props.placement, () => {
43+
props.onClose?.();
44+
});
45+
};
46+
4047
const renderIcon = () => {
4148
let iconContent;
4249
if (props.icon === false) return null;
@@ -59,7 +66,7 @@ export default defineComponent({
5966
const renderClose = () => {
6067
const defaultClose = <CloseIcon />;
6168
return (
62-
<span class={`${classPrefix.value}-message__close`} onClick={close}>
69+
<span class={`${classPrefix.value}-message__close`} onClick={handleCloseBtnClick}>
6370
{renderTNode('closeBtn', defaultClose)}
6471
</span>
6572
);

packages/components/notification/props.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@ export default {
5252
onCloseBtnClick: Function as PropType<TdNotificationProps['onCloseBtnClick']>,
5353
/** 计时结束时触发 */
5454
onDurationEnd: Function as PropType<TdNotificationProps['onDurationEnd']>,
55+
/** 调用 NotificationPlugin.close 时触发 */
56+
onClose: Function as PropType<TdNotificationProps['onClose']>,
5557
};

packages/components/notification/type.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export interface TdNotificationProps {
5050
* 计时结束时触发
5151
*/
5252
onDurationEnd?: () => void;
53+
/**
54+
* 调用 NotificationPlugin.close 时触发
55+
*/
56+
onClose?: () => void;
5357
}
5458

5559
export interface NotificationOptions extends TdNotificationProps {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
pr_number: 5958
3+
contributor: baozjj
4+
---
5+
6+
- feat(Notification): 新增 `onClose` 事件,用于处理调用 `NotifyPlugin.close()` 的相关回调场景 @baozjj ([#5958](https://github.com/Tencent/tdesign-vue-next/pull/5958))
7+
- fix(Notification): 修复调用 `NotifyPlugin.close()` 错误触发 `onCloseBtnClick` 回调的问题 @baozjj ([#5958](https://github.com/Tencent/tdesign-vue-next/pull/5958))

0 commit comments

Comments
 (0)