24
24
// import type {BPopoverEmits, BPopoverProps} from '@/types/components'
25
25
import type {BPopoverDelayObject } from ' @/types/components'
26
26
import Popover from ' bootstrap/js/dist/popover'
27
- import {ComponentPublicInstance , computed , nextTick , onMounted , ref , useSlots , watch } from ' vue'
27
+ import {
28
+ ComponentPublicInstance ,
29
+ computed ,
30
+ nextTick ,
31
+ onBeforeUnmount ,
32
+ onMounted ,
33
+ ref ,
34
+ useSlots ,
35
+ watch ,
36
+ } from ' vue'
28
37
import useEventListener from ' @/composables/useEventListener'
29
38
import type {ColorVariant } from ' @/types'
30
39
@@ -100,23 +109,29 @@ const getElement = (element: HTMLElement | string | undefined): HTMLElement | un
100
109
return element
101
110
}
102
111
112
+ const generatePopoverInstance = (
113
+ targetValue : string | ComponentPublicInstance <HTMLElement > | HTMLElement | undefined
114
+ ) => {
115
+ target .value = getElement (cleanElementProp (targetValue ))
116
+
117
+ if (! target .value ) return
118
+
119
+ instance .value = new Popover (target .value , {
120
+ container: cleanElementProp (props .container ),
121
+ trigger: props .triggers ,
122
+ placement: props .placement ,
123
+ title: props .title || slots .title ? titleRef .value : ' ' ,
124
+ content: contentRef .value ,
125
+ html: props .html ,
126
+ delay: props .delay ,
127
+ sanitize: props .sanitize ,
128
+ offset: props .offset ,
129
+ })
130
+ }
131
+
103
132
onMounted (() => {
104
133
nextTick (() => {
105
- target .value = getElement (cleanElementProp (props .target ))
106
-
107
- if (target .value )
108
- instance .value = new Popover (target .value , {
109
- container: cleanElementProp (props .container ),
110
- trigger: props .triggers ,
111
- placement: props .placement ,
112
- title: props .title || slots .title ? titleRef .value : ' ' ,
113
- content: contentRef .value ,
114
- html: props .html ,
115
- delay: props .delay ,
116
- sanitize: props .sanitize ,
117
- offset: props .offset ,
118
- })
119
- else console .warn (' [B-Popover] Target is a mandatory props.' )
134
+ generatePopoverInstance (props .target )
120
135
})
121
136
122
137
element .value ?.parentNode ?.removeChild (element .value )
@@ -126,6 +141,18 @@ onMounted(() => {
126
141
}
127
142
})
128
143
144
+ onBeforeUnmount (() => {
145
+ instance .value ?.dispose ()
146
+ })
147
+
148
+ watch (
149
+ () => props .target ,
150
+ (newValue ) => {
151
+ instance .value ?.dispose ()
152
+ generatePopoverInstance (newValue )
153
+ }
154
+ )
155
+
129
156
watch (
130
157
() => props .show ,
131
158
(show , oldVal ) => {
0 commit comments