Skip to content

Commit 288fcc9

Browse files
committed
feat: CCollapse: add possibility of different duration of show and hide
1 parent ed0d5fa commit 288fcc9

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/components/collapse/CCollapse.vue

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<script>
88
const props = {
99
duration: {
10-
type: Number,
10+
type: [Number, Object],
1111
default: 400
1212
},
1313
transition: {
@@ -34,7 +34,7 @@ export default {
3434
this.visible = val
3535
},
3636
visible (val) {
37-
if (this.duration) {
37+
if (this.toggleTime) {
3838
this.collapseController(val)
3939
} else {
4040
this.reset()
@@ -47,17 +47,23 @@ export default {
4747
beforeDestroy () {
4848
clearTimeout(this.heightWatcher)
4949
},
50+
computed: {
51+
toggleTime () {
52+
return (this.visible ? this.duration.show : this.duration.hide) || this.duration
53+
}
54+
},
5055
methods: {
5156
collapseController (val) {
5257
if (this.collapsing === false) {
5358
val ? this.toggle(true) : this.toggle(false)
54-
this.setFinishTimer(this.duration)
59+
this.setFinishTimer(this.toggleTime)
5560
} else {
61+
this.setTransition()
5662
this.turn()
5763
const height = Number(this.collapsing.slice(0,-2))
5864
const current = this.$el.offsetHeight
5965
const time = (val ? height - current : current) / height
60-
this.setFinishTimer(this.duration * time)
66+
this.setFinishTimer(this.toggleTime * time)
6167
}
6268
},
6369
turn () {
@@ -72,10 +78,13 @@ export default {
7278
this.collapsing = this.$el.scrollHeight + 'px'
7379
this.$el.style.height = val ? 0 : this.$el.scrollHeight + 'px'
7480
this.$el.style.overflow = 'hidden'
75-
this.$el.style.transition = `all ${this.duration}ms ${this.transition}`
81+
this.setTransition()
7682
const self = this
7783
setTimeout(() => { self.$el.style.height = val ? this.collapsing : 0 }, 0)
7884
},
85+
setTransition () {
86+
this.$el.style.transition = `all ${this.toggleTime}ms ${this.transition}`
87+
},
7988
setFinishTimer (duration) {
8089
clearTimeout(this.heightWatcher)
8190
this.heightWatcher = setTimeout(() => this.reset(), duration)

src/components/collapse/test/CCollapse.spec.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const customWrapper = mount(CCollapse, {
1919
const togglerWrapper = mount(CCollapse, {
2020
attachToDocument: true,
2121
propsData: {
22-
duration: 500,
22+
duration: 0,
2323
show: true,
2424
navbar: true
2525
},
@@ -72,4 +72,10 @@ describe(ComponentName, () => {
7272
}
7373
expect(typeof errors).toBe('undefined')
7474
})
75+
it('do not set timer, but reset collapse when duration is set to 0', () => {
76+
const spy = jest.spyOn(togglerWrapper.vm, 'setFinishTimer')
77+
togglerWrapper.setProps({show: false})
78+
expect(spy).not.toBeCalled()
79+
expect(togglerWrapper.emitted().finish).toBeTruthy()
80+
})
7581
})

src/components/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export declare class CCarouselItem extends Vue {
116116
}
117117

118118
export declare class CCollapse extends Vue {
119-
duration: number
119+
duration: number | { show: number, hide: number }
120120
transition: string
121121
show: boolean
122122
navbar: boolean

0 commit comments

Comments
 (0)