simplabs/ember-pikaday

Name: ember-pikaday

Owner: simplabs

Description: A datepicker component for Ember CLI projects.

Created: 2015-03-17 10:10:15.0

Updated: 2015-03-17 10:10:16.0

Pushed: 2015-07-09 14:53:04.0

Homepage:

Size: 217

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

ember-pikaday Build Status

ember-pikaday is an addon that can be installed with Ember CLI. It gives you a datepicker input component that can be used in your Ember.js application. Pikaday and Moment.js are used in the background so they are added as Bower dependencies to your application.

The component provided by ember-pikaday is fully acceptance tested. It also provides test helpers to interact with the datepicker in your own acceptance tests.

Installation
our-project-directory
r install:addon ember-pikaday
Usage

While the input shows a formatted date to the user the bound attribute is always a JavaScript date object. If the application sets the attribute without a user interaction the datepicker updates accordingly.

el>
art date:
pikaday-input value=startsAt}}
bel>

You can also change the default format from DD.MM.YYYY to any format string supported by Moment.js.

el>
art date:
pikaday-input value=startsAt format="MM/DD/YYYY"}}
bel>

You can change the yearRange. It defaults to 10. the yearRange can be a single number or two comma separated years.

el>
art date:
pikaday-input value=startsAt yearRange="4"}}
bel>
andlebars
el>
art date:
pikaday-input value=startsAt yearRange="2004,2008"}}
bel>

If the second year of the comma separated years is set to currentYear, it sets the maximum selectable year to the current year.

el>
art date:
pikaday-input value=startsAt yearRange="2004,currentYear"}}
bel>

The readonly attribute is supported as binding so you can make the input readonly for mobile or other usecases.

el>
art date:
pikaday-input value=startsAt readonly="readonly"}}
bel>
Return dates in UTC time zone

The date returned by ember-pikaday is in your local time zone due to the JavaScript default behaviour of new Date(). This can lead to problems when your application converts the date to UTC. In additive time zones (e.g. +0010) the resulting converted date could be yesterdays date. You can force the component to return a date with the UTC time zone by passing useUTC=true to it.

el>
art date:
pikaday-input value=startsAt useUTC=true}}
bel>

ember-pikaday will not automatically convert the date to UTC if your application is setting the datepicker value directly!

Localization

Localizing the datepicker is possible in two steps. To localize the output of the datepicker, this is the formatted string visible in the input field, you simply add the correct Moment.js locale file to your applications Brocfile.js.

If I want to use the Austrian / German locale for example, my Brocfile.js will look like this. To use another locale you only have to change de-at.js to whatever locale you want to use.

import('bower_components/moment/moment.js');
import('bower_components/moment/locale/de-at.js');
import('bower_components/pikaday/pikaday.js');
import('bower_components/pikaday/css/pikaday.css');

To localize the datepicker itself, this is the popup you see after clicking the input, a little more work is necessary. The prefered way to do this is writting a custom initializer to inject a localized i18n object into the datepicker component. Naturaly you can use your own localized strings instead of the ones provided by Moment.js.

pp/initializers/setup-pikaday-i18n.js

lobals moment */

rt Ember from 'ember';

rt default {
me: 'setup-pikaday-i18n',
itialize: function(container, application) {
var i18n = Ember.Object.extend({
  previousMonth: 'Vorheriger Monat',
  nextMonth: 'Nächster Monat',
  months: moment.localeData()._months,
  weekdays: moment.localeData()._weekdays,
  weekdaysShort: moment.localeData()._weekdaysShort
});

container.register('pikaday-i18n:main', i18n, { singleton: true });
application.inject('component:pikaday-input', 'i18n', 'pikaday-i18n:main');


Test Helpers

The test helpers provided by ember-pikaday allow you to interact with the datepicker in your acceptance tests. After importing them you are ready to rock and roll.

rt { openDatepicker } from 'ember-pikaday/helpers/pikaday';

To open the datepicker use openDatepicker and pass the input element as argument.

Datepicker(Ember.$('#my-datepicker'));

openDatepicker not only opens the datepicker but also returns an interactor that can be used to interact with it. For example you can select a specific date by using selectDate.

interactor = openDatepicker(Ember.$('#my-datepicker'));

ractor.selectDate(new Date(1989, 3, 28));

To check if a specific day, month or year is selected there are also relevant methods available.

interactor = openDatepicker(Ember.$('#my-datepicker'));

ractor.selectDate(new Date(1989, 3, 28));

l(interactor.selectedYear(), 1989);
l(interactor.selectedMonth(), 3);
l(interactor.selectedDay(), 28);

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.