[Improvement] PullRefresh: add refersh event (#625)

This commit is contained in:
neverland 2018-02-08 10:38:34 +08:00 committed by GitHub
parent c2773b49a6
commit 6ed43f8c75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 42 deletions

View File

@ -1,5 +1,5 @@
<template> <template>
<van-pull-refresh v-model="isLoading"> <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
<demo-section name="pull-refresh" background="#fff"> <demo-section name="pull-refresh" background="#fff">
<demo-block :title="$t('basicUsage')"> <demo-block :title="$t('basicUsage')">
<p>{{ $t('text') }}: {{ count }}</p> <p>{{ $t('text') }}: {{ count }}</p>
@ -12,10 +12,12 @@
export default { export default {
i18n: { i18n: {
'zh-CN': { 'zh-CN': {
text: '刷新次数' text: '刷新次数',
success: '刷新成功'
}, },
'en-US': { 'en-US': {
text: 'Refresh Count' text: 'Refresh Count',
success: 'Refresh success'
} }
}, },
@ -26,15 +28,13 @@ export default {
}; };
}, },
watch: { methods: {
isLoading() { onRefresh() {
if (this.isLoading) { setTimeout(() => {
setTimeout(() => { Toast(this.$t('success'));
Toast('刷新成功'); this.isLoading = false;
this.isLoading = false; this.count++;
this.count++; }, 500);
}, 500);
}
} }
} }
}; };

View File

@ -9,14 +9,11 @@ Vue.use(PullRefresh);
### Usage ### Usage
#### Basic Usage
The `refresh` event will be triggered when pull refresh, you should set `v-model` to `false` to reset loading status after process refresh event.
```html ```html
<!-- use v-model to control loading status --> <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
<van-pull-refresh
v-model="isLoading"
pulling-text="Pull to refresh..."
loosing-text="Loose to refresh..."
loading-text="Loading..."
>
<p>Refresh Count: {{ count }}</p> <p>Refresh Count: {{ count }}</p>
</van-pull-refresh> </van-pull-refresh>
``` ```
@ -30,15 +27,13 @@ export default {
} }
}, },
watch: { methods: {
isLoading() { onRefresh() {
if (this.isLoading) { setTimeout(() => {
setTimeout(() => { this.$toast('Refresh Success');
Toast('Refresh Success'); this.isLoading = false;
this.isLoading = false; this.count++;
this.count++; }, 500);
}, 500);
}
} }
} }
} }
@ -49,12 +44,19 @@ export default {
| Attribute | Description | Type | Default | Accepted Values | | Attribute | Description | Type | Default | Accepted Values |
|-----------|-----------|-----------|-------------|-------------| |-----------|-----------|-----------|-------------|-------------|
| v-model | Loading status | `Boolean` | - | - | | v-model | Loading status | `Boolean` | - | - |
| pulling-text | Text to show when pulling | `String` | `下拉即可刷新...` | - | | pulling-text | Text to show when pulling | `String` | `Pull to refresh...` | - |
| loosing-text | Text to show when loosing | `String` | `释放即可刷新...` | - | | loosing-text | Text to show when loosing | `String` | `Loose to refresh...` | - |
| loading-text | Text to show when loading | `String` | `加载中...` | - | | loading-text | Text to show when loading | `String` | `Loading...` | - |
| animation-duration | Animation duration | `Number` | `300` | - | | animation-duration | Animation duration | `Number` | `300` | - |
| head-height | Height of head | `Number` | `50` | - | | head-height | Height of head | `Number` | `50` | - |
### Event
| Event | Description | Parameters |
|-----------|-----------|-----------|
| refresh | Triggered when pull refresh | - |
### Slot ### Slot
| name | Description | | name | Description |

View File

@ -9,9 +9,11 @@ Vue.use(PullRefresh);
### 代码演示 ### 代码演示
#### 基础用法
下拉刷新时会触发 `refresh` 事件,在事件的回调函数中可以进行同步或异步操作,操作完成后将 `v-model` 设置为 `false`,表示加载完成。
```html ```html
<!-- 通过 v-model 控制加载状态 --> <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
<van-pull-refresh v-model="isLoading">
<p>刷新次数: {{ count }}</p> <p>刷新次数: {{ count }}</p>
</van-pull-refresh> </van-pull-refresh>
``` ```
@ -25,15 +27,13 @@ export default {
} }
}, },
watch: { methods: {
isLoading() { onRefresh() {
if (this.isLoading) { setTimeout(() => {
setTimeout(() => { this.$toast('刷新成功');
Toast('刷新成功'); this.isLoading = false;
this.isLoading = false; this.count++;
this.count++; }, 500);
}, 500);
}
} }
} }
} }
@ -50,6 +50,12 @@ export default {
| animation-duration | 动画时长 | `Number` | `300` | - | | animation-duration | 动画时长 | `Number` | `300` | - |
| head-height | 顶部内容高度 | `Number` | `50` | - | | head-height | 顶部内容高度 | `Number` | `50` | - |
### Event
| 事件名 | 说明 | 参数 |
|-----------|-----------|-----------|
| refresh | 下拉刷新时触发 | - |
### Slot ### Slot
| 名称 | 说明 | | 名称 | 说明 |

View File

@ -127,6 +127,7 @@ export default create({
if (this.status === 'loosing') { if (this.status === 'loosing') {
this.getStatus(this.headHeight, true); this.getStatus(this.headHeight, true);
this.$emit('input', true); this.$emit('input', true);
this.$emit('refresh');
} else { } else {
this.getStatus(0); this.getStatus(0);
} }

View File

@ -56,11 +56,15 @@ describe('PullRefresh', () => {
} }
}); });
const refreshSpy = sinon.spy();
wrapper.vm.$on('refresh', refreshSpy);
wrapper.vm.$on('input', value => { wrapper.vm.$on('input', value => {
wrapper.vm.value = value; wrapper.vm.value = value;
setTimeout(() => { setTimeout(() => {
wrapper.vm.value = false; wrapper.vm.value = false;
expect(refreshSpy.calledOnce).to.be.true;
setTimeout(() => { setTimeout(() => {
expect(wrapper.vm.status).to.equal('normal'); expect(wrapper.vm.status).to.equal('normal');
done(); done();