fix(carousel): fix carousel don't update the indicator and items when data change(#1244)

* fix(carousel): add methods of remove item

* fix: if delete item is current item then go next

Co-authored-by: Ryan2128 <33176053+Ryan2128@users.noreply.github.com>
This commit is contained in:
BeADre 2021-01-24 18:19:04 +08:00 committed by GitHub
parent 80de9fc5e1
commit 59422d7fab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 7 deletions

View File

@ -43,6 +43,7 @@ export interface InjectCarouselScope {
type: string
items: Ref<UnwrapRef<CarouselItem[]>>
loop: boolean
updateItems: (item: CarouselItem) => void
addItem: (item: CarouselItem) => void
removeItem: (uid: number) => void
setActiveItem: (index: number) => void
}

View File

@ -29,6 +29,7 @@ import {
computed,
toRefs,
getCurrentInstance,
onUnmounted,
} from 'vue'
import {
autoprefixer,
@ -158,8 +159,8 @@ export default defineComponent({
// lifecycle
onMounted(() => {
if (injectCarouselScope.updateItems) {
injectCarouselScope.updateItems({
if (injectCarouselScope.addItem) {
injectCarouselScope.addItem({
uid: instance.uid,
...props,
...toRefs(data),
@ -168,13 +169,17 @@ export default defineComponent({
}
})
onUnmounted(() => {
if (injectCarouselScope.removeItem) {
injectCarouselScope.removeItem(instance.uid)
}
})
return {
data,
itemStyle,
translateItem,
type: injectCarouselScope.type,
handleItemClick,
}
},

View File

@ -236,10 +236,18 @@ export default defineComponent({
})
}
function updateItems(item) {
function addItem(item) {
items.value.push(item)
}
function removeItem(uid) {
const index = items.value.findIndex(item => item.uid === uid)
if (index !== -1) {
items.value.splice(index, 1)
if(data.activeIndex === index) next()
}
}
function itemInStage(item, index) {
const length = items.value.length
if (
@ -358,7 +366,8 @@ export default defineComponent({
type: props.type,
items,
loop: props.loop,
updateItems,
addItem,
removeItem,
setActiveItem,
})