Giter VIP home page Giter VIP logo

appointment-picker's Introduction

Appointment Picker

A lightweight, accessible and customizable javascript timepicker widget

Yet another timepicker? - Advantages

  • no dependencies
  • tiny (6KB minified, 2KB gzipped)
  • only the listed times can be picked or entered, no validation necessary

See Appointment-Picker Demo and Examples

Installation

npm i -S appointment-picker

Setup

When using inside a static HTML file, add both the stylesheet and the script

<link rel="stylesheet" href="css/appointment-picker.css">
<script src="js/appointment-picker.min.js"></script>

<script type="text/javascript">
    new AppointmentPicker(...);
</script>

Import

When using with a module loader, import the node-module

// Webpack (React, Angular, ES6)
import AppointmentPicker from 'appointment-picker';

// CommonJS (Node, Browserify)
const AppointmentPicker = require('appointment-picker');

Initialize the picker using the new keyword

<input id="time" type="text" value="10:00">
var picker = new AppointmentPicker(document.getElementById('time'), {});

Options

The appointment-picker can be configured with options

  • interval sets the interval between appointments in minutes (1-60), if this number gets lower (more possible appointments) the picker will get longer
  • mode the picker can be used in standard 24h hour mode or in 12h mode - the latter uses am/pm postfix
  • minTime sets the minimum hour that can be picked, default is 0 what is eqivalent to 12am
  • maxTime sets the maximum hour that can be picked, default is 24
  • startTime hides all appointments below this hour, default is 0
  • endTime hides all appointments above this hour, default is 24
  • disabled array of disabled appointments, i.e. ['10:30', '1:15pm', ...] - these times cannot be selected or entered and will be skipped using the keyboard arrows
  • large increases the size of the picker and the appointments by setting a is-large modifier
  • leadingZero adds leading zero to single-digit hour if true (i.e. 07:15)
  • allowReset whether a time can be reset once entered
  • title defines the picker's heading
  • templateOuter HTML template that renders the picker's outer frame (usually containing a wrapper and title), must contain {{innerHtml}} placeholder (example)
  • templateInner template for repeated list items (time inputs), must contain at least an input tag, {{time}} and optional {{disabled}} placeholder, i.e. <input type="button" value="{{time}}" {{disabled}}>
  • timeFormat24 custom time format for 24h mode (use placeholder H for hour, M for minute), i.e. H/M could evaluate to 13/45 (example)
  • timeFormat12 custom time format for am/pm mode (use placeholder apm for postfix - the algorithm will remove either the a or the p from the pattern), i.e. H.M AP.M. could evaluate to 1.30 A.M

Note: with startTime and endTime appointments below and above can be visually removed. If startTime is greater than minTime a lower time can still be manually set via the keyboard. On the other hand the picker does not accept lower hours than minTime and higher than maxTime. Manually entered times outside of the defined bounds will be rejected by the picker, no extra validation is therefore needed. Entering an empty string into the input resets the time.

Now you can pass your options into the the AppointmentPicker invocation

var picker = new AppointmentPicker(document.getElementById('time-2'), {
  interval: 30,
  mode: '12h',
  minTime: 09,
  maxTime: 22,
  startTime: 08,
  endTime: 24,
  disabled: ['16:30', '17:00'],
  templateInner: '<li class="appo-picker-list-item {{disabled}}"><input type="button" tabindex="-1" value="{{time}}" {{disabled}}></li>',
  templateOuter: '<span class="appo-picker-title">{{title}}</span><ul class="appo-picker-list">{{innerHtml}}</ul>'
});

Methods

The appointment-picker exposes several functions to change its behaviour from outside (example).

Method Desc.
picker.open() open the picker popup
picker.getTime() get the current time programmatically from a picker instance, returns an object like { h: 14, m: 30, displayTime: '2:30 pm' }
picker.setTime('10:30') set a time of a picker instance (empty string resets the time)
picker.close() close the picker popup
picker.destroy() destroy the picker instance and remove both the markup and all event listeners

Events

Appointment-picker exposes events for hooking into the functionality:

  • change.appo.picker triggered on each successful value change (event example)
  • open.appo.picker is fired each time the picker is opened
  • close.appo.picker is fired each time the picker is closed

Each event contains 2 properties time and displayTime, i.e. event.time: {'h':14,'m':30} and event.displayTime: '2:30 pm'

document.body.addEventListener('change.appo.picker', function(e) { var time = e.time; }, false);

Styling

All appointment-picker styles are namespaced with .appo-picker, i.e. .appo-picker-list-item. You can either copy and modify the provided CSS, or import it using

/* Using sass/scss */
@import '~appointment-picker/css/appointment-picker';

or in your javascript file

// Using a css-loader inside JS (relative path to your node_modules folder)
import '../node_modules/appointment-picker/css/appointment-picker.css';

Accessibility

For screen reader support add both a aria-label and aria-live properties on the input field

<input id="time-1" type="text" aria-live="assertive" aria-label="Use up or down arrow keys to change time">

Browser Support

  • Chrome
  • Firefox
  • Safari (macOS 10 & iOS 9)
  • Edge
  • IE11 / IE10
  • IE9 (with classList polyfill)

Legacy browser support (IE9)

Add the element.classList polyfill by either importing it with a module loader or simply add the polyfill from a CDN in your html head.

Integration

Use as jQuery plugin

If you would like to use the appointment-picker as a jQuery plugin add following code before initializing

$.fn.appointmentPicker = function(options) {
  this.appointmentPicker = new AppointmentPicker(this[0], options);
  return this;
};

Now you can initialize the picker on any text input field using $

<input id="time-1" type="text">
var $picker = $('#time-1').appointmentPicker();

// Or pass in options
$('#time-1').appointmentPicker({
  interval: 15
});

// And access all exposed methods using jQuery
$picker.appointmentPicker.getTime(); // i.e. { h: 15, m: 30, displayTime: '3:30 pm' }

Use with React

Appointment Picker can be easily integrated into a React component. Simply import the node module and use React.createRef() to pass the DOM element when calling new AppointmentPicker (see example)

import AppointmentPicker from 'appointment-picker';

class AppoPicker extends React.Component {

  constructor(props) {
    super(props);
    this.pickerRef = React.createRef();
    this.onTimeSelect = this.onTimeSelect.bind(this);
  }
  
  render() {
    return <input type="text" ref={ this.pickerRef }></input>;
  }

  onTimeSelect(event) {
    console.log(event.time);
  }
  
  componentDidMount() {
    this.picker = new AppointmentPicker(this.pickerRef.current, {});
    this.pickerRef.current.addEventListener('change.appo.picker', this.onTimeSelect);
  }
  
  componentWillUnmount() {
    this.pickerRef.current.removeEventListener('change.appo.picker', this.onTimeSelect);
    this.picker.destroy();
  }
}

Use with Angular

To integrate AppointmentPicker into an Angular component, import it's CSS, create a @ViewChild reference (#pickerInput) and pass it's nativeElement when calling new AppointmentPicker.

/* CSS, SCSS */
@import '~appointment-picker/css/appointment-picker';
<!-- HTML -->
<input #pickerInput type="text">
import AppointmentPicker from 'appointment-picker';

@Component({
  selector: 'xyz-picker',
  templateUrl: './picker.component.html',
  styleUrls: ['./picker.component.scss'],
  // Disable View Encapsulation to pass down the picker's CSS styles
  encapsulation: ViewEncapsulation.None
})
export class PickerComponent implements OnInit, OnDestroy {

  @ViewChild('pickerInput') input: ElementRef;

  picker: AppointmentPicker;

  @HostListener('change.appo.picker', ['$event'])
  onChangeTime(event: any) {
    console.log('change.appo.picker', event.time);
  }

  ngOnInit() {
    this.picker = new AppointmentPicker(this.input.nativeElement, {});
  }

  ngOnDestroy() { this.picker.destroy(); }
}

Author & License

  • Jan Suwart | MIT License

appointment-picker's People

Contributors

jannicz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

appointment-picker's Issues

Intervals of 45mins not being calculated properly

When choosing an interval of 45mins, the time slots are not being calculated properly.
See screenshot provided.

image
image

It must be 10:00, 10:45, 11:30, 12:15, ...
I have not tested for all different kind of intervals between 1-60, other intervals may not be calculated properly as well.

Click, hold & release on input closes the picker

The picker should not close either if clicked in the same input field or after holding and releasing the mouse button. The picker should only close if clicked outside of both the input and the picker area.

only allowed dates?

As it seems timeslot are created programmatically via min/max, start/end and disabled dates -

But I have an array here which contains only the available start times of a timeslot.
(the length of a slot is defined.)

Would it be possible to pass this array to to th picker somehow?

Thank you in advance and best Regards, MT

Leading Zero

Do a-picker capable of leading zero for 24H format (05:30 instead of 5:30)?

Missing IE10 Flexbox support

IE10 and older Safari Browsers could be easily supported by adding

display: -ms-flexbox;  /* IE 10 */
display: -webkit-flex; /* Safari 6.1+. iOS 7.1+, BB10 */

Add a case to the valid check

if (hour < opt.minTime || hour > opt.maxTime || hour > 24) { // Out of min/max

maxTime sets the maximum hour that can be picked, default is 24

it mean that e.g 9 is set that 9:15 cannot be picked or ?

if yes, add please this check (hour === opt.maxTime && minute) for the intervals, in the moment u can pick 9:00 as last maxTime is correct, but not the 9:[intervals].
} else if(hour === opt.maxTime && minute > 0){

Not working in IE9

Add classList polyfill to make the picker work in IE9 (and comparable) browsers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.