0.0.4 • Published 4 years ago

react-time-tracker-stopwatch v0.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

react-time-tracker-stopwatch

A React and TypeScript based stopwatch for time tracking.

Installation

npm install --save react-time-tracker-stopwatch
# or
yarn add react-time-tracker-stopwatch

Usage

Styling is not included, but with only three internal elements and a wide array of properties to modify the styling of said elements, this should be fairly simple to work into whatever project.

import * as React from 'react';
import TimeTracker from 'react-time-tracker-stopwatch';

class MyTimer extends React.Component {
  constructor(props, ...args) {
    super(props, ...args);
    this.timeTracker = React.createRef();
  }

  render() {
    return (
      <div>
        <TimeTracker
          onTimeUpdate={(...args) => console.log(args)}
          ref={this.timeTracker}
        >
        <button
          onClick={() => this.timeTracker.current.setTimeTrackerState(0)}
        >
          Reset to 0:00:00 but don't change whether or not the tracker is running.
        </button>
        <button
          onClick={() => this.timeTracker.current.setTimeTrackerState(0, 0)}
        >
          Reset to 0:00:00 but make sure the tracker is running.
        </button>
        <button
          onClick={() => this.timeTracker.current.setTimeTrackerState(0, -1)}
        >
          Reset to 0:00:00 but make sure the tracker is not running.
        </button>
        <button
          onClick={() => this.timeTracker.current.setTimeTrackerState(3600, -1)}
        >
          Reset to 1:00:00 but make sure the tracker is not running.
        </button>
      </div>
    )
  }
}

Props

PropTypeDescription
startTextstring?Text for the control button to start the timer. Defaults to Start.
stopTextstring?Text for the control button to stop the timer. Defaults to Stop.
initialValuenumber?The initial value of the timer in seconds. Defaults to 0.
initialStartTimestampnumber?The initial starting timestamp of the timer in seconds. If this value is greater than or equal to zero, the timer will automatically be started with the given timestamp. If the value is not provided or is less than zero, the timer will not be started and the starting timestamp will be set to null.
wrapClassstring?The class of the div element wrapping the component. Defaults to time-tracker-wrap.
inputClassstring?The class of the input element of the component. Defaults to time-tracker-input.
buttonClassstring?The class of the button element of the component. Defaults to time-tracker-button.
wrapStyleReact.CSSProperties?Inline styles to be passed to the wrapping div element.
inputStyleReact.CSSProperties?Inline styles to be passed to the input element of the component.
buttonStyleReact.CSSProperties?Inline styles to be passed to the button element of the component.
onTimeUpdate(value: number, startTimestamp: number | null) => voidAn update handler for the component. Will be called when the time updates. value is the current value of the component in seconds. startTimestamp is the UNIX timestamp of the most recent time the tracker started running in seconds or null if the timer is not running.

Member Functions

FunctionSignatureDescription
setTimeTrackerState(value: number, startTimestamp?: number) => voidA function used to set the value of the time tracker in seconds and control the state of the tracker. The startTimestamp argument can be used to control the running state of the time tracker with the following rules: a number greater than or equal to zero will start the timer with the given startTimestamp, a number less than zero will stop the timer, and undefined (including if no value is passed) will not affect the state of time tracker.