mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-06 03:58:05 +08:00
feat(CountDown): update slot (#2143)
This commit is contained in:
parent
95476ae754
commit
b9f52933ff
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
<demo-block title="自定义样式">
|
<demo-block title="自定义样式">
|
||||||
<van-count-down
|
<van-count-down
|
||||||
useCustom
|
use-slot
|
||||||
time="{{ time }}"
|
time="{{ time }}"
|
||||||
bind:change="onChange"
|
bind:change="onChange"
|
||||||
>
|
>
|
||||||
|
@ -12,4 +12,3 @@
|
|||||||
background-color: #1989fa;
|
background-color: #1989fa;
|
||||||
border-radius: 2px;
|
border-radius: 2px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,11 +53,11 @@ Page({
|
|||||||
|
|
||||||
### 自定义样式
|
### 自定义样式
|
||||||
|
|
||||||
通过`bind:change`事件获取`timeData`对象,格式见下方表格
|
设置`use-slot`属性后可以自定义倒计时样式,需要通过`bind:change`事件获取`timeData`对象并自行渲染,格式见下方表格
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<van-count-down
|
<van-count-down
|
||||||
useCustom
|
use-slot
|
||||||
time="{{ time }}"
|
time="{{ time }}"
|
||||||
bind:change="onChange"
|
bind:change="onChange"
|
||||||
>
|
>
|
||||||
@ -150,14 +150,14 @@ Page({
|
|||||||
| format | 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒 | *string* | `HH:mm:ss` | - |
|
| format | 时间格式,DD-日,HH-时,mm-分,ss-秒,SSS-毫秒 | *string* | `HH:mm:ss` | - |
|
||||||
| auto-start | 是否自动开始倒计时 | *boolean* | `true` | - |
|
| auto-start | 是否自动开始倒计时 | *boolean* | `true` | - |
|
||||||
| millisecond | 是否开启毫秒级渲染 | *boolean* | `false` | - |
|
| millisecond | 是否开启毫秒级渲染 | *boolean* | `false` | - |
|
||||||
| useCustom | 是否自定义样式 | *boolean* | `false` | - |
|
| use-slot | 是否使用自定义样式插槽 | *boolean* | `false` | - |
|
||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
|
||||||
| 事件名 | 说明 | 回调参数 |
|
| 事件名 | 说明 | 回调参数 |
|
||||||
|------|------|------|
|
|------|------|------|
|
||||||
| change | 时间变化时触发 | timeData |
|
|
||||||
| finish | 倒计时结束时触发 | - |
|
| finish | 倒计时结束时触发 | - |
|
||||||
|
| change | 时间变化时触发,仅在开启`use-slot`后才会触发 | timeData |
|
||||||
|
|
||||||
### timeData 格式
|
### timeData 格式
|
||||||
|
|
||||||
|
@ -1,28 +1,26 @@
|
|||||||
import { VantComponent } from '../common/component';
|
import { VantComponent } from '../common/component';
|
||||||
import { isSameSecond, parseFormat, parseTimeData } from './utils';
|
import { isSameSecond, parseFormat, parseTimeData } from './utils';
|
||||||
|
|
||||||
|
function simpleTick(fn: Function) {
|
||||||
|
return setTimeout(fn, 30);
|
||||||
|
}
|
||||||
|
|
||||||
VantComponent({
|
VantComponent({
|
||||||
props: {
|
props: {
|
||||||
|
useSlot: Boolean,
|
||||||
|
millisecond: Boolean,
|
||||||
time: {
|
time: {
|
||||||
type: Number,
|
type: Number,
|
||||||
observer: 'reset',
|
observer: 'reset'
|
||||||
},
|
},
|
||||||
format: {
|
format: {
|
||||||
type: String,
|
type: String,
|
||||||
value: 'HH:mm:ss'
|
value: 'HH:mm:ss'
|
||||||
},
|
},
|
||||||
useCustom: {
|
|
||||||
type: Boolean,
|
|
||||||
value: false,
|
|
||||||
},
|
|
||||||
autoStart: {
|
autoStart: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
value: true,
|
value: true
|
||||||
},
|
}
|
||||||
millisecond: {
|
|
||||||
type: Boolean,
|
|
||||||
value: false,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
|
|
||||||
data: {
|
data: {
|
||||||
@ -68,17 +66,17 @@ VantComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
microTick() {
|
microTick() {
|
||||||
this.tid = setTimeout(() => {
|
this.tid = simpleTick(() => {
|
||||||
this.setRemain(this.getRemain());
|
this.setRemain(this.getRemain());
|
||||||
|
|
||||||
if (this.remain !== 0) {
|
if (this.remain !== 0) {
|
||||||
this.microTick();
|
this.microTick();
|
||||||
}
|
}
|
||||||
}, 100);
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
macroTick() {
|
macroTick() {
|
||||||
this.tid = setTimeout(() => {
|
this.tid = simpleTick(() => {
|
||||||
const remain = this.getRemain();
|
const remain = this.getRemain();
|
||||||
|
|
||||||
if (!isSameSecond(remain, this.remain) || remain === 0) {
|
if (!isSameSecond(remain, this.remain) || remain === 0) {
|
||||||
@ -88,7 +86,7 @@ VantComponent({
|
|||||||
if (this.remain !== 0) {
|
if (this.remain !== 0) {
|
||||||
this.macroTick();
|
this.macroTick();
|
||||||
}
|
}
|
||||||
}, 1000);
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
getRemain() {
|
getRemain() {
|
||||||
@ -98,7 +96,11 @@ VantComponent({
|
|||||||
setRemain(remain) {
|
setRemain(remain) {
|
||||||
this.remain = remain;
|
this.remain = remain;
|
||||||
const timeData = parseTimeData(remain);
|
const timeData = parseTimeData(remain);
|
||||||
this.$emit('change', timeData);
|
|
||||||
|
if (this.data.useSlot) {
|
||||||
|
this.$emit('change', timeData);
|
||||||
|
}
|
||||||
|
|
||||||
this.setData({
|
this.setData({
|
||||||
formattedTime: parseFormat(this.data.format, timeData)
|
formattedTime: parseFormat(this.data.format, timeData)
|
||||||
});
|
});
|
||||||
|
@ -1,13 +1,4 @@
|
|||||||
<view
|
<view class="van-count-down">
|
||||||
wx:if="{{ useCustom }}"
|
<slot wx:if="{{ useSlot }}"/>
|
||||||
class="van-count-down"
|
<block wx:else>{{ formattedTime }}</block>
|
||||||
>
|
|
||||||
<slot/>
|
|
||||||
</view>
|
|
||||||
|
|
||||||
<view
|
|
||||||
wx:else
|
|
||||||
class="van-count-down"
|
|
||||||
>
|
|
||||||
{{ formattedTime }}
|
|
||||||
</view>
|
</view>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user