|
| 1 | +import { defineComponent, renderSlot, useSlots, watch, toRefs, ref } from 'vue' |
| 2 | + |
| 3 | +// Components |
| 4 | +import DCarouselIndicator from './components/carousel-indicator' |
| 5 | +import DCarouselPrev from './components/carousel-prev' |
| 6 | +import DCarouselNext from './components/carousel-next' |
| 7 | + |
| 8 | +// Composables |
| 9 | +import usePage from './composables/use-page' |
| 10 | +import useAutoplay from './composables/use-autoplay' |
| 11 | + |
| 12 | +// Util |
| 13 | +import { formatPageIndex } from './carousel.util' |
| 14 | + |
| 15 | +// Props/Types |
| 16 | +import { carouselProps, CarouselProps } from './carousel.type' |
| 17 | + |
| 18 | +// SCSS |
| 19 | +import './carousel.scss' |
| 20 | + |
| 21 | +export default defineComponent({ |
| 22 | + name: 'DCarousel', |
| 23 | + components: { |
| 24 | + DCarouselIndicator, |
| 25 | + DCarouselPrev, |
| 26 | + DCarouselNext, |
| 27 | + }, |
| 28 | + props: carouselProps, |
| 29 | + emits: ['update:modelValue'], |
| 30 | + setup(props: CarouselProps, { slots, emit }) { |
| 31 | + const { modelValue, autoplay, interval } = toRefs(props) |
| 32 | + |
| 33 | + const { pageIndex, prevPage, nextPage, setPageIndex } = usePage(modelValue.value) |
| 34 | + const { startPlay, stopPlay } = useAutoplay(nextPage, interval.value) |
| 35 | + |
| 36 | + const count = useSlots().default().filter(item => typeof item.type !== 'symbol').length |
| 37 | + const defaultFormattedPageIndex = formatPageIndex(pageIndex.value, count) |
| 38 | + const formattedPageIndex = ref(defaultFormattedPageIndex) |
| 39 | + |
| 40 | + const launchTimer = (autoplay) => { |
| 41 | + if (autoplay) { |
| 42 | + startPlay() |
| 43 | + } else { |
| 44 | + stopPlay() |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + launchTimer(autoplay.value) |
| 49 | + |
| 50 | + watch(autoplay, (newVal) => { |
| 51 | + launchTimer(newVal) |
| 52 | + }) |
| 53 | + |
| 54 | + watch(modelValue, (newVal: number) => { |
| 55 | + pageIndex.value = newVal |
| 56 | + }) |
| 57 | + |
| 58 | + watch(pageIndex, (newVal: number) => { |
| 59 | + emit('update:modelValue', newVal) |
| 60 | + formattedPageIndex.value = formatPageIndex(pageIndex.value, count) |
| 61 | + }) |
| 62 | + |
| 63 | + watch(formattedPageIndex, (newVal: number) => { |
| 64 | + pageIndex.value = newVal |
| 65 | + }) |
| 66 | + |
| 67 | + return () => { |
| 68 | + return ( |
| 69 | + <div class="devui-carousel"> |
| 70 | + <div |
| 71 | + class="devui-carousel-item-container" |
| 72 | + style={{ |
| 73 | + width: count * 100 + '%', |
| 74 | + left: -(formattedPageIndex.value - 1) * 100 + '%', |
| 75 | + }} |
| 76 | + > |
| 77 | + {renderSlot(useSlots(), 'default')} |
| 78 | + </div> |
| 79 | + { |
| 80 | + slots.pagination |
| 81 | + ? renderSlot(useSlots(), 'pagination', { |
| 82 | + prevPage, nextPage |
| 83 | + }) : <> |
| 84 | + <DCarouselPrev onClick={() => { |
| 85 | + emit('update:modelValue', props.modelValue-1) |
| 86 | + prevPage() |
| 87 | + }} /> |
| 88 | + <DCarouselNext onClick={() => { |
| 89 | + emit('update:modelValue', props.modelValue+1) |
| 90 | + nextPage() |
| 91 | + }} /> |
| 92 | + </> |
| 93 | + } |
| 94 | + {slots.indicator ? ( |
| 95 | + slots.indicator({ |
| 96 | + count, |
| 97 | + pageIndex: formattedPageIndex.value, |
| 98 | + setPageIndex |
| 99 | + }) |
| 100 | + ) : ( |
| 101 | + <DCarouselIndicator |
| 102 | + count={count} |
| 103 | + v-model={formattedPageIndex.value} |
| 104 | + ></DCarouselIndicator> |
| 105 | + )} |
| 106 | + </div> |
| 107 | + ) |
| 108 | + } |
| 109 | + }, |
| 110 | +}) |
0 commit comments