mx-count-down.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <template>
  2. <uv-button v-bind="binding" :disabled="counting" @click="handleClick">
  3. <template v-if="counting" #default>
  4. <uv-count-down ref="timer" :time="time" :auto-start="false" :format="format" @finish="counting=false"/>
  5. </template>
  6. </uv-button>
  7. </template>
  8. <script>
  9. import {nextTick, ref} from 'vue'
  10. import {buttonProps} from "@/uni_modules/uv-button/components/uv-button/uv-button.vue";
  11. import {blockRunner, createPropDefine} from "@/utils";
  12. export default {
  13. name: "mx-count-down",
  14. props: {
  15. ...buttonProps,
  16. time: createPropDefine(60 * 1000, Number),
  17. format: createPropDefine('ss'),
  18. type: createPropDefine('text'),
  19. plain: createPropDefine(true, Boolean),
  20. customStyle: createPropDefine({
  21. width: '40px',
  22. height: '32px',
  23. color: 'var(--primary-deep-color)'
  24. }, [Object, String]),
  25. validateBlock: createPropDefine(null, Function)
  26. },
  27. emits: ['click'],
  28. setup(props, {attrs, emit}) {
  29. const binding = {
  30. ...props,
  31. ...attrs
  32. }
  33. const counting = ref(false)
  34. const timer = ref(null)
  35. const handleClick = async function () {
  36. await blockRunner(props.validateBlock)
  37. if (counting.value) return
  38. counting.value = true // NOTE:这里手工控制,让template#default渲染出来timer才能挂钩
  39. emit('click')
  40. await nextTick() // NOTE: 等待template#default渲染之后
  41. timer.value.start()
  42. }
  43. return {
  44. binding,
  45. timer,
  46. counting,
  47. handleClick
  48. }
  49. }
  50. }
  51. </script>
  52. <style scoped lang="scss">
  53. ::v-deep .uv-count-down__text {
  54. color: var(--primary-color);
  55. }
  56. </style>