xnzt/xnzt-h5/components/count-down-button/count-down-button.vue
2024-11-11 10:27:27 +08:00

218 lines
4.1 KiB
Vue

<!--<template>
<button
class="count-down-button"
@click="handleClick"
:disabled="buttonDisabled || loading"
:autofocus="autofocus"
:type="nativeType"
:class="[
type ? 'el-button--' + type : '',
buttonSize ? 'el-button--' + buttonSize : '',
{
'is-disabled': buttonDisabled,
'is-loading': loading,
'is-plain': plain,
'is-round': round,
'is-circle': circle
}
]"
>
<i class="el-icon-loading" v-if="loading"></i>
<i :class="icon" v-if="icon && !loading"></i>
<span v-if="$slots.default"><slot></slot></span>
</button>
</template>
<script>
export default {
name: 'countDownButton',
inject: {
elForm: {
default: ''
},
elFormItem: {
default: ''
}
},
props: {
type: {
type: String,
default: 'default'
},
size: String,
icon: {
type: String,
default: ''
},
nativeType: {
type: String,
default: 'button'
},
loading: {
type: Boolean,
default: false,
},
ghost: {
type: Boolean,
default: false,
},
disabled: {
type: Boolean,
default: false,
},
icon: String,
shape: String,
long: {
type: Boolean,
default: false,
},
loading: Boolean,
disabled: Boolean,
plain: Boolean,
autofocus: Boolean,
round: Boolean,
circle: Boolean
},
computed: {
_elFormItemSize() {
return (this.elFormItem || {}).elFormItemSize;
},
buttonSize() {
return this.size || this._elFormItemSize || (this.$ELEMENT || {}).size;
},
buttonDisabled() {
return this.$options.propsData.hasOwnProperty('disabled') ? this.disabled : (this.elForm || {}).disabled;
}
},
methods: {
handleClick(evt) {
this.$emit('click', evt);
}
}
};
</script>
<style lang="scss" scoped>
</style>-->
<template>
<div>
<button
class="count-down-button"
:countTime="countTime"
:loading="loading"
:type="type"
:size="size"
:ghost="ghost"
:disabled="disabled || clicked"
:icon="icon"
:shape="shape"
:long="long"
@click="handleClick"
><text>{{ buttonText }}</text></button
>
</div>
</template>
<script>
export default {
// name: "iconChoose",
name: "countDownButton",
props: {
text: {
type: String,
default: "提交",
},
autoCountDown: {
type: Boolean,
default: true,
},
countTime: {
type: [Number, String],
default: 5,
},
suffixText: {
type: String,
default: "秒后重试",
},
type: String,
size: String,
loading: {
type: Boolean,
default: false,
},
ghost: {
type: Boolean,
default: false,
},
disabled: {
type: Boolean,
default: false,
},
icon: String,
shape: String,
long: {
type: Boolean,
default: false,
},
},
data() {
return {
buttonText: this.text,
count: Number(this.countTime),
clicked: false,
};
},
methods: {
init() {},
handleClick() {
if (this.autoCountDown) {
this.clicked = true;
this.countDown();
}
this.$emit("click", true);
},
startCountDown() {
this.clicked = true;
this.countDown();
},
countDown() {
if (this.count == 0) {
this.clicked = false;
this.count = this.countTime;
this.buttonText = this.text;
return;
} else {
this.buttonText = this.count + " " + this.suffixText;
this.count--;
}
setTimeout(() => {
this.countDown();
}, 1000);
},
setText(value) {
if (value === this.buttonText) {
return;
}
this.buttonText = value;
},
},
watch: {
text(val) {
this.setText(val);
},
},
mounted() {
this.init();
},
};
</script>
<style lang="scss" scoped>
// .count-down-button{
// background-color: black;
// }
</style>