1
1
import {
2
2
defineComponent ,
3
3
h ,
4
- reactive ,
5
4
ref ,
6
5
VNode ,
7
6
onBeforeMount ,
@@ -79,10 +78,15 @@ const CCarousel = defineComponent({
79
78
} ,
80
79
setup ( props , { slots } ) {
81
80
const carouselRef = ref ( )
82
- const timeout = ref ( )
81
+
82
+ const active = ref ( props . index )
83
83
const animating = ref ( false )
84
- const visible = ref ( )
85
84
const customInterval = ref < boolean | number > ( props . interval )
85
+ const direction = ref ( 'next' )
86
+ const items = ref < VNode [ ] > ( [ ] )
87
+ const timeout = ref ( )
88
+ const visible = ref ( )
89
+
86
90
const setAnimating = ( value : boolean ) => {
87
91
animating . value = value
88
92
}
@@ -105,28 +109,15 @@ const CCarousel = defineComponent({
105
109
}
106
110
}
107
111
108
- const state = reactive ( {
109
- items : [ ] as VNode [ ] ,
110
- active : props . index ,
111
- direction : 'next' ,
112
- } )
113
-
114
- onBeforeMount ( ( ) => {
115
- if ( slots . default ) {
116
- // @ts -expect-error TODO: fix types
117
- state . items = slots . default ( ) . filter ( ( child ) => child . type . name === 'CCarouselItem' )
118
- }
119
- } )
120
-
121
- const handleControlClick = ( direction : string ) => {
112
+ const handleControlClick = ( _direction : string ) => {
122
113
if ( animating . value ) {
123
114
return
124
115
}
125
- state . direction = direction
126
- if ( direction === 'next' ) {
127
- state . active === state . items . length - 1 ? ( state . active = 0 ) : state . active ++
116
+ direction . value = _direction
117
+ if ( _direction === 'next' ) {
118
+ active . value === items . value . length - 1 ? ( active . value = 0 ) : active . value ++
128
119
} else {
129
- state . active === 0 ? ( state . active = state . items . length - 1 ) : state . active --
120
+ active . value === 0 ? ( active . value = items . value . length - 1 ) : active . value --
130
121
}
131
122
}
132
123
@@ -139,19 +130,19 @@ const CCarousel = defineComponent({
139
130
}
140
131
141
132
const handleIndicatorClick = ( index : number ) => {
142
- if ( state . active === index ) {
133
+ if ( active . value === index ) {
143
134
return
144
135
}
145
136
146
- if ( state . active < index ) {
147
- state . direction = 'next'
148
- state . active = index
137
+ if ( active . value < index ) {
138
+ direction . value = 'next'
139
+ active . value = index
149
140
return
150
141
}
151
142
152
- if ( state . active > index ) {
153
- state . direction = 'prev'
154
- state . active = index
143
+ if ( active . value > index ) {
144
+ direction . value = 'prev'
145
+ active . value = index
155
146
}
156
147
}
157
148
@@ -163,6 +154,17 @@ const CCarousel = defineComponent({
163
154
}
164
155
}
165
156
157
+ onBeforeMount ( ( ) => {
158
+ if ( slots . default ) {
159
+ const children = typeof slots . default ( ) [ 0 ] . type === 'symbol' ? slots . default ( ) [ 0 ] . children : slots . default ( )
160
+
161
+ if ( children && Array . isArray ( children ) ) {
162
+ // @ts -expect-error TODO: fix types
163
+ items . value = children . filter ( ( child ) => child . type . name === 'CCarouselItem' )
164
+ }
165
+ }
166
+ } )
167
+
166
168
onMounted ( ( ) => {
167
169
window . addEventListener ( 'scroll' , handleScroll )
168
170
} )
@@ -174,7 +176,7 @@ const CCarousel = defineComponent({
174
176
return
175
177
}
176
178
177
- if ( ! props . wrap && state . active < state . items . length - 1 ) {
179
+ if ( ! props . wrap && active . value < items . value . length - 1 ) {
178
180
! animating . value && cycle ( )
179
181
}
180
182
} )
@@ -204,23 +206,23 @@ const CCarousel = defineComponent({
204
206
{
205
207
class : 'carousel-indicators' ,
206
208
} ,
207
- state . items . map ( ( _ , index ) => {
209
+ items . value . map ( ( _ , index ) => {
208
210
return h ( 'button' , {
209
211
type : 'button' ,
210
212
id : index ,
211
213
'data-coreui-target' : '' ,
212
- ...( state . active === index && { class : 'active' } ) ,
214
+ ...( active . value === index && { class : 'active' } ) ,
213
215
onClick : ( ) => handleIndicatorClick ( index ) ,
214
216
} )
215
217
} ) ,
216
218
) ,
217
219
h (
218
220
'div' ,
219
221
{ class : 'carousel-inner' } ,
220
- state . items . map ( ( item , index ) => {
222
+ items . value . map ( ( item , index ) => {
221
223
return h ( item , {
222
- active : state . active === index ? true : false ,
223
- direction : state . direction ,
224
+ active : active . value === index ? true : false ,
225
+ direction : direction . value ,
224
226
} )
225
227
} ) ,
226
228
) ,
0 commit comments