Skip to content

Commit 1e61f32

Browse files
jevin98sleeprite
authored andcommitted
!473 fix(popper): 多层级点击外部隐藏失效
* fix(popper): 多层级点击外部隐藏失效
1 parent e623edc commit 1e61f32

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

packages/component/component/popper/component/content.vue

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ const props = withDefaults(defineProps<ContentProps>(), {});
2727
const ContentRef = ref<HTMLElement | null>(null);
2828
const ArrowRef = ref<HTMLDivElement | null>(null);
2929
30-
const subContents = ref<Array<Ref<HTMLElement>>>([]);
30+
const currentContents: Array<Ref<HTMLElement>> = [];
3131
3232
const { TriggerRef, onShow, onHidden } = inject(POPPER_INJECTION_KEY)!;
33-
const { collectorSubContent } = inject(CONTENT_INJECTION_KEY, {});
33+
const { allContents: parentAllContents = [] } = inject(
34+
CONTENT_INJECTION_KEY,
35+
{}
36+
);
3437
3538
const isExist = ref(props.modelValue);
3639
const _visible = ref(false);
@@ -110,8 +113,30 @@ watch(innerVisible, () => {
110113
}
111114
});
112115
116+
const removeCurrentContentLink = () => {
117+
/**
118+
* 删除当前 `currentContents` 管理的队列
119+
*/
120+
parentAllContents.splice(
121+
parentAllContents.findIndex((contents) => contents === currentContents),
122+
1
123+
);
124+
125+
/**
126+
* 删除在其他队列中的当前 `ContentRef`
127+
*/
128+
parentAllContents.forEach((contents) => {
129+
contents.splice(
130+
contents.findIndex((content) => content.value === ContentRef.value),
131+
1
132+
);
133+
});
134+
};
135+
113136
onBeforeUnmount(() => {
114137
stopAutoUpdate.value && stopAutoUpdate.value();
138+
139+
removeCurrentContentLink();
115140
});
116141
117142
onClickOutside(ContentRef, (event: PointerEvent) => {
@@ -122,7 +147,7 @@ onClickOutside(ContentRef, (event: PointerEvent) => {
122147
)
123148
return;
124149
125-
for (const item of subContents.value) {
150+
for (const item of currentContents) {
126151
if (item.value?.contains(event.target as HTMLElement)) {
127152
return;
128153
}
@@ -155,15 +180,13 @@ const hidden = () => {
155180
};
156181
157182
onMounted(() => {
158-
collectorSubContent?.(ContentRef as Ref<HTMLElement>);
183+
parentAllContents.forEach((content) => {
184+
content.push(ContentRef as Ref<HTMLElement>);
185+
});
159186
});
160187
161-
const _collectorSubContent = (sub: Ref<HTMLElement>) => {
162-
subContents.value.push(sub);
163-
};
164-
165188
provide(CONTENT_INJECTION_KEY, {
166-
collectorSubContent: _collectorSubContent,
189+
allContents: [...parentAllContents, currentContents],
167190
});
168191
169192
defineExpose({ show, hidden, update });

packages/component/component/popper/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export type ContentComponentInstance = ComponentPublicInstance<{
4747
}>;
4848

4949
export type ContentContext = {
50-
collectorSubContent?: (sub: Ref<HTMLElement>) => void;
50+
allContents?: Ref<HTMLElement>[][];
5151
};
5252

5353
export const CONTENT_INJECTION_KEY: InjectionKey<ContentContext> =

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy