Skip to content

Commit f22ebf8

Browse files
committed
feat: Improved adapSwiper
1 parent 98ff2e3 commit f22ebf8

File tree

2 files changed

+107
-14
lines changed

2 files changed

+107
-14
lines changed

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,74 @@ export default class MyComponent extends Vue {
113113
}
114114
}
115115
```
116+
117+
## Usage AdapSwiper
118+
AdapSwiper uses [vue-awesome-swiper](https://github.com/surmon-china/vue-awesome-swiper).
119+
All vue-awesome-swiper props and events are inherited to adap-swiper. See [docs](https://github.surmon.me/vue-awesome-swiper/) to learn more
120+
121+
```html
122+
<adap-swiper
123+
ref="adapSwiper"
124+
spinner="customSpinner"
125+
slideClass="custom-slide-class"
126+
:collection="collection"
127+
@init="updateEvent"
128+
@slideChange="updateEvent"
129+
@resize="updateEvent"
130+
@expand="updateEvent"
131+
>
132+
<template slot="header">
133+
<!--Custom arrow swiper example-->
134+
<a v-if="!isBeginning" @click="prevSlide()" >Go Left</a>
135+
<a v-if="!isEnd" @click="nextSlide()" >Go Right</a>
136+
</template>
137+
138+
<template slot="slide" slot-scope="props">
139+
<!--example using tailwind-->
140+
<div class="p-4 w-full h-80" :key="props.i">
141+
<div>
142+
{{ props.item.$tag }}
143+
</div>
144+
</div>
145+
</template>
146+
147+
<template slot="empty">
148+
Empty list
149+
</template>
150+
</adap-swiper>
151+
```
152+
On Code:
153+
```typescript
154+
import Swiper from 'swiper'
155+
156+
@Component
157+
export default class MyComponent extends Vue {
158+
collection = new MyCollection()
159+
160+
isBeginning = false
161+
isEnd = false
162+
163+
async created() {
164+
await this.$await.run('customSpinner', () => this.collection.expand())
165+
}
166+
167+
updateEvent(swiper: Swiper) {
168+
this.isBeginning = swiper.isBeginning
169+
this.isEnd = swiper.isEnd
170+
}
171+
172+
nextSlide() {
173+
const component = this.$refs.adapSwiper?.swiperComponent as any
174+
if (component) {
175+
component.$swiper?.slideNext()
176+
}
177+
}
178+
179+
prevSlide() {
180+
const component = this.$refs.adapSwiper?.swiperComponent as any
181+
if (component) {
182+
component.$swiper?.slidePrev()
183+
}
184+
}
185+
}
186+
```

src/AdapSwiper.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ const template = `
77
<div class="adap-swiper__content">
88
<swiper
99
ref="swiperComponent"
10-
class="swiper-container--sm-edge-preview"
10+
v-bind="vBind"
11+
v-on="vOn"
1112
:options="options"
13+
:cleanupStylesOnDestroy="cleanupStylesOnDestroy"
1214
@slideChange="pageChangeEvent"
15+
class="swiper-container--sm-edge-preview"
1316
>
1417
<swiper-slide
1518
v-for="(item, i) in slides"
@@ -35,7 +38,7 @@ const template = `
3538

3639
import { Component, Prop, Watch, Mixins } from 'vue-property-decorator'
3740
import { ExpansibleCollection, IResource } from '@simpli/resource-collection'
38-
import { SwiperOptions } from 'swiper'
41+
import Swiper, { SwiperOptions } from 'swiper'
3942
import { MixinAdapScreenSize } from './MixinAdapScreenSize'
4043
// @ts-ignore
4144
import { SwiperComponent } from 'vue-awesome-swiper'
@@ -45,10 +48,16 @@ import AdapTableWrapper from './index'
4548
export class AdapSwiper extends Mixins(MixinAdapScreenSize) {
4649
@Prop({ type: Object, required: true })
4750
collection!: ExpansibleCollection<IResource>
51+
4852
@Prop({ type: String })
4953
slideClass?: string
54+
5055
@Prop({ type: String, default: 'list' })
5156
spinner!: string
57+
58+
@Prop({ type: Boolean, default: false })
59+
cleanupStylesOnDestroy?: boolean
60+
5261
@Prop({ type: Object })
5362
options?: SwiperOptions
5463

@@ -61,6 +70,17 @@ export class AdapSwiper extends Mixins(MixinAdapScreenSize) {
6170
MEDIUM_SCREEN = AdapTableWrapper.screenMedium
6271
LARGE_SCREEN = AdapTableWrapper.screenLarge
6372

73+
get vBind() {
74+
return { ...this.$attrs }
75+
}
76+
77+
get vOn() {
78+
const listeners = { ...this.$listeners }
79+
delete listeners.input
80+
delete listeners.slideChange
81+
return listeners
82+
}
83+
6484
/**
6585
* In order to clean the cache, the collection empty its content and then repopulate it.
6686
* To prevent malfunction of swiper, this watch does not accept empty list
@@ -70,17 +90,6 @@ export class AdapSwiper extends Mixins(MixinAdapScreenSize) {
7090
async itemsEvent(items: IResource[]) {
7191
if (items && items.length) {
7292
this.slides = items
73-
74-
await this.$nextTick()
75-
76-
// https://github.com/surmon-china/vue-awesome-swiper/issues/317
77-
const component = this.$refs.swiperComponent as SwiperComponent
78-
if (component) {
79-
component.swiper.destroy = () => {
80-
// Não executar a função nativa pois existe um bug de layout
81-
// A falta do destroy não interfere na performace pois o VUE já cuida do lifecycle
82-
}
83-
}
8493
}
8594
}
8695

@@ -123,6 +132,13 @@ export class AdapSwiper extends Mixins(MixinAdapScreenSize) {
123132
this.collection.perPage = 4 * this.perPage
124133
}
125134

135+
mounted() {
136+
const component = this.$refs.swiperComponent as SwiperComponent
137+
if (component) {
138+
this.$emit('init', component.$swiper)
139+
}
140+
}
141+
126142
nextSlide() {
127143
const component = this.$refs.swiperComponent as SwiperComponent
128144
if (component) {
@@ -137,7 +153,9 @@ export class AdapSwiper extends Mixins(MixinAdapScreenSize) {
137153
}
138154
}
139155

140-
async pageChangeEvent() {
156+
async pageChangeEvent(args: Swiper) {
157+
this.$emit('slideChange', args)
158+
141159
this.isBeginning = Boolean(
142160
this.swiperComponent && this.swiperComponent.swiper && this.swiperComponent.swiper.isBeginning
143161
)
@@ -149,6 +167,8 @@ export class AdapSwiper extends Mixins(MixinAdapScreenSize) {
149167
await this.expand()
150168
this.isEnd = false
151169
}
170+
171+
this.$emit('slideChangeAfterExpand', args)
152172
}
153173

154174
async expand() {
@@ -161,5 +181,7 @@ export class AdapSwiper extends Mixins(MixinAdapScreenSize) {
161181
await this.collection.expand()
162182
}
163183
this.locked = false
184+
185+
this.$emit('expand')
164186
}
165187
}

0 commit comments

Comments
 (0)