1.0.2 • Published 6 years ago

analogue-time-picker v1.0.2

Weekly downloads
46
License
MIT
Repository
github
Last release
6 years ago

analogue-time-picker

A small and simple time picker based on google material UI

time picker

  • 12h and 24h versions
  • Mobile and desktop enabled
  • Custom styles, and works with material UI
  • Responsive
  • Accessable
  • No dependencies

How to use

Demo

https://shanegh.github.io/analogue-time-picker/demo/

Use

import { timePickerInput } from 'analogue-time-picker'

var input = document.getElementById("myInput");
timePickerInput({ inputElement: input });

Style

The time picker has a default style. You can override by

Size

The size of the time picker is specified by the width and the font size. The clock has a fixed aspect ratio, so the width will determine the width and the height of the component. The font size specifies the size and spacing of the figures.

  • Retain the default size of 300px.
  • Add a width paramater to the time picker: timePicker({ width: "300px" }). The font size is then generated automatically.
  • Specify the width on the parent element
var parent = document.getElementById("theParent");
parent.style.width = "300px";
parent.style.fontSize = "16px";
timePicker({ element: parent });

API

analog-time-picker exposes 3 functions: timePicker, timePickerModal and timePickerInput.

timePicker

Create a timepicker contained in the "element" property of the output.

var timePicker = timePicker({
    element: document.getElementById("parentElement"),
    mode: 12,
    width: "300px",
    time: { hour: 13, minute: 0 }
});

Inputs

NameTypeOptionalDescriptionDefault
elementHTMLElementtrueAn element to append the time picker html to.undefined
modenumber/stringtrue12 hour or 24 hour mode. Accepts 12, 24, "12", "24".The default browser culture of the user
widthnumber/stringtrueThe width of the component. Will also be used to calculate the font size. If % or em are used, the font sized cannot be auto calculated, and must be specified manually via css"300px"
time{hour: number | string, minute: number | string} or DatetrueThe initial time of the component in 24h format{ hour: 0, minute: 0 }
focusbooleantrueIf set to true, the "hours" input will be focused on render.false

Output

NameTypeDescription
elementHTMLElementThe element which contains the time picker
setWidth(width: number | string) => voidSee width input.
getTime() => {hour: number, minute: number}Return the current time
setTime(hours?: number | string, minutes?: number | string) => voidSet the current time programmatically
set12h() => voidSet the clock to 12 hour mode. If the clock is in 12h mode, the times used in getTime, setTime and onOk will still be in 24h format
set24h() => voidSet the clock to 24 hour mode
showHours() => voidShow the hour hand
showMinutes() => voidShow the minute hand
ok() => voidDispose of the time picker and invoke any "onOk" event handlers
cancel() => voidDispose of the time picker and invoke any "onCancel" event handlers
onTimeChanged(callback: (hour: number, minute: number) => void) => voidAdd an event handler for when the time changes
onOk(callback: (hour: number, minute: number) => void) => voidAdd an event handler for when the set time operation completes successfully
onCancel(callback: () => void) => voidAdd an event handler for when the set time operation is canceled
onDispose(callback: () => void) => voidAdd an event handler for when the time picker is disposed of
dispose() => voidManually dispose of the time picker

timePickerModal

Create a timepicker and render in a modal popup

var timePickerModal = timePickerModal({
    mode: 12,
    width: "300px",
    time: { hour: 13, minute: 0 }
});

Inputs

NameTypeOptionalDescriptionDefault
modenumber/stringtrue12 hour or 24 hour mode. Accepts 12, 24, "12", "24".The default browser culture of the user
widthnumber/stringtrueThe width of the component. Will also be used to calculate the font size. If % or em are used, the font sized cannot be auto calculated, and must be specified manually"300px"
timeobj or DatetrueThe initial time of the component in 24h format{ hour: 0, minute: 0 }

Output

See timePicker output

timePickerInput

Create a timepicker from an <input /> element. The timepicker renders when the input is focused and adds it value to the input when closed.

var timePickerModal = timePickerInput({
    inputElement: document.getElementById("myInput"),
    mode: 12,
    width: "300px",
    time: { hour: 13, minute: 0 }
});

Inputs

NameTypeOptionalDescriptionDefault
inputElementHTMLInputElementfalseThe input to specify as a time pickern/a
modenumber/stringtrue12 hour or 24 hour mode. Accepts 12, 24, "12", "24".The default browser culture of the user
widthnumber/stringtrueThe width of the component. Will also be used to calculate the font size. If % or em are used, the font sized cannot be auto calculated, and must be specified manually"300px"
timeobj or DatetrueThe initial time of the component in 24h format{ hour: 0, minute: 0 }

Output

NameTypeDescription
getTime() => {hour: number, minute: number}If the time picker is open, get it's time. Otherwise, get the time in the <input />. If the input cannot be parsed, returns null
setTime(hour: number | string, minute: number | string, force?: boolean) => voidSet the time in the time picker if open, otherwise, set the time in the input. If paramater force === true, set the time in the input either way
dispose() => voidDispose of the time picker input