CountDown 倒计时

注:

  • 本组件没有指定样式,默认情况下可对文字样式进行自定义。
import {
  CountDownOnEnd,
  CountDownOnPause,
  CountDownOnResume,
  showToast,
  TaGroup,
  TaCell,
  TaCountDown,
  TaButton,
  CountDownRef
} from '@/index'
import { useRef, useState } from 'react'

export default function ExpCountDown() {
  const ref = useRef<CountDownRef>(null)
  const [paused, setPaused] = useState(false)

  const onPause: CountDownOnPause = e => {
    console.log('pause', e)
    setPaused(true)
    showToast('已暂停')
  }

  const onResume: CountDownOnResume = e => {
    console.log('resume', e)
    setPaused(false)
    showToast('继续计时')
  }

  const onEnd: CountDownOnEnd = e => {
    console.log('end', e)
    showToast('计时结束')
  }

  function onPauseOrResume() {
    if (paused) {
      ref.current?.resume()
    } else {
      ref.current?.pause()
    }
  }

  function onReset() {
    ref.current?.reset(100 * 1000)
  }

  return (
    <>
      <TaGroup title="基础用法">
        <TaCell label="默认" className="exp-countDown-box">
          <TaCountDown initialTiming={300 * 1000} />
        </TaCell>
        <TaCell label="显示天数" className="exp-countDown-box">
          <TaCountDown initialTiming={1.5 * 24 * 3600 * 1000} showDays />
        </TaCell>
      </TaGroup>
      <TaGroup title="Slot render">
        <TaCell label="中文显示" className="exp-countDown-box">
          <TaCountDown
            initialTiming={300 * 1000}
            render={countDown =>
              `${countDown.fullHours}${countDown.minutes}${countDown.seconds}`
            }
          />
        </TaCell>
        <TaCell label="毫秒" className="exp-countDown-box">
          <TaCountDown
            initialTiming={300 * 1000}
            render={countDown =>
              `${countDown.fullHours}:${countDown.minutes}:${countDown.seconds}.${countDown.milliseconds}`
            }
          ></TaCountDown>
        </TaCell>
        <TaCell label="自定义风格" className="exp-countDown-box">
          <TaCountDown
            initialTiming={300 * 1000}
            render={countDown => (
              <>
                <span className="exp-countDown-time-item">
                  {countDown.fullHours}
                </span>
                <span className="exp-countDown-time-item">
                  {countDown.minutes}
                </span>
                <span className="exp-countDown-time-item">
                  {countDown.seconds}
                </span>
              </>
            )}
          ></TaCountDown>
        </TaCell>
      </TaGroup>
      <TaGroup title="时间监听">
        <TaCell label="onPause/onResume/onEnd" className="exp-countDown-box">
          <div className="exp-countDown-time-r">
            <TaCountDown
              initialTiming={100 * 1000}
              onPause={onPause}
              onResume={onResume}
              onEnd={onEnd}
              ref={ref}
            />
          </div>
          <TaButton onClick={onPauseOrResume} size="small">
            {paused ? '恢复' : '暂停'}
          </TaButton>
          <TaButton onClick={onReset} size="small" type="danger">
            重置
          </TaButton>
        </TaCell>
      </TaGroup>
    </>
  )
}

Import

import { TaCountDown } from 'tantalum-ui-mobile-react'

具体的引入方式可以参考引入组件

Import Type

组件导出的类型定义:

import type {
  CountTime,
  CountDownOnEnd,
  CountDownOnPause,
  CountDownOnResume,
  CountDownRef
} from 'tantalum-ui-mobile-react'

Props

属性类型默认值必填说明
initialTimingnumber99初始倒计时时长,单位 ms
showDaysbooleanfalse是否显示天数

Events

事件描述回调函数参数TypeScript 函数
onPause计时暂停时触发payload: { remainTime: number } remainTime 剩余时间,单位 msCountDownOnPause
onResume恢复计时时触发payload: { remainTime: number } remainTime 剩余时间,单位 msCountDownOnResume
onEnd计时结束时触发payload: { startTime: number, endTime: number } startTime 本地开始时间戳,endTime 本地结束时间戳,单位 msCountDownOnEnd

Slots

render(countTime)

<TaCountDown
  initialTiming={300 * 1000}
  render={countTime =>
    `${countTime.fullHours}${countTime.minutes}${countTime.seconds}`
  }
/>

countTime 的结构

字段名类型说明
timenumber持续时间
daysstring天数
hoursstring小时数(<24),需要跟 days 配合,保留 2 位
fullHoursstring小时数,含天数综合,如 '124',至少保留 2 位
thousandsFullHoursstring千分位形式的小时数,含天数综合,如 '1,234'
minutesstring分钟数,保留 2 位
secondsstring秒钟数,保留 2 位
millisecondsstring毫秒数,保留 3 位

Methods

方法名说明参数
pause暂停倒计时
resume恢复倒计时
reset重置倒计时( timing: number, autoStart?: boolean ) 单位 ms