@@ -27,10 +27,13 @@ const props = withDefaults(defineProps<ContentProps>(), {});
27
27
const ContentRef = ref <HTMLElement | null >(null );
28
28
const ArrowRef = ref <HTMLDivElement | null >(null );
29
29
30
- const subContents = ref < Array <Ref <HTMLElement >>>([]) ;
30
+ const currentContents : Array <Ref <HTMLElement >> = [] ;
31
31
32
32
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
+ );
34
37
35
38
const isExist = ref (props .modelValue );
36
39
const _visible = ref (false );
@@ -110,8 +113,30 @@ watch(innerVisible, () => {
110
113
}
111
114
});
112
115
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
+
113
136
onBeforeUnmount (() => {
114
137
stopAutoUpdate .value && stopAutoUpdate .value ();
138
+
139
+ removeCurrentContentLink ();
115
140
});
116
141
117
142
onClickOutside (ContentRef , (event : PointerEvent ) => {
@@ -122,7 +147,7 @@ onClickOutside(ContentRef, (event: PointerEvent) => {
122
147
)
123
148
return ;
124
149
125
- for (const item of subContents . value ) {
150
+ for (const item of currentContents ) {
126
151
if (item .value ?.contains (event .target as HTMLElement )) {
127
152
return ;
128
153
}
@@ -155,15 +180,13 @@ const hidden = () => {
155
180
};
156
181
157
182
onMounted (() => {
158
- collectorSubContent ?.(ContentRef as Ref <HTMLElement >);
183
+ parentAllContents .forEach ((content ) => {
184
+ content .push (ContentRef as Ref <HTMLElement >);
185
+ });
159
186
});
160
187
161
- const _collectorSubContent = (sub : Ref <HTMLElement >) => {
162
- subContents .value .push (sub );
163
- };
164
-
165
188
provide (CONTENT_INJECTION_KEY , {
166
- collectorSubContent: _collectorSubContent ,
189
+ allContents: [ ... parentAllContents , currentContents ] ,
167
190
});
168
191
169
192
defineExpose ({ show , hidden , update });
0 commit comments