Stopwatch 秒表

注:

  • 本组件没有指定样式,默认情况下可对文字样式进行自定义。
import {
  CountTime,
  TaStopwatch,
  TaGroup,
  TaCell,
  TaButton,
  StopwatchOnStop,
  StopwatchRef
} from '@/index'
import { useRef, useState } from 'react'

export default function ExpStopwatch() {
  const [paused, setPaused] = useState(true)
  const [laps, setLaps] = useState<string[]>([])
  const stopWatchRef = useRef<StopwatchRef>(null)

  const updateLaps = (_laps: CountTime[] = []) => {
    setLaps(
      _laps.reverse().map(countTime => {
        return `${
          parseInt(countTime.fullHours) > 0
            ? countTime.thousandsFullHours + ':'
            : ''
        }${countTime.minutes}:${countTime.seconds}.${countTime.milliseconds}`
      })
    )
  }

  const resetOrLap = () => {
    if (paused) {
      stopWatchRef.current?.reset()
      updateLaps([])
    } else {
      updateLaps(stopWatchRef.current?.lap() ?? [])
    }
  }

  const startOrStop = () => {
    if (paused) {
      stopWatchRef.current?.start()
    } else {
      stopWatchRef.current?.stop()
    }
  }

  const onStop: StopwatchOnStop = e => {
    setPaused(true)

    console.log('stop', e)

    updateLaps(e.laps)
  }

  const onStart = () => {
    setPaused(false)
  }

  const onReset = () => {
    setPaused(true)
  }

  function renderLaps() {
    return laps.map((item, index) => (
      <TaCell label={'计次 ' + (laps.length - index)} key={item}>
        {item}
      </TaCell>
    ))
  }
  return (
    <>
      <TaGroup title="基础用法">
        <div className="exp-stopwatch-box">
          <div className="exp-stopwatch-box-header">
            <TaStopwatch
              onStop={onStop}
              onStart={onStart}
              onReset={onReset}
              ref={stopWatchRef}
            />
          </div>
          <div className="exp-stopwatch-box-body">
            <TaButton onClick={resetOrLap}>{paused ? '重置' : '计次'}</TaButton>
            <TaButton
              onClick={startOrStop}
              type={!paused ? 'danger' : 'success'}
            >
              {paused ? '启动' : '停止'}
            </TaButton>
          </div>
        </div>
        {renderLaps()}
      </TaGroup>
    </>
  )
}

Import

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

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

Import Type

组件导出的类型定义:

import type { CountTime, StopwatchOnStop } from 'tantalum-ui-mobile-react'

Props

属性类型默认值必填说明
showMillisecondsbooleantrue是否显示毫秒数
thousandsbooleantrue小时位是否以千分位形式显示

Events

事件描述回调函数参数函数 TypeScript
onStart计时启动时触发
onStop计时停止时触发payload: { detail: CountTime, laps: CountTime[] } detail 周期总时间,laps,周期内所有计次时间StopwatchOnStop
onReset计时复位时触发

CountTime 的结构

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

Methods

方法名说明参数
start开始计时() => void
stop停止计时() => void
reset复位/重置() => void
lap触发 1 次计次,返回一个周期() => CountTime[]

Slots

自定义风格显示(render)

<TaStopwatch render={countTime => <>{countTime.fullHours}</>} />