pickadate.js
  • Documentation
  • Discussions
  • GitHub
  • v3 Docs

›Bindings

Getting Started

  • Introduction
  • Installation
  • Usage

Bindings

  • Plain JavaScript
  • React DOM
  • React Native
  • jQuery

Guides

  • Using Translations
  • Creating Custom Actors
  • Upgrading from v3

API

  • The State Object
  • The Picker Object
  • The Store Object
  • The Translation Object

Plain JavaScript

The plain JavaScript binding uses a simple DOM rendering mechanism to render the UI and keep it in sync with the state of the picker.

It comes with the following methods:

  • create
  • render
  • unrender

Overview

The first step is to create a picker object:

const picker = pickadate.create()

With the picker created, it can be rendered into any HTML element:

const element = document.getElementById('pickadate')
pickadate.render(element, picker)

Once done with the UI and it is ready to be destroyed, it can be unrendered from the HTML element:

const element = document.getElementById('pickadate')
pickadate.unrender(element)

Continue reading below for a full reference on all the methods and events.

create

ArgumentsReturns
initialState: ?Objectpicker
initialTranslation: ?Object

Creates a picker object that can be rendered into an element.

Initial State

The picker can be created with an optional initial state.

const initialState = {
  selected: new Date(),
}
const picker = pickadate.create(initialState)

Initial Translation

The picker can also be created with an optional translation object.

import frenchTranslation from 'pickadate/builds/translations/fr_FR'
const initialState = {
  selected: new Date(),
}
const picker = pickadate.create(initialState, frenchTranslation)

render

ArgumentsReturns
element: HTMLElementnothing
picker
options: ?Object

Renders the UI for the picker into an HTML element.

If the element is an input element, a new div is created as a sibling and the UI is rendered into it instead.

const element = document.getElementById('date-picker')
pickadate.render(element, picker)

options

The third argument to pickadate.render is an optional object to customize how the picker renders.

Here a list of all the valid options properties:

  • className
  • renderCell

options.className

The various class names used to style the picker UI.

KeyTypeDescription
rootstring, string[]The root element of the picker.
root__activestring, string[]The active state of the root.
inputRootstringThe wrapper of the root when an input element is the source element.
header, body, footerstring, string[]The respective header, body, and footer sections of the picker.
monthAndYear, monthAndYear_month, monthAndYear_yearstring, string[]The month and year text elements.
gridstring, string[]The grid button element.
grid__focusedstringThe focused state of the grid. This is to be used alongside the :focus pseudo selector state of the grid.
grid_rowstring, string[]The rows in the grid.
grid_row__labelstringThe modification added to grid_row for the heading labels row in the grid.
grid_labelstring, string[]The heading labels of the grid.
grid_cellstring, string[]The cells of the grid.
grid_cell__today, grid_cell__highlighted, grid_cell__selected, grid_cell__outOfView, grid_cell__disabledstringThe modifications applied to grid_cell based on the state of the cell.
button_previousstring, string[]The "previous" button.
button_nextstring, string[]The "next" button.
button_todaystring, string[]The "today" button.
button_clearstring, string[]The "clear" button.
button_meridiemstring, string[]The "meridiem" button in the time units.
timestring, string[]The time units section.
time_hoursstring, string[]The "hours" section.
time_minutesstring, string[]The "minutes" section.
time_separatorstring, string[]The "separator" between the hours & minutes.
time_inputstringThe input of a time unit.
time_input__hoursstringThe input of the "hours" unit.
time_input__minutesstringThe input of the "minutes" unit.
time_counterstringThe counters of a time unit.
time_counter__up, time_counter__downstringThe up/down counter of a time unit.

Default value

const className = {
  root: 'pickadate--root',
  root__active: 'pickadate--root__active',
  inputRoot: 'pickadate--input-root',

  header: 'pickadate--header',
  body: 'pickadate--body',
  footer: 'pickadate--footer',

  monthAndYear: 'pickadate--monthAndYear',
  monthAndYear_month: 'pickadate--monthAndYear_month',
  monthAndYear_year: 'pickadate--monthAndYear_year',

  grid: 'pickadate--grid',
  grid__focused: 'pickadate--grid__focused',
  grid_row: 'pickadate--grid_row',
  grid_row__label: 'pickadate--grid_row__label',
  grid_label: 'pickadate--grid_label',
  grid_cell: 'pickadate--grid_cell',
  grid_cell__today: 'pickadate--grid_cell__today',
  grid_cell__highlighted: 'pickadate--grid_cell__highlighted',
  grid_cell__selected: 'pickadate--grid_cell__selected',
  grid_cell__outOfView: 'pickadate--grid_cell__outOfView',
  grid_cell__disabled: 'pickadate--grid_cell__disabled',

  button_previous: ['pickadate--button', 'pickadate--button__previous'],
  button_today: ['pickadate--button', 'pickadate--button__today'],
  button_next: ['pickadate--button', 'pickadate--button__next'],
  button_clear: ['pickadate--button', 'pickadate--button__clear'],
  button_meridiem: ['pickadate--button', 'pickadate--button__meridiem'],

  time: 'pickadate--time',
  time_hours: ['pickadate--time_unit', 'pickadate--time_unit__hours'],
  time_minutes: ['pickadate--time_unit', 'pickadate--time_unit__minutes'],
  time_separator: 'pickadate--time_separator',
  time_input: 'pickadate--time_input',
  time_input__hours: 'pickadate--time_input__hours',
  time_input__minutes: 'pickadate--time_input__minutes',
  time_counter: 'pickadate--time_counter',
  time_counter__up: 'pickadate--time_counter__up',
  time_counter__down: 'pickadate--time_counter__down',
}

options.renderCell

A render method to customize the appearance of individual date cells in the grid of the picker.

Default value

By default, it does nothing:

const renderCell = () => {}

Custom value

To customize the rendering, the date object and the default children are passed as arguments:

type RenderCell = ({
  dateObject: Date,
  children: HTMLElement,
}) => ?HTMLElement,

The children can be used or ignored; a new element can be returned or not:

const renderCell = ({ dateObject, children }) => {
  if (dateObject.getDate() === 10) {
    children.style.color = 'red'
    return
  }
  if (dateObject.getDay() === 4) {
    const newElement = document.createElement('div')
    newElement.innerHTML = `<div>${dateObject.getDate()}<div>⭐️</div></div>`
    return newElement
  }
}

unrender

ArgumentsReturns
element: HTMLElementnothing

Unrenders the UI for the picker bound to an HTML element.

const element = document.getElementById('date-picker')
picker.unrender(element)

Events

To bind a handler to a UI event, add an event listener to the element the picker is rendered into:

const onChange = formattedValue => {
  console.log('New value:', formattedValue)
}
element.addEventListener('pickadate:change', onChange)

To unbind the handler, remove the listener as any other DOM event:

element.removeEventListener('pickadate:change', onChange)

The event names are:

  • pickadate:change
  • pickadate:mount
  • pickadate:unmount
  • pickadate:input-open
  • pickadate:input-close

pickadate:change

Dispatched when the value of the picker changes.

pickadate:mount

Dispatched when the picker mounts into the element.

pickadate:unmount

Dispatched when the picker unmounts from the element.

pickadate:input-open

Dispatched when the picker (rendered into an input element) opens.

pickadate:input-close

Dispatched when the picker (rendered into an input element) closes.

← UsageReact DOM →
  • Overview
  • create
    • Initial State
    • Initial Translation
  • render
    • options
    • options.className
    • options.renderCell
  • unrender
  • Events
    • pickadate:change
    • pickadate:mount
    • pickadate:unmount
    • pickadate:input-open
    • pickadate:input-close
pickadate.js
Documentation
Getting StartedJavaScriptReact DOMReact NativejQuery
Community
Stack OverflowSpectrum
More
GitHubStar
Copyright © 2012-2019 Amsul and the pickadate.js documentation authors.