• Stars
    star
    159
  • Rank 235,916 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A datepicker component for Ember CLI projects.

ember-pikaday

Ember Observer Score NPM

ember-pikaday provides a datepicker modifier & components for Ember using the Pikaday library.

This addon is fully integration tested, and it provides test helpers to interact with the datepicker in your own tests.

Installation

Prerequisites:

  • Ember.js v3.25 or above
  • Node.js v12 or above
  • ember-auto-import 2.0 or above

Optional prerequisites:

  • If you use the backward-compatible <PikadayInput> or <PikadayInputless> components, your app must depend on either moment or moment-timezone and you should remember to configure your locale and timezone requirements. See Using Moment.js in Ember Apps & Addons.
  • But if you only use the new <input {{pikaday}} /> modifier, moment or moment-timezone are optional. Pikday itself uses them if they present, but doesn't require them.

Anti-prerequisites:

  • Remove ember-cli-moment-shim from your app. Earlier versions of this addon required it, but now it will only give you a redundant copy with the one provided by ember-auto-import.

Styles

In order to give apps control over styling, the default CSS does not load unless you tell it to. The recommended way to load the CSS is to create this file:

// app/modifiers/pikaday.js

/* Opt-in to using pikaday's default CSS */
import 'ember-pikaday/pikaday.css';
export { default } from 'ember-pikaday/modifiers/pikaday';

This guarantees that the CSS will load whenever your app uses the {{pikaday}} modifier (and the {{pikday}} modifier is used internally by all the other provided components, so this covers them too).

Usage

{{pikaday}} modifier

The {{pikaday}} modifier invokes new Pikaday() with your element as Pikaday's field option:

<input
  {{pikaday
    format='DD.MM.YYYY'
    value=this.startDate
    onSelect=this.setStartDate
  }}
/>

The optional value argument can be used to synchronize external changes into Pikaday. Internally, we do this using Pikaday's setDate.

All other named arguments are passed directly to Pikaday, so see Pikaday's configuration docs.

The only behaviors this modifier adds to the stock Pikaday are:

  • if you set minDate or maxDate and that causes value to be outside the legal range, we adjust value and fire onSelect to inform you of the change
  • if you set your <input> element's disabled attribute we will close Pikaday if it had been open.

<PikadayInput> Component

While the input shows a formatted date to the user, the value attribute can be any valid JavaScript date including Date object. If the application sets the attribute without a user interaction the datepicker updates accordingly.

<label>
  Start date:
  <PikadayInput @onSelection={{action 'doSomethingWithSelectedValue'}} />
</label>

You can also pass in other closure actions to handle onOpen, onClose and onDraw events.

<label>
  Start date:
  <PikadayInput
    @onOpen={{action 'doSomethingOnOpen'}}
    @onClose={{action 'doSomethingOnClose'}}
    @onDraw={{action 'doSomethingOnDraw'}}
  />
</label>

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

<label>
  Start date:
  <PikadayInput @format={{'MM/DD/YYYY'}} />
</label>

You can define a theme which will be a CSS class that can be used as a hook for styling different themes.

<label>
  Start date:
  <PikadayInput @theme={{'dark-theme'}} />
</label>

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

<label>
  Start date:
  <PikadayInput @yearRange={{'4'}} />
</label>
<label>
  Start date:
  <PikadayInput @yearRange={{'2004,2008'}} />
</label>

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

<label>
  Start date:
  <PikadayInput @yearRange={{'2004,currentYear'}} />
</label>

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

<label>
  Start date:
  <PikadayInput @readonly={{'readonly'}} />
</label>

The placeholder attribute is supported as binding so you can improve the user experience of your interface.

<label>
  Due date:
  <PikadayInput @placeholder={{'Due date of invoice'}} />
</label>

The disabled attribute is supported as binding so you can disabled the datepicker entirely. If the datepicker is shown to the user and it gets disabled it will close the datepicker itself.

<label>
  Due date:
  <PikadayInput @disabled={{isDisabled}} />
</label>

The firstDay attribute is supported as a binding so you can set the first day of the calendar week. Defaults to Monday.

  • 0 = Sunday
  • 1 = Monday
  • etc...
<label>
  Due date:
  <PikadayInput @firstDay={{0}} />
</label>

The minDate attribute is supported as a binding so you can set the earliest date that can be selected.

<label>
  Due Date:
  <PikadayInput @minDate={{minDate}} />
</label>

The maxDate attribute is supported as a binding so you can set the latest date that can be selected.

<label>
  Due Date:
  <PikadayInput @maxDate={{maxDate}} />
</label>

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.

<label>
  Start date:
  <PikadayInput @useUTC={{true}} />
</label>

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

Using pikaday specific options

You can pass any custom pikaday option through the component like this

<label>
  <PikadayInput
    @options={{hash
      numberOfMonths=2
      disableWeekends=true
      disableDayFn=(action 'someAction')
    }}
  />
</label>

Please refer to pikaday configuration

<PikadayInputless>

If you don't want to show an input field, you can use the <PikadayInputless/> component instead of <PikadayInput/>. It has the same API, but doesn't support onOpen and onClose. When disabled=true on a pikaday-inputless, the datepicker gets hidden.

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 include all the locales by following the ember-cli-moment-shim instructions and include the following in your ember-cli-build.js

app.import('node_modules/moment/locale/de.js');

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. Naturally you can use your own localized strings instead of the ones provided by Moment.js.

// app/initializers/setup-pikaday-i18n.js

import EmberObject from '@ember/object';
import moment from 'moment';

export default {
  name: 'setup-pikaday-i18n',
  initialize: function (application) {
    let i18n = EmberObject.extend({
      previousMonth: 'Vorheriger Monat',
      nextMonth: 'NΓ€chster Monat',
      months: moment.localeData().months(),
      weekdays: moment.localeData().weekdays(),
      weekdaysShort: moment.localeData().weekdaysShort(),
    });

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

Examples

Show ember-pikaday when clicking on a button:

<button {{action 'togglePika'}}>Show Pika</button>
{{#if showPika}}
  <PikadayInputless @value={{'2017-07-07'}} />
{{/if}}
// app/controller/index.js
import Ember from 'ember';
export default Ember.Controller.extend({
  actions: {
    togglePika() {
      this.toggleProperty('showPika');
    },
  },
});

Show ember-pikaday when hovering over a div:

<div
  {{action 'showPika' on='mouseEnter'}}
  {{action 'hidePika' on='mouseLeave'}}
>
  Hover me to pika
  {{#if showPika}}
    <PikadayInputless @value={{'2017-07-07'}} />
  {{/if}}
</div>
// app/controller/index.js

import Controller from '@ember/controller';
export default Controller.extend({
  actions: {
    showPika() {
      this.set('showPika', true);
    },
    hidePika() {
      this.set('showPika', false);
    },
  },
});

Test Helpers

The test helpers provided by ember-pikaday allow you to interact with the datepicker in your integration and acceptance tests.

Opening Pikaday

To open the datepicker use click from the @ember/test-helpers package:

import { click } from '@ember/test-helpers';

await click('.my-pikaday-input');

Closing Pikaday

Pikaday can be closed with the provided close helper:

import { close as closePikaday } from 'ember-pikaday/test-support';

await closePikaday('.my-pikaday-input');

Interacting with Pikaday

An Interactor, like a page object, provides helpers for getting and setting dates in a date picker:

import { click } from '@ember/test-helpers';
import { Interactor as Pikaday } from 'ember-pikaday/test-support';

await click('#my-datepicker');
await Pikaday.selectDate(new Date(1989, 3, 28));

There are also methods available to check if a specific day, month or year is selected:

await Interactor.selectDate(new Date(1989, 3, 28));

assert.equal(Interactor.selectedYear(), 1989);
assert.equal(Interactor.selectedMonth(), 3);
assert.equal(Interactor.selectedDay(), 28);

Other Resources

More Repositories

1

ember-electron

⚑ Build, test, compile and package desktop apps with Ember and Electron
JavaScript
806
star
2

ember-cp-validations

Ember computed property based validations
JavaScript
443
star
3

ember-changeset

Ember.js flavored changesets, inspired by Ecto
JavaScript
431
star
4

ember-moment

JavaScript
399
star
5

ember-infinity

⚑ Simple, flexible Infinite Scroll for Ember CLI Apps.
JavaScript
377
star
6

ember-data-model-fragments

Ember Data addon to support nested JSON documents
JavaScript
371
star
7

ember-metrics

Send data to multiple analytics integrations without re-implementing new API
JavaScript
367
star
8

ember-cli-flash

Simple, highly configurable flash messages for ember-cli
JavaScript
355
star
9

ember-light-table

Lightweight, contextual component based table for Ember
JavaScript
312
star
10

ember-data-factory-guy

Factories and helper functions for (unit, integration, acceptance) testing + development scenarios with Ember Data
JavaScript
302
star
11

ember-sortable

Sortable UI primitives for Ember.js
JavaScript
295
star
12

ember-burger-menu

An off-canvas sidebar component with a collection of animations and styles using CSS transitions
JavaScript
279
star
13

ember-cli-sass

Use node-sass to preprocess your ember-cli app's files, with support for sourceMaps and include paths
JavaScript
276
star
14

ember-notify

Notification messages for your Ember.js app
JavaScript
264
star
15

ember-collection

An efficient incremental rendering component for Ember.js with support for custom layouts and large lists
JavaScript
236
star
16

ember-changeset-validations

Validations for ember-changeset
JavaScript
219
star
17

ember-file-upload

File uploads for Ember apps
TypeScript
201
star
18

emberx-select

Select component for Ember based on the native html select element.
JavaScript
200
star
19

ember-keyboard

An Ember.js addon for the painless support of keyboard events
JavaScript
177
star
20

ember-impagination

An Ember Addon that puts the fun back in asynchronous, paginated datasets
JavaScript
122
star
21

active-model-adapter

Adapters and Serializers for Rails's ActiveModel::Serializers
TypeScript
102
star
22

ember-cli-hot-loader

An early look at what hot reloading might be like in the ember ecosystem
JavaScript
99
star
23

ember-autoresize

Autoresize for Ember Components
JavaScript
88
star
24

ember-cli-windows

🚀 Improve Ember-Cli Performance on Windows
JavaScript
79
star
25

ember-set-helper

A better `mut` helper
JavaScript
68
star
26

ember-inputmask

Ember wrapper around Inputmask.js
JavaScript
68
star
27

ember-collapsible-panel

An unopinionated, zero-dependency panel and accordion
JavaScript
57
star
28

ember-qunit-nice-errors

Because expected true, result false is not enough!
JavaScript
56
star
29

ember-cli-ifa

Ember CLI addon for injecting fingerprinted asset map file into Ember app
JavaScript
54
star
30

emberx-file-input

A tiny Ember component which does one thing and only: select files beautifully.
JavaScript
52
star
31

ember-cognito

AWS Amplify/Cognito and ember-simple-auth integration
JavaScript
32
star
32

ember-validators

A collection of EmberJS validators
JavaScript
24
star
33

ember-cli-bugsnag

Integrates Bugsnag reporting service into your Ember CLI app.
JavaScript
20
star
34

ember-stripe-elements

A simple Ember wrapper for Stripe Elements
JavaScript
18
star
35

ember-popper-modifier

An Ember modifier for working with Popper.js.
TypeScript
14
star
36

ember-launch-darkly

A modern Ember addon to wrap the Launch Darkly service
JavaScript
13
star
37

ember-require-module

Dynamically require modules
JavaScript
7
star
38

ember-indexeddb-adapter

Ember Data adapter for IndexedDB
JavaScript
4
star
39

program-guidelines

Checklist and guidelines for the Adopted Ember Addons org.
JavaScript
3
star
40

ember-cli-deploy-new-relic-sourcemap

Ember CLI Deploy plugin to deploy source maps to new relic
JavaScript
2
star