Giter VIP home page Giter VIP logo

pickerjs's Introduction

Picker.js

Build Status Coverage Status Downloads Version

JavaScript date time picker.

Table of contents

Main

dist/
├── picker.css
├── picker.min.css   (compressed)
├── picker.js        (UMD)
├── picker.min.js    (UMD, compressed)
├── picker.common.js (CommonJS, default)
└── picker.esm.js    (ES Module)

Getting started

Installation

npm install pickerjs

Include files:

<link  href="/path/to/picker.css" rel="stylesheet">
<script src="/path/to/picker.js"></script>

Usage

Syntax

new Picker(element[, options])
  • element

    • Type: HTMLElement
    • The target element for picking.
  • options (optional)

    • Type: Object
    • The options for picking. Check out the available options.

Example

<input type="text" id="input">
var input = document.getElementById('input');
var picker = new Picker(input, {
  format: 'YYYY/MM/DD HH:mm',
});

⬆ Back to top

Options

You may set picker options with new Picker(element, options). If you want to change the global default options, You may use Picker.setDefaults(options).

container

  • Type: Element or Selector
  • Default: null

Define the container for putting the picker. If not present, the picker will be appended to the document.body.

new Picker(element, {
  container: document.querySelector('.picker-container'),
});

Or

new Picker(element, {
  container: '.picker-container',
});

controls

  • Type: Boolean
  • Default: false

Indicate whether show the prev and next arrow controls on each column.

date

  • Type: Date or String
  • Default: null

The initial date. If not present, use the current date.

new Picker(element, {
  date: new Date(2048, 9, 24, 5, 12),
});

Or

new Picker(element, {
  date: '2048-10-24 05:12',
});

format

  • Type: String
  • Default: 'YYYY-MM-DD HH:mm'
  • Tokens:
    • YYYY: 4 digits year with leading zero
    • YYY: 3 digits year with leading zero
    • YY: 2 digits year with leading zero and be converted to a year near 2000
    • Y: Years with any number of digits and sign
    • MMMM: Month name
    • MMM: Short month name
    • MM: Month number with leading zero
    • M: Month number
    • DD: Day of month with leading zero
    • D: Day of month
    • HH: Hours with leading zero
    • H: Hours
    • mm: Minutes with leading zero
    • m: Minutes
    • ss: Seconds with leading zero
    • s: Seconds
    • SSS: Milliseconds with leading zero
    • SS: Milliseconds with leading zero
    • S: Milliseconds

The date string format, also as the sorting order of date time columns.

new Picker(element, {
  date: '2048-10-24 05:12:02.056',
  format: 'YYYY-MM-DD HH:mm:ss.SSS',
});

Or

new Picker(element, {
  date: 'Oct 24, 2048',
  format: 'MMM D, YYYY',
});

headers

  • Type: Boolean
  • Default: false

Indicate whether show the column headers. The text content of each header is defined in the text option.

increment

  • Type: Number or Object
  • Default: 1

Define the increment for each date time part.

new Picker(element, {
  increment: 10,
});

Or

new Picker(element, {
  increment: {
    year: 1,
    month: 1,
    day: 1,
    hour: 1,
    minute: 10,
    second: 10,
    millisecond: 100,
  },
});

inline

  • Type: Boolean
  • Default: false

Enable inline mode.

language

  • Type: String (ISO language code)
  • Default: ''

Define the language.

You should define the language first. Check out the i18n folder for more information.

months

  • Type: Array
  • Default: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

Months' name.

monthsShort

  • Type: Array
  • Default: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

Short months' name.

rows

  • Type: Number
  • Default: 5

Define the number of rows for showing.

text

  • Type: Object

  • Default:

    {
      title: 'Pick a date and time',
      cancel: 'Cancel',
      confirm: 'OK',
      year: 'Year',
      month: 'Month',
      day: 'Day',
      hour: 'Hour',
      minute: 'Minute',
      second: 'Second',
      millisecond: 'Millisecond',
    }

Define the title and button text of the picker.

translate

  • Type: Function

  • Default:

    function (type, text) {
      return text;
    }

Translate date time text.

new Picker(element, {
  translate(type, text) {
    const aliases = ['〇', '一', '二', '三', '四', '五', '六', '七', '八', '九'];

    return String(text).split('').map((n) => aliases[Number(n)]).join('');
  },
});

show

  • Type: Function
  • Default: null

The shortcut of the show event.

shown

  • Type: Function
  • Default: null

The shortcut of the shown event.

hide

  • Type: Function
  • Default: null

The shortcut of the hide event.

hidden

  • Type: Function
  • Default: null

The shortcut of the hidden event.

pick

  • Type: Function
  • Default: null

The shortcut of the pick event.

⬆ Back to top

Methods

If a method doesn't need to return any value, it will return the picker instance (this) for chain composition.

show()

Show the picker.

hide()

Hide the picker.

prev(type)

  • type:
    • Type: String
    • Options: 'year', 'month', 'day', 'hour', 'minute', 'second', 'millisecond'
    • Date time type.

Pick the previous item.

next(type)

  • type: (the same as the prev method)

Pick the next item.

pick()

Pick the current date to the target element.

getDate([formatted])

  • formatted (optional):
    • Type: Boolean
    • Format the date.
  • (return value):
    • Type: Date or String

Get the current date.

const picker = new Picker(element, {
  date: new Date(2048, 9, 24, 5, 12),
});

picker.getDate();
// > Sat Oct 24 2048 05:12:00 GMT+0800 (China Standard Time)

picker.getDate(true);
// > 2048-10-24 05:12

setDate(date)

  • date:
    • Type: Date
    • The new date.

Override the current date with a new date.

update()

Update the picker with the current the element value / text.

reset()

Reset the picker and the element value / text.

parseDate(date)

  • date:
    • Type: String
  • (return value):
    • Type: Date

Parse a date string with the set date format.

const picker = new Picker(element, options);

picker.parseDate('2048-10-24 05:12');
// > Sat Oct 24 2048 05:12:00 GMT+0800 (China Standard Time)

formatDate(date)

  • date:
    • Type: Date
  • (return value):
    • Type: String
    • The formatted date string.

Format a date object to a string with the set date format.

const picker = new Picker(element, options);

picker.formatDate(new Date(2048, 9, 24, 5, 12));
// > 2048-10-24 05:12

destroy()

Destroy the picker and remove the instance from the target element.

⬆ Back to top

Events

show

This event fires when a picker modal starts to show.

Only available in non-inline mode.

shown

This event fires when a picker modal has shown.

Only available in non-inline mode.

hide

This event fires when a picker modal starts to hide.

Only available in non-inline mode.

hidden

This event fires when a picker modal has hidden.

Only available in non-inline mode.

pick

This event fires when pick the current date to the target element.

If the target element is an <input> or <textarea> element, then a change event will be triggered too.

⬆ Back to top

No conflict

If you have to use other picker with the same namespace, just call the Picker.noConflict static method to revert to it.

<script src="other-picker.js"></script>
<script src="picker.js"></script>
<script>
  Picker.noConflict();
  // Code that uses other `Picker` can follow here.
</script>

Browser support

  • Chrome (latest)
  • Firefox (latest)
  • Safari (latest)
  • Opera (latest)
  • Edge (latest)
  • Internet Explorer 9+

Versioning

Maintained under the Semantic Versioning guidelines.

License

MIT © Chen Fengyuan

⬆ Back to top

pickerjs's People

Contributors

arsengoian avatar dependabot[bot] avatar fengyuanchen avatar ldursw avatar robsonoduarte 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pickerjs's Issues

bug? with format and init date

Hi,

I init picker with

date: 2018-07-05 12:00, format: 'YYYY-MM-DD HH:mm',

when I use getDate(), I get :
2018-07-05T10:00:35.695Z

so everything is ok, but when I use this :

date: 2018-07-05 12:00, format: 'HH:mm',

I get the current date and not the date initialized.

what do you think ?

Avoid tab when the picker is open

(Thanks for sharing this, it's a really good date picker!)

Is your feature request related to a problem? Please describe.
When the picker is open, pressing tab should not move the cursor in other fields, which can -also- cause some other pickers to show

Describe the solution you'd like
if the picker is open, a preventDefault() should be called if event.key is tab

How to enable only specific date?

Hi,

I want to disable all the dates except today and previous four days. How can I achieve this in pickerjs Datepicker?
That means today is 30th Dec and I want to allow user to select Date from 26th Dec. to today.

Week Input Support

Hi! First of all, I want to say that I really like your plugin.
I would like to suggest you to implement support for week inputs.
It would be really useful, and I think would make this plugin complete.


I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report 
[x] Feature request
[ ] Documentation issue or request

Current behavior

  • No format for weeks.

Expected behavior

  • There should be a ww or Www format for weeks.

31 days in June

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report 
[ ] Feature request
[ ] Documentation issue or request

Current behavior

When I pick 2018-07-31 (July) and I change the month to 06 (June), the picker change to 2018-06-01 but there is 31 days available instead of 30. The same thing happens for every months with less than 31 days.
The error comes from date.setMonth(value); in "current" function on line 911. Setting the month of new Date('2018-01-31') to 1 (February) for example returns 2018-03-03.

Expected behavior

  • There should be the good number of days available.
  • I think the picker should change to the last day of the month instead of the first.

I fix it with


    // line 911 of dist/picker.js
    date.setMonth(value);
    if (date.getMonth() != value) {
        date.setMonth(value + 1, 0);
    }

Environment


Picker.js version: 1.0.0


Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [x] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

Others:

Issue: DD-MM-YYYY HH:mm format Year has an additional - in front

Issue: DD-MM-YYYY HH:mm format Year has an additional - in front
issuedmy

eg. the field value is
02-10-2018 15:03
picker format I use is DD-MM-YYYY HH:mm

when the picker appear, the year drop download shows
-2018
-2019

Same issue appears in all kind of browser.
Only when the value is blank at beginning, things are OK.

up/down arrow

Hi, thanks you for this cool library :)

do you intend to add an up/down arrow as an option for each column, IMO is more intuitive on the desktop, for now I use the next() and prev(), but is not "design"
thanks

max day of month is wrong after setDate()

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report 
[ ] Feature request
[ ] Documentation issue or request

Current behavior

After using picker.setDate(date) the maximum number of days available is set to the number of days of the month of the initial date instead of the month of the new date.

Expected behavior

The number of days available should be the number of days of the month of the new date.

Minimal reproduction of the problem with instructions

  • init a picker on January
  • set the date to February using picker.setDate(date)
  • you can pick the 31th of February

Environment


Picker.js version: 1.0.0


Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [x] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

Others:

The problem comes from data.max function defined on line 1595 of dist/picker.js. After using setDate the date variable is always equal to the initial date.
It seems to be better after replacing date with _this.date.

Bug: DD-MM-YYYY HH:mm format Year has an additional - in front

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ x ] Bug report 
[ ] Feature request
[ ] Documentation issue or request

Current behavior

In your Sample Page:
set value 25-10-2018 15:45
set date format DD-MM-YYYY HH:mm
then click Show,
Year got - in front

Expected behavior

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Picker.js version: X.Y.Z


Browser:
- [x ] Chrome (desktop) version XX
- [x ] Chrome (Android) version XX
- [ x] Chrome (iOS) version XX
- [ x] Firefox version XX
- [ x] Safari (desktop) version XX
- [ x] Safari (iOS) version XX
- [ x] IE version XX
- [ x] Edge version XX

Others:

Make options clickable & have minDate option

Make options clickable

So far we can either scroll or use the Control option to add the arrows to select the date. But to be even more enhanced you should have the option to be able to click on any value and for it to work.

Lets say day 20 is picked. And i can see day 19, 18 above and 21, 22 below the picked number 20. It would be cool if i click on 18 and that became the picked number. Because users would expect this.

minDate

My second feature i would like introduced is to have a minDate option & maxDate. I don't want the user to be able to select a date prior to today. Yes the date starts on todays date by default, but they can still scroll backwards...and the Year can literally go back to the year 0.

These implementations would really make this library even better 👍

Cheers

The slide/scroll doen't work on mobile device

Hi,

The pickerjs works fine on desktop devices that have mouse.
But it doesn't work well on mobile. While trying to scroll the date/time etc. the entire page scrolls down instead of pickerjs.

Any ideas ?

How to prevent default behaviour (printing picked time to el) when picking date/time?

Hi,

I'm wondering if there's a way to block the default behaviour of printing out the picked time to the select element on the pick event? I'm using momentjs to handle all time functionality and would prefer to use moment to print out the date to the element.

It would also allow you to select a larger element as the trigger element to without it wiping out your markup.

Thanks!

Scroll background on safari ios 11.4

Issue : thanks for your awesome library, i have 1 issue on ios 11.4 ( iphone X ), modal opened so when we scroll time on body content background still scrollable

bug

p/s : i edited your library for my project

Missing source map

The dist/picker.js file is referencing a missing sourcemap: //# sourceMappingURL=picker.js.map

Dragging the drums out of the modal should not trigger close

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report 
[ ] Feature request
[ ] Documentation issue or request

Current behavior

When using PickerJS not in inline mode, dragging drums out of the modal closes it.

Expected behavior

If a user releases the drum outside of the modal, PickerJS should not close the modal. Instead, it should set whatever the value it landed on for it.

Minimal reproduction of the problem with instructions

CodePen (Sorry I had to pull the JS and the CSS from your GH pages, if there's any problem I'll fix it right away)

  1. Make sure you use a mouse (and a PC) for this reproduction. This is not the case for mobile devices.
  2. Click the input.
  3. Drag one of the picker drums that appears. Make sure you drag out of the modal.
  4. The modal closes without updating the input field.

What is the motivation / use case for changing the behavior?

This is for those who want to change DateTime values in the PickerJS in a large amount with mice. Right now, we need to drag it so that we don't go out of the modal and get all the changes cancelled.

Environment


Picker.js version: 1.2.1 


Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [x] Firefox version 66.0.2 (macOS / PC)
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [x] Edge version 42.17134.1.0

Others:

not support format YYYYMM

It seems not support format like YYYYMM

new Picker(document.querySelector('.js-date-picker'), {
      format: 'YYYYMM',
});

Time Zone support

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report 
[X] Feature request
[ ] Documentation issue or request

Current behavior

The picker always uses the local date and time

Expected behavior

It should be possible to specify an optional time zone to use (like UTC)

What is the motivation / use case for changing the behavior?

Sometimes, it is necessary to pick dates and times in a different time zone from the current one

Environment


Picker.js version: 1.2.1


Browser:
- [X] Chrome (desktop) version 74.0.3729.131
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [X] Firefox version 66.0.5 (64-bit)
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

Others:

multiple time-pickers with inline-container

What have I to do to initialize multiple time-pickers with a inline-container ?

Must I have one container by each time-picker or is it possible to use one container for all time-pickers ?

Thanks and regards

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.