Skip to content

Commit 73affb9

Browse files
committed
fix: correct ts type
1 parent b24a642 commit 73affb9

File tree

3 files changed

+14
-29
lines changed

3 files changed

+14
-29
lines changed

packages/components/_util/dom.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isString } from 'lodash-es';
22
import { getCssVarsValue } from './style';
3-
import type { AttachNode, AttachNodeReturnValue } from '../common';
3+
import type { AttachNode } from '../common';
44

55
// 用于判断是否可使用 dom
66
export const canUseDocument = !!(typeof window !== 'undefined' && window.document && window.document.createElement);
@@ -14,7 +14,7 @@ export const canUseDocument = !!(typeof window !== 'undefined' && window.documen
1414
*/
1515
export const isWindow = (val: unknown): val is Window => val === window;
1616

17-
export const getAttach = (node: AttachNode): AttachNodeReturnValue => {
17+
export const getAttach = (node: AttachNode): HTMLElement => {
1818
const attachNode = typeof node === 'function' ? node() : node;
1919
if (!attachNode) {
2020
return document.body;

packages/components/message/Message.tsx

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { PlacementOffset } from './const';
1010
import MessageComponent from './MessageComponent';
1111
import { useMessageClass } from './useMessageClass';
1212

13-
import type { AttachNodeReturnValue } from '../common';
1413
import type {
1514
MessageCloseAllMethod,
1615
MessageConfigMethod,
@@ -56,7 +55,7 @@ interface ContainerInstance {
5655
let messageKey = 1;
5756

5857
// 不同 attach 和 placement 对应的消息容器
59-
const MessageContainerMaps: Map<AttachNodeReturnValue, Map<MessagePlacementList, ContainerInstance>> = new Map();
58+
const MessageContainerMaps: Map<HTMLElement, Map<MessagePlacementList, ContainerInstance>> = new Map();
6059

6160
const MessageContainer: React.FC<MessageContainerProps> = (props) => {
6261
const { placement, children, zIndex, renderCallback } = props;
@@ -93,15 +92,15 @@ const MessageContainer: React.FC<MessageContainerProps> = (props) => {
9392
);
9493
};
9594

96-
function getAttachNodeMap(attachNode: AttachNodeReturnValue) {
95+
function getAttachNodeMap(attachNode: HTMLElement) {
9796
if (!MessageContainerMaps.has(attachNode)) {
9897
MessageContainerMaps.set(attachNode, new Map());
9998
}
10099
return MessageContainerMaps.get(attachNode);
101100
}
102101

103102
async function findExistingContainer(
104-
attachNode: AttachNodeReturnValue,
103+
attachNode: HTMLElement,
105104
placement: MessagePlacementList,
106105
zIndex?: number,
107106
) {
@@ -162,15 +161,6 @@ async function renderElement(theme, config: MessageOptions): Promise<MessageInst
162161
const index = containerInstance.messages.indexOf(message);
163162
if (index === -1) return;
164163
containerInstance.messages.splice(index, 1);
165-
// 如果容器内没有消息,整个移除
166-
if (containerInstance.messages.length === 0) {
167-
containerInstance.container.remove();
168-
const attachNodeMap = getAttachNodeMap(attachNode);
169-
attachNodeMap.delete(placement);
170-
if (attachNodeMap.size === 0) {
171-
MessageContainerMaps.delete(attachNode);
172-
}
173-
}
174164
},
175165
};
176166

@@ -259,13 +249,11 @@ MessagePlugin.close = (messageInstance) => {
259249
* @desc 关闭所有的 message
260250
*/
261251
MessagePlugin.closeAll = (): MessageCloseAllMethod => {
262-
// 收集所有需要关闭的消息实例
263252
const allMessages: MessageInstance[] = [];
264-
265253
MessageContainerMaps.forEach((placementMap) => {
266-
placementMap.forEach((containerInfo) => {
267-
// 复制消息数组,避免在关闭过程中数组被修改
268-
allMessages.push(...containerInfo.messages.slice());
254+
placementMap.forEach((instance) => {
255+
// 收集需要关闭的消息实例,避免同时遍历与删除导致的索引错乱问题
256+
allMessages.push(...instance.messages.slice());
269257
});
270258
});
271259

@@ -275,8 +263,6 @@ MessagePlugin.closeAll = (): MessageCloseAllMethod => {
275263
message.close();
276264
}
277265
});
278-
279-
MessageContainerMaps.clear();
280266
return;
281267
};
282268

packages/components/popup/PopupPlugin.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1+
import React, { useLayoutEffect, useMemo, useRef, useState } from 'react';
2+
import { CSSTransition } from 'react-transition-group';
13
import { createPopper, Instance, Placement, type Options } from '@popperjs/core';
24
import classNames from 'classnames';
35
import { isString } from 'lodash-es';
4-
import React, { useLayoutEffect, useMemo, useRef, useState } from 'react';
5-
import { CSSTransition } from 'react-transition-group';
66
import { getAttach } from '../_util/dom';
77
import { off, on } from '../_util/listener';
88
import { render, unmount } from '../_util/react-render';
9-
import type { TNode } from '../common';
109
import PluginContainer from '../common/PluginContainer';
1110
import ConfigProvider from '../config-provider';
1211
import useDefaultProps from '../hooks/useDefaultProps';
1312
import { popupDefaultProps } from './defaultProps';
13+
14+
import type { AttachNode, TNode } from '../common';
1415
import type { TdPopupProps } from './type';
1516

1617
export interface PopupPluginApi {
1718
config: TdPopupProps;
1819
}
1920

20-
type TriggerEl = string | HTMLElement;
21-
2221
export interface OverlayProps extends TdPopupProps {
23-
triggerEl: TriggerEl;
22+
triggerEl: AttachNode;
2423
renderCallback: (instance: HTMLElement) => void;
2524
}
2625

@@ -172,7 +171,7 @@ function removeOverlayInstance() {
172171
}
173172
}
174173

175-
export type PluginMethod = (triggerEl: TriggerEl, content: TNode, popupProps?: TdPopupProps) => Promise<Instance>;
174+
export type PluginMethod = (triggerEl: AttachNode, content: TNode, popupProps?: TdPopupProps) => Promise<Instance>;
176175

177176
const renderInstance = (props, attach: HTMLElement): Promise<HTMLElement> =>
178177
new Promise((resolve) => {

0 commit comments

Comments
 (0)