Skip to content

Commit

Permalink
fix(Notification): fix attach render bug (#3306)
Browse files Browse the repository at this point in the history
* fix(notification): attach传递root id 时,notification会覆盖整个react app render 出的dom节点

* chore: update snapshot

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
centuryPark and github-actions[bot] authored Jan 7, 2025
1 parent 7d321e9 commit f59fa5f
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/notification/NotificationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ const renderNotification = (theme: NotificationThemeList, options: NotificationI
const { placement } = configs;

const attach: HTMLElement = (() => {
if (options.attach && typeof options.attach === 'string') {
const element: Element = document.querySelector(options.attach);
if (element instanceof HTMLElement) return element;
}

if (options.attach instanceof HTMLElement) return options.attach;

const containerId = `tdesign-notification-${placement}`;
const container = document.querySelector(`#${containerId}`);
if (container && container instanceof HTMLElement) {
Expand All @@ -54,6 +47,13 @@ const renderNotification = (theme: NotificationThemeList, options: NotificationI

const element: HTMLDivElement = document.createElement('div');
element.setAttribute('id', containerId);

if (options.attach && typeof options.attach === 'string') {
const container: Element = document.querySelector(options.attach);
if (container instanceof HTMLElement) return container.appendChild(element);
}

if (options.attach instanceof HTMLElement) return options.attach.appendChild(element);
document.body.appendChild(element);
return element;
})();
Expand Down
63 changes: 63 additions & 0 deletions src/notification/_example/attach.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { useEffect, useRef } from 'react';
import { NotificationPlugin, Button, Space } from 'tdesign-react';

export default function NotificationExample() {
const timerRef = useRef<any>();
const openInfoNotification = () => {
NotificationPlugin.info({
title: '信息',
content: '这是一条可以自动关闭的消息通知, 未配置attach',
duration: 3000,
});
};

const openSuccessNotification = () => {
NotificationPlugin.success({
attach: '#app',
title: '信息',
content: '这是一条可以自动关闭的消息通知, 配置attach 为 #app',
duration: 3000,
});
};

const openWarningNotification = () => {
NotificationPlugin.warning({
attach: '#app',
title: '信息',
content: '这是一条可以自动关闭的消息通知, 配置attach 为 #app',
duration: 3000,
});
};

const openErrorNotification = () => {
NotificationPlugin.error({
attach: '#app',
title: '信息',
content: '这是一条可以自动关闭的消息通知, 配置attach 为 #app',
duration: 3000,
});
};

useEffect(() => {
if (timerRef.current) {
clearTimeout(timerRef.current);
}
timerRef.current = setTimeout(() => {
NotificationPlugin.error({
attach: '#app',
title: '信息',
content: '这是一条可以自动关闭的消息通知, useEffect定时2000ms触发',
duration: 10000,
});
}, 2000);
}, []);

return (
<Space>
<Button onClick={() => openInfoNotification()}>信息</Button>
<Button onClick={() => openSuccessNotification()}>成功</Button>
<Button onClick={() => openWarningNotification()}>警告</Button>
<Button onClick={() => openErrorNotification()}>错误</Button>
</Space>
);
}
68 changes: 68 additions & 0 deletions test/snap/__snapshots__/csr.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -64047,6 +64047,72 @@ exports[`csr snapshot test > csr test src/message/_example/offset.tsx 1`] = `
</div>
`;

exports[`csr snapshot test > csr test src/notification/_example/attach.tsx 1`] = `
<div>
<div
class="t-space t-space-horizontal"
style="gap: 16px;"
>
<div
class="t-space-item"
>
<button
class="t-button t-button--theme-primary t-button--variant-base"
type="button"
>
<span
class="t-button__text"
>
信息
</span>
</button>
</div>
<div
class="t-space-item"
>
<button
class="t-button t-button--theme-primary t-button--variant-base"
type="button"
>
<span
class="t-button__text"
>
成功
</span>
</button>
</div>
<div
class="t-space-item"
>
<button
class="t-button t-button--theme-primary t-button--variant-base"
type="button"
>
<span
class="t-button__text"
>
警告
</span>
</button>
</div>
<div
class="t-space-item"
>
<button
class="t-button t-button--theme-primary t-button--variant-base"
type="button"
>
<span
class="t-button__text"
>
错误
</span>
</button>
</div>
</div>
</div>
`;

exports[`csr snapshot test > csr test src/notification/_example/base.tsx 1`] = `
<div>
<div
Expand Down Expand Up @@ -134886,6 +134952,8 @@ exports[`ssr snapshot test > ssr test src/message/_example/methods.tsx 1`] = `"<

exports[`ssr snapshot test > ssr test src/message/_example/offset.tsx 1`] = `"<div style="gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><div style="gap:16px" class="t-space t-space-horizontal"><div class="t-space-item"><div style="width:200px" class="t-input__wrap" value="0" spellcheck="false"><div class="t-input t-align-left"><input placeholder="请输入横向偏移量" type="text" class="t-input__inner" value="0"/></div></div></div><div class="t-space-item"><div style="width:200px;margin-left:16px" class="t-input__wrap" value="0" spellcheck="false"><div class="t-input t-align-left"><input placeholder="请输入纵向偏移量" type="text" class="t-input__inner" value="0"/></div></div></div></div></div><div class="t-space-item"><div style="position:relative;margin:0 auto;width:500px;height:260px;gap:16px" class="t-space t-space-horizontal"><div class="t-space-item"><button style="position:absolute;right:42%;bottom:42%" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">center</span></button></div><div class="t-space-item"><button style="position:absolute;top:0;left:42%" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">top</span></button></div><div class="t-space-item"><button style="position:absolute;left:0;top:42%" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">left</span></button></div><div class="t-space-item"><button style="position:absolute;right:0;top:42%" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">right</span></button></div><div class="t-space-item"><button style="position:absolute;bottom:0;left:42%" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">bottom</span></button></div><div class="t-space-item"><button style="position:absolute;top:0;left:70px" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">top-left</span></button></div><div class="t-space-item"><button style="position:absolute;top:0;right:70px" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">top-right</span></button></div><div class="t-space-item"><button style="position:absolute;bottom:0;left:70px" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">bottom-left</span></button></div><div class="t-space-item"><button style="position:absolute;bottom:0;right:70px" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">bottom-right</span></button></div></div></div></div>"`;

exports[`ssr snapshot test > ssr test src/notification/_example/attach.tsx 1`] = `"<div style="gap:16px" class="t-space t-space-horizontal"><div class="t-space-item"><button type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">信息</span></button></div><div class="t-space-item"><button type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">成功</span></button></div><div class="t-space-item"><button type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">警告</span></button></div><div class="t-space-item"><button type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">错误</span></button></div></div>"`;

exports[`ssr snapshot test > ssr test src/notification/_example/base.tsx 1`] = `"<div class="t-notification t-notification-is-info"><div class="t-notification__icon"><svg fill="none" viewBox="0 0 24 24" width="1em" height="1em" class="t-icon t-icon-info-circle-filled t-is-info"><path fill="currentColor" d="M12 23a11 11 0 100-22 11 11 0 000 22zM11 8.5v-2h2v2h-2zm2 1.5v7.5h-2V10h2z"></path></svg></div><div class="t-notification__main"><div class="t-notification__title__wrap"><span class="t-notification__title">标题名称</span><div class="t-notification-close"></div></div><div class="t-notification__content">这是一条消息通知</div></div></div>"`;

exports[`ssr snapshot test > ssr test src/notification/_example/close-all.tsx 1`] = `"<div style="gap:16px" class="t-space t-space-horizontal"><div class="t-space-item"><button type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">点击打开多个消息</span></button></div><div class="t-space-item"><button type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">点击关闭多个消息</span></button></div></div>"`;
Expand Down
2 changes: 2 additions & 0 deletions test/snap/__snapshots__/ssr.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,8 @@ exports[`ssr snapshot test > ssr test src/message/_example/methods.tsx 1`] = `"<

exports[`ssr snapshot test > ssr test src/message/_example/offset.tsx 1`] = `"<div style="gap:16px" class="t-space t-space-vertical"><div class="t-space-item"><div style="gap:16px" class="t-space t-space-horizontal"><div class="t-space-item"><div style="width:200px" class="t-input__wrap" value="0" spellcheck="false"><div class="t-input t-align-left"><input placeholder="请输入横向偏移量" type="text" class="t-input__inner" value="0"/></div></div></div><div class="t-space-item"><div style="width:200px;margin-left:16px" class="t-input__wrap" value="0" spellcheck="false"><div class="t-input t-align-left"><input placeholder="请输入纵向偏移量" type="text" class="t-input__inner" value="0"/></div></div></div></div></div><div class="t-space-item"><div style="position:relative;margin:0 auto;width:500px;height:260px;gap:16px" class="t-space t-space-horizontal"><div class="t-space-item"><button style="position:absolute;right:42%;bottom:42%" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">center</span></button></div><div class="t-space-item"><button style="position:absolute;top:0;left:42%" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">top</span></button></div><div class="t-space-item"><button style="position:absolute;left:0;top:42%" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">left</span></button></div><div class="t-space-item"><button style="position:absolute;right:0;top:42%" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">right</span></button></div><div class="t-space-item"><button style="position:absolute;bottom:0;left:42%" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">bottom</span></button></div><div class="t-space-item"><button style="position:absolute;top:0;left:70px" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">top-left</span></button></div><div class="t-space-item"><button style="position:absolute;top:0;right:70px" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">top-right</span></button></div><div class="t-space-item"><button style="position:absolute;bottom:0;left:70px" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">bottom-left</span></button></div><div class="t-space-item"><button style="position:absolute;bottom:0;right:70px" type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">bottom-right</span></button></div></div></div></div>"`;

exports[`ssr snapshot test > ssr test src/notification/_example/attach.tsx 1`] = `"<div style="gap:16px" class="t-space t-space-horizontal"><div class="t-space-item"><button type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">信息</span></button></div><div class="t-space-item"><button type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">成功</span></button></div><div class="t-space-item"><button type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">警告</span></button></div><div class="t-space-item"><button type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">错误</span></button></div></div>"`;

exports[`ssr snapshot test > ssr test src/notification/_example/base.tsx 1`] = `"<div class="t-notification t-notification-is-info"><div class="t-notification__icon"><svg fill="none" viewBox="0 0 24 24" width="1em" height="1em" class="t-icon t-icon-info-circle-filled t-is-info"><path fill="currentColor" d="M12 23a11 11 0 100-22 11 11 0 000 22zM11 8.5v-2h2v2h-2zm2 1.5v7.5h-2V10h2z"></path></svg></div><div class="t-notification__main"><div class="t-notification__title__wrap"><span class="t-notification__title">标题名称</span><div class="t-notification-close"></div></div><div class="t-notification__content">这是一条消息通知</div></div></div>"`;

exports[`ssr snapshot test > ssr test src/notification/_example/close-all.tsx 1`] = `"<div style="gap:16px" class="t-space t-space-horizontal"><div class="t-space-item"><button type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">点击打开多个消息</span></button></div><div class="t-space-item"><button type="button" class="t-button t-button--theme-primary t-button--variant-base"><span class="t-button__text">点击关闭多个消息</span></button></div></div>"`;
Expand Down

0 comments on commit f59fa5f

Please sign in to comment.