Skip to content

Commit

Permalink
fix(Message): fix messagePlugin instance error (#3514)
Browse files Browse the repository at this point in the history
  • Loading branch information
maoyiluo authored and uyarn committed Mar 7, 2025
1 parent 377a071 commit 5de0ed2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/message/messageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ export const MessageList = mixins(classPrefixMixins).extend({
},
methods: {
add(msg: MessageOptions): number {
const key = getUniqueId();
const mg = {
...msg,
key: getUniqueId(),
key,
placement: this.placement,
};
this.list.push(mg);
return this.list.length - 1;
return key;
},
remove(index: number) {
this.list.splice(index, 1);
Expand Down Expand Up @@ -74,6 +75,11 @@ export const MessageList = mixins(classPrefixMixins).extend({
'duration-end': () => this.remove(index),
};
},
getMessageInstance(key: string) {
const index = this.list.findIndex((msgOption) => msgOption.key === key);
if (index === -1) return null;
return this.$children[index];
},
},
render(): VNode {
if (!this.list.length) return;
Expand Down
8 changes: 5 additions & 3 deletions src/message/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const MessageFunction = (props: MessageOptions): Promise<MessageInstance> => {
}
const p = instanceMap.get(attachDom)[placement];

let messageKey: number | null = null;

// attachDom被清空(如attachDom.innerHtml=''),p也会存在,需要判断下dom包含关系
if (!p || !attachDom.contains(p.$el)) {
const instance = new MessageList({
Expand All @@ -79,18 +81,18 @@ const MessageFunction = (props: MessageOptions): Promise<MessageInstance> => {
placement: options.placement,
},
}).$mount();
instance.add(options);
messageKey = instance.add(options);
instanceMap.get(attachDom)[placement] = instance;
attachDom.appendChild(instance.$el);
} else {
p.add(options);
messageKey = p.add(options);
}
// 返回最新消息的 Element
return new Promise((resolve) => {
const ins = instanceMap.get(attachDom)[placement];
ins.$nextTick(() => {
const msg: Array<MessageInstance> = ins.$children;
resolve(msg[msg.length - 1]);
resolve(messageKey ? ins.getMessageInstance(messageKey) : msg[msg.length - 1]);
});
});
};
Expand Down

0 comments on commit 5de0ed2

Please sign in to comment.