Installations
npm install stimulus-flatpickr
Developer
adrienpoly
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
NPM Version
Statistics
415 Stars
229 Commits
30 Forks
5 Watching
18 Branches
10 Contributors
Updated on 19 Nov 2024
Bundle Size
5.91 kB
Minified
2.19 kB
Minified + Gzipped
Languages
JavaScript (75.99%)
HTML (15.49%)
CSS (8.52%)
Total Downloads
Cumulative downloads
Total Downloads
5,408,516
Last day
-11.7%
5,755
Compared to previous day
Last week
-3.7%
33,997
Compared to previous week
Last month
15.6%
148,096
Compared to previous month
Last year
-8.2%
1,606,475
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
49
📆 Stimulus-Flatpickr Wrapper
Modest yet powerful wrapper of Flatpickr for Stimulus
Only ~1kb
- Simple: create advanced datepickers with less code
- Backend Friendly: easily pass backend information to the datepicker (locals, availabilities, date formats etc)
- strftime friendly: converts automatically strftime formats to flatpickr formating tokens
- Disable days of week: easily disable days of week (ie: all sundays)
- Turbolinks: make all your datepickers compatible with Turbolinks by design
- Getters: all Flatpickr elements are available as targets
- Events/hooks: all flatpickr events/hooks are directly available in your Stimulus Controller.
- Example: detailed example for adavanced usage of flatpickr
- MIT Licensed: free for personal and commercial use
A modest wrapper of Flatpickr for Stimulus
By using this wrapper of Flatpickr for Stimulus you can make all configurations for the Datepicker directly with the data-attributes
of the HTML. This makes it very handy to create datepicker with server generate html and pass information from the backend to the datepicker.
Here is a simple example:
1<%= form_with model: Appointement.new, authenticity_token: true do |f| %> 2 <%= f.text_field :start_time, 3 data: { 4 controller: "flatpickr", 5 flatpickr_min_date: Time.zone.now #disables past dates 6 } %> 7<% end %>
👇👇👇👇👇👇
Example
An example of a Rails app showcasing
- localization of the datepicker 🌍
- localization of the date formats 🌍
- availabilities in the date picker 📅
- Fully boosted with Turbolinks 🚀
is available here : Rails Stimulus Flatpickr
Install
This assumes that you have Stimulus already installed. For Rails(5.1+) app please refer this doc (https://github.com/rails/webpacker/blob/master/docs/integrations.md#stimulus) to get started with Stimulus.
In your project just add the flatpickr
and stimulus-flatpickr
package.
1yarn add flatpickr 2yarn add stimulus-flatpickr
or
1npm i flatpickr 2npm i stimulus-flatpickr
Note: Do not use both yarn
and npm
to install packages, this might lead to an error: ...It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files
Using importmap
1./bin/importmap pin flatpickr stimulus-flatpickr@beta
Basic usage
If you only need to convert an input field in a DateTime picker, you just need to register a standard Stimulus controller and add some markup to your input field.
Register a Flatpickr Controller
manually register a new Stimulus controller in your main JS entry point.
1// ./packs/application.js 2import { Application } from 'stimulus' 3import { definitionsFromContext } from 'stimulus/webpack-helpers' 4 5const application = Application.start() 6const context = require.context('../controllers', true, /\.js$/) 7application.load(definitionsFromContext(context)) 8 9// import Flatpickr 10import Flatpickr from 'stimulus-flatpickr' 11 12// Import style for flatpickr 13require("flatpickr/dist/flatpickr.css") 14 15// Manually register Flatpickr as a stimulus controller 16application.register('flatpickr', Flatpickr)
Note:
- Setup: By Manually registering Flatpickr controller, you don't need to create a
flatpickr_controller.js
file. However, To add custom behavior you will have to create theflatpickr_controller.js
file. Read more details about it below. - Style: You can always choose different theme for calender by requiring different
.css
file. You can find them inside your app's root directorynode_modules/flatpickr/dist/themes
- Deployment: In Production environment, include
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
in yourapplication.html.erb
file in order to load the calendar style.
Using it with Rails
You can now create forms and input fields easily by adding a data-controller="flatpickr"
attribute to the input fields and pass options with the Stimulus Controller states : data-flatpickr-the-option
.
1<%= form_with model: Appointement.new, authenticity_token: true do |f| %> 2 <%= f.text_field :start_time, 3 data: { 4 controller: "flatpickr", 5 flatpickr_date_format: "Y-m-d", 6 flatpickr_min_date: Time.zone.now 7 } %> 8<% end %>
👇👇👇👇👇👇
Options & conventions
All options for Flatpickr can be found here.
All options are in camelCase
(JS) and must be converted to lower_snake_case
in the data-attribute
. lower_snake_case
is automatically converted to kebab-case
when rails render the HTML.
1<%= f.text_field :start_time, 2 data: { 3 controller: "flatpickr", 4 flatpickr_enable_time: true 5 } 6} %>
will output this HTML:
1<input data-controller="flatpickr" data-flatpickr-enable-time="true" type="text" name="appointement[start_time]" />
HTML markup
If you are not using Rails or simply wants to markup your HTML directly, simply add a html data-controller="flatpickr"
to your input field and some options html data-flatpickr-some-option="value"
options must be converted from camelCase
to kebab-case
Advanced Usage
If you need more than just displaying the standard DateTime picker, then you can extend the stimulus-flatpickr
wrapper controller. This is necessary when you need to:
- set a custom language
- create custom callbacks
- perform JS business logic
Skip basics installation steps from above!
Extends the controller
create a new Stimulus controller that will inherit from stimulus-flatpickr
1// ./controllers/flatpickr_controller.js 2// import stimulus-flatpickr wrapper controller to extend it 3import Flatpickr from 'stimulus-flatpickr' 4 5// you can also import a translation file 6import { French } from 'flatpickr/dist/l10n/fr.js' 7 8// import a theme (could be in your main CSS entry too...) 9import 'flatpickr/dist/themes/dark.css' 10 11// create a new Stimulus controller by extending stimulus-flatpickr wrapper controller 12export default class extends Flatpickr { 13 initialize() { 14 // sets your language (you can also set some global setting for all time pickers) 15 this.config = { 16 locale: French 17 } 18 } 19 20 // all flatpickr hooks are available as callbacks in your Stimulus controller 21 change(selectedDates, dateStr, instance) { 22 console.log('the callback returns the selected dates', selectedDates) 23 console.log('but returns it also as a string', dateStr) 24 console.log('and the flatpickr instance', instance) 25 } 26}
Global settings for all datepickers
As we have seen just above you can easily from your rails erb
code pass the flatpickr options. This is great for passing dynamic options that might change (ie enableDate, dateFormat etc).
If all your datepickers share some global settings you can define them in your initialize()
or connect()
function.
1initialize() { 2 //global options 3 this.config = { 4 enableTime: true, 5 time_24hr: true 6 }; 7 }
or with connect()
1connect() { 2 //global options 3 this.config = { 4 ...this.config, //spread options in case some where defined in initialize 5 enableTime: true, 6 time_24hr: true 7 }; 8 9 //always call super.connect() 10 super.connect(); 11 }
HTML markup
Then in the same way as above you can now create forms and input fields easily by adding a data-controller="flatpickr"
attribute to the input fields and pass options with the Stimulus Controller states : data-flatpick-the-option
.
1<%= form_with model: Appointement.new, authenticity_token: true do |f| %> 2 <%= f.text_field :start_time, 3 data: { 4 controller: "flatpickr", 5 flatpickr_date_format: "Y-m-d", 6 flatpickr_min_date: Time.zone.now } 7 %> 8 <% end %>
👇👇👇👇👇👇
Date and Time formats
Flatpickr has custom formatting tokens. in Rails (and other backends) formats are based on strftime
standard.
This package automatically converts strftime
datetime formats to the nearest Flatpickr format.
With this solution, it becomes handy to localize your date formats. t("date.formats.long")
outputs "%B %d, %Y"
for the local :en
and it outputs "%e %B %Y"
for the locale :fr
.
1<%= form_with model: appointment do |f| %> 2 <%= f.text_field :start_at, 3 data: { 4 controller: "flatpickr", 5 flatpickr_alt_format: t("date.formats.long"), 6 flatpickr_alt_input: true, 7 flatpickr_min_date: Time.zone.now, 8 } %> 9<% end %>
👇👇👇👇👇👇
Enable/Disable days of week
With Flatpickr to disable certain days of the week, you need to use the disable js function. Obviously passing a function through data-attributes is not easy 😄.
The wrapper introduce two new configuration options:
disableDaysOfWeek
: pass an array of days to disable (all others are enabled)enableDaysOfWeek
: pass an array of days to enable (all others are disabled)
Code | Result |
---|---|
<%= form_with model: Appointement.new, authenticity_token: true do |f| %>
<%= f.text_field :start_time,
data: {
controller: "flatpickr",
flatpickr_disable_days_of_week: [5,6], #disables saturdays and sundays
flatpickr_disable: ["2018-09-25", "2018-09-26"] #disables individual dates
} %>
<% end %>
|
|
Callbacks
All Flatpickr events/hooks are available as callbacks in the extended controller as demonstrated above for the onChange
hook.
Just add the function to your Stimulus Controller in camelCase
without on
.
onChange
-> change(){}
Instance and its methods
You can access the flatpickr instance from your Stimulus controller by calling this.fp
. Also, the instance methods are available through this instance call.
1yourFunction () { 2 // ... 3 this.fp.clear() 4 this.fp.close() 5}
Custom elements
If you want to display additional information on the calendar, you can wrap the Flatpickr controller arround custom elements. You can use the predefined target instance
to attach the input element to the date picker.
Example:
1<div data-controller="flatpickr"> 2 <!-- the flatpicker instance --> 3 <input type="text" placeholder="Select Date.." data-flatpickr-target="instance" /> 4 <!-- the custom element --> 5 <input type="text" data-flatpickr-target="custom" /> 6</div>
In the stimulus controller, add the target:
1static targets = ['custom'] 2 3yourFunction () { 4 //... 5 this.customTarget 6}
Getters
Elements
In your controller you can access the Flapickr elements using some Stimulus like targets.
this.calendarContainerTarget
: Self-explanatory. This is the div.flatpickr-calendar element.
this.currentYearElementTarget
: The input holding the current year.
this.daysTarget
: The container for all the day elements.
this.daysContainerTarget
: The container for all the day elements.
this.inputTarget
: The text input element associated with flatpickr.
this.nextMonthNavTarget
: The “right arrow” element responsible for incrementing the current month.
this.monthNavTarget
: The container with the month navigation.
this.prevMonthNavTarget
: The “left arrow” element responsible for decrementing the current month.
this.selectedDateElemTarget
: the selected date element.
this.todayDateElemTarget
: today element.
this.weekdayContainerTarget
: the container we all the days of the week.
Overriding connect & disconnect
if you need to override the connect function in the extended controller, you need to call super
1connect(){ 2 // ... 3 // define global settings as explained in the global settings section before super 4 // ... 5 6 // always call super.connect() 7 super.connect(); 8 9 // ... 10 // Your code can access this.fp flatpickr instance 11 // ... 12}
Internationalization
To handle multiple language to translate your datepicker and convert the date formats, you can have a look at the example app. stimulus-flatpickr
makes it straight forward to handle locales.
CSS
This wrapper does not include any CSS. Flatpickr CSS should be loaded separately from the main Flatpickr package as you would normally do.
Contributing
Bug reports and pull requests are welcome.
To contribute:
Fork the project.
Install dependencies
$ yarn install
Start the test watcher
$ yarn test:watch
Running one-off test runs can be done with:
$ yarn test
You can test locally also the results with the playground project ./playground
$ yarn start:playground
Then :
👍 Write some tests
💪 Add your feature
🚀 Send a PR
License
This package is available as open source under the terms of the MIT License.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 4/12 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 23 are checked with a SAST tool
Reason
86 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-whgm-jr23-g3j9
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-ff7x-qrg7-qggm
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-j4f2-536g-r55m
- Warn: Project is vulnerable to: GHSA-r7qp-cfhv-p84w
- Warn: Project is vulnerable to: GHSA-6h5x-7c5m-7cr7
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-7x7c-qm48-pq9c
- Warn: Project is vulnerable to: GHSA-rc3x-jf5g-xvc5
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-82v2-mx6x-wq7q
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-92xj-mqp7-vmcj
- Warn: Project is vulnerable to: GHSA-wxgw-qj99-44c2
- Warn: Project is vulnerable to: GHSA-5rrq-pxf6-6jx5
- Warn: Project is vulnerable to: GHSA-8fr3-hfg3-gpgp
- Warn: Project is vulnerable to: GHSA-gf8q-jrpm-jvxq
- Warn: Project is vulnerable to: GHSA-2r2c-g63r-vccr
- Warn: Project is vulnerable to: GHSA-cfm4-qjh2-4765
- Warn: Project is vulnerable to: GHSA-x4jg-mjrx-434g
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-g6ww-v8xp-vmwg
- Warn: Project is vulnerable to: GHSA-566m-qj78-rww5
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-g4rg-993r-mgx7
- Warn: Project is vulnerable to: GHSA-fxwf-4rqh-v8g3
- Warn: Project is vulnerable to: GHSA-25hc-qcg6-38wj
- Warn: Project is vulnerable to: GHSA-xfhh-g9f5-x4m4
- Warn: Project is vulnerable to: GHSA-qm95-pgcg-qqfq
- Warn: Project is vulnerable to: GHSA-cqmj-92xf-r6r9
- Warn: Project is vulnerable to: GHSA-c9g6-9335-x697
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-rqff-837h-mm52
- Warn: Project is vulnerable to: GHSA-8v38-pw62-9cw2
- Warn: Project is vulnerable to: GHSA-hgjh-723h-mx2j
- Warn: Project is vulnerable to: GHSA-jf5r-8hm2-f872
- Warn: Project is vulnerable to: GHSA-v4rh-8p82-6h5w
- Warn: Project is vulnerable to: GHSA-mgfv-m47x-4wqp
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-72mh-269x-7mh5
- Warn: Project is vulnerable to: GHSA-h4j5-c7cj-74xg
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
2.1
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More