diff --git a/README.md b/README.md
index 5c4d4d7..df2c783 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,13 @@
# easy-dates-picker
-`easy-dates-picker` is a super lightweight, zero-dependency, customizable date picker library, written in vanilla JavaScript.
+`easy-dates-picker` is a super lightweight, zero-dependency, localized, customizable date picker library, written in vanilla JavaScript.
-![npm](https://img.shields.io/npm/dt/easy-dates-picker)
-![npm](https://img.shields.io/npm/dw/easy-dates-picker)
+![npm bundle size](https://img.shields.io/bundlephobia/minzip/easy-dates-picker)
+![npm bundle size](https://img.shields.io/bundlephobia/min/easy-dates-picker)
+![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/sanypockets/easy-dates-picker/ci)
![GitHub issues](https://img.shields.io/github/issues/sandypockets/easy-dates-picker)
![GitHub pull requests](https://img.shields.io/github/issues-pr/sandypockets/easy-dates-picker)
+![npm](https://img.shields.io/npm/dt/easy-dates-picker)
+![npm](https://img.shields.io/npm/dw/easy-dates-picker)
![NPM](https://img.shields.io/npm/l/easy-dates-picker)
Is this the right library for me?
@@ -13,10 +16,10 @@ Is this the right library for me?
- You need both single and/or range date selection.
- You need a localized date picker with 10 languages included.
- You need a date picker that can automatically detect and display the correct language.
-- Accessibility and cross-browser compatibility are important for your project.
+- You need an accessible, fully keyboard navigable, cross-browser compatible date picker.
- You need a date picker that can be easily styled beyond the defaults.
-- Your project does not use a virtual DOM.
- You need a date picker that's lightning fast to set up.
+- Your project does not use a virtual DOM.
## Getting started
Our Date Picker is as quick to set up as it is lightweight. Follow the directions and example below to learn how to integrate `easy-dates-picker` in your project.
@@ -53,7 +56,7 @@ The `options` object is used to pass additional arguments to the picker on initi
const options = {
mode: 'single', // 'single' or 'range'
onSelect: yourDatePickerCallback, // Callback triggered a date or date range is selected
- blockedDays: [], // An array of week day indexes that won't be selectable (0=sunday, 1=monday, etc)
+ blockedDays: [0, 6], // Array of week day indexes that won't be selectable. Example blocks Sat and Sun (0=sunday, 1=monday, etc)
showDayNames: true, // Display the day name at the top of the calendar
textInputEnabled: true, // Display an input field with the selected date. The calendar becomes visible when clicking the input
darkMode: false, // Use light or dark colour scheme
@@ -63,7 +66,21 @@ const options = {
};
```
-Of these options, only `onSelect` is required. The others will fallback to defaults if not provided.
+Of these options, only `onSelect` is required. For other options not provided, they fallback to these defaults:
+
+```javascript
+const options = {
+ mode: 'single',
+ onSelect: yourDatePickerCallback, // Required, there is no fallback
+ blockedDays: [],
+ showDayNames: true,
+ textInputEnabled: false,
+ darkMode: false,
+ language: 'en',
+ usePageLanguage: false,
+ usePageLanguageFallback: 'en'
+};
+```
#### `onSelect` callback
The `options` object requires that you provide a callback function. The callback will run when the user picks a new date. The callback has access to `startDate` and (if using a range) `endDate`.
@@ -153,7 +170,7 @@ To contribute code:
3. Send a pull request from each feature branch to the main branch.
4. Please adhere to the existing code style and conventions.
-The project uses Prettier to manage code style. Before committing your code, please be sure to format your code by running `npm run prettier`
+* The project uses Prettier to manage code style. Before committing your code, please be sure to format your code by running `npm run prettier`
## License
MIT. See [LICENSE.md](./LICENSE.md)
\ No newline at end of file
diff --git a/dist/datepicker.bundle.js b/dist/datepicker.bundle.js
index bad5432..0b7d5c4 100644
--- a/dist/datepicker.bundle.js
+++ b/dist/datepicker.bundle.js
@@ -1 +1 @@
-!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.DatePicker=t():e.DatePicker=t()}(this,(()=>(()=>{"use strict";var e={d:(t,n)=>{for(var a in n)e.o(n,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:n[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};function n(e,t,n){var a=e.querySelector(".prev-month"),i=e.querySelector(".next-month"),r=e.querySelector(".datepicker-calendar-container"),o=e.querySelector(".datepicker-days");a.removeEventListener("click",a.listener),i.removeEventListener("click",i.listener),o.removeEventListener("click",o.listener),document.removeEventListener("click",document.listener),a.listener=function(e){e.stopPropagation(),t(-1)},i.listener=function(e){e.stopPropagation(),t(1)},o.listener=function(e){var t=e.target.closest(".datepicker-day");if(t&&!t.classList.contains("blocked")){var a=parseInt(t.getAttribute("data-day"),10),i=parseInt(t.getAttribute("data-month"),10);n(a,i)}},document.listener=function(t){r.contains(t.target)||e.querySelector(".datepicker-input").contains(t.target)||(r.style.display="none")},a.addEventListener("click",a.listener),i.addEventListener("click",i.listener),o.addEventListener("click",o.listener),document.addEventListener("click",document.listener)}e.d(t,{default:()=>u});var a={en:"Choose a date",fr:"Choisissez une date",es:"Elige una fecha",de:"Wählen Sie ein Datum",it:"Scegli una data",nl:"Kies een datum",pt:"Escolha uma data",ja:"日付を選択","zh-CN":"选择日期","zh-TW":"選擇日期",ru:"Выберите дату"},i={en:{prevMonth:"Go to previous month",nextMonth:"Go to next month"},fr:{prevMonth:"Aller au mois précédent",nextMonth:"Aller au mois suivant"},es:{prevMonth:"Ir al mes anterior",nextMonth:"Ir al siguiente mes"},de:{prevMonth:"Gehe zum vorherigen Monat",nextMonth:"Gehe zum nächsten Monat"},it:{prevMonth:"Vai al mese precedente",nextMonth:"Vai al mese successivo"},nl:{prevMonth:"Ga naar de vorige maand",nextMonth:"Ga naar de volgende maand"},pt:{prevMonth:"Ir para o mês anterior",nextMonth:"Ir para o próximo mês"},ja:{prevMonth:"前の月へ",nextMonth:"次の月へ"},"zh-CN":{prevMonth:"转到上一个月",nextMonth:"转到下一个月"},"zh-TW":{prevMonth:"轉到上一個月",nextMonth:"轉到下一個月"},ru:{prevMonth:"Перейти к предыдущему месяцу",nextMonth:"Перейти к следующему месяцу"}},r={en:["January","February","March","April","May","June","July","August","September","October","November","December"],fr:["Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre"],es:["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"],de:["Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember"],it:["Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre"],nl:["Januari","Februari","Maart","April","Mei","Juni","Juli","Augustus","September","Oktober","November","December"],pt:["Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro"],ja:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],"zh-CN":["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],"zh-TW":["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],ru:["Январь","Февраль","Март","Апрель","Май","Июнь","Июль","Август","Сентяюрь","Октябрь","Ноябрь","Декабрь"]},o={en:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],fr:["Dim","Lun","Mar","Mer","Jeu","Ven","Sam"],es:["Dom","Lun","Mar","Mié","Jue","Vie","Sáb"],de:["So","Mo","Di","Mi","Do","Fr","Sa"],it:["Dom","Lun","Mar","Mer","Gio","Ven","Sab"],nl:["Zo","Ma","Di","Wo","Do","Vr","Za"],pt:["Dom","Seg","Ter","Qua","Qui","Sex","Sáb"],ja:["日","月","火","水","木","金","土"],"zh-CN":["日","一","二","三","四","五","六"],"zh-TW":["日","一","二","三","四","五","六"],ru:["Вс","Пн","Вт","Ср","Чт","Пт","Сб"]},l={1:"一",2:"二",3:"三",4:"四",5:"五",6:"六",7:"七",8:"八",9:"九",10:"十",11:"十一",12:"十二",13:"十三",14:"十四",15:"十五",16:"十六",17:"十七",18:"十八",19:"十九",20:"二十",21:"二十一",22:"二十二",23:"二十三",24:"二十四",25:"二十五",26:"二十六",27:"二十七",28:"二十八",29:"二十九",30:"三十",31:"三十一"},s={"zh-CN":{1:"一",2:"二",3:"三",4:"四",5:"五",6:"六",7:"七",8:"八",9:"九",10:"十",11:"十一",12:"十二",13:"十三",14:"十四",15:"十五",16:"十六",17:"十七",18:"十八",19:"十九",20:"二十",21:"廿一",22:"廿二",23:"廿三",24:"廿四",25:"廿五",26:"廿六",27:"廿七",28:"廿八",29:"廿九",30:"三十",31:"三十一"},"zh-TW":{1:"一",2:"二",3:"三",4:"四",5:"五",6:"六",7:"七",8:"八",9:"九",10:"十",11:"十一",12:"十二",13:"十三",14:"十四",15:"十五",16:"十六",17:"十七",18:"十八",19:"十九",20:"二十",21:"廿一",22:"廿二",23:"廿三",24:"廿四",25:"廿五",26:"廿六",27:"廿七",28:"廿八",29:"廿九",30:"三十",31:"三十一"}};function c(e){var t=document.documentElement.lang;return["en","fr","es","de","it","nl","pt","ja","zh-CN","zh-TW","ru"].includes(t)?t:e}function d(e,t,n,a,i,r,o){var c=new Date(e,t,n),d=n;"ja"===o.language?d=l[n]:"zh-CN"!==o.language&&"zh-TW"!==o.language||(d=s[o.language][n]);var u=a?"datepicker-day current-month":"datepicker-day";return o.blockedDays.includes(c.getDay())?u+=" blocked":a&&i(c)?u+=" selected":a&&r(c)&&(u+=" in-range"),'
').concat(d,"
")}function u(e,t){var l,s,u,h,g,p,v,m,D,b,y;this.element=document.getElementById(e),this.currentDate=new Date,this.currentDate.setDate(1),this.selectedStartDate=null,this.selectedEndDate=null,this.options={mode:null!==(l=null==t?void 0:t.mode)&&void 0!==l?l:"single",onSelect:null!==(s=null==t?void 0:t.onSelect)&&void 0!==s?s:null,blockedDays:null!==(u=null==t?void 0:t.blockedDays)&&void 0!==u?u:[],showDayNames:null===(h=null==t?void 0:t.showDayNames)||void 0===h||h,textInputEnabled:null!==(g=null==t?void 0:t.textInputEnabled)&&void 0!==g&&g,darkMode:null!==(p=null==t?void 0:t.darkMode)&&void 0!==p&&p,usePageLanguage:null!==(v=null==t?void 0:t.usePageLanguage)&&void 0!==v&&v,usePageLanguageFallback:null!==(m=null==t?void 0:t.usePageLanguageFallback)&&void 0!==m?m:"en",language:null!=t&&t.usePageLanguage?c(null==t?void 0:t.usePageLanguageFallback):null!==(D=null==t?void 0:t.language)&&void 0!==D?D:"en",textInputPlaceholder:null!==(b=null==t?void 0:t.textInputPlaceholder)&&void 0!==b?b:a[null!=t&&t.usePageLanguage?c():null!==(y=null==t?void 0:t.language)&&void 0!==y?y:"en"]},this.init=function(){this.options.darkMode&&this.element.classList.add("dark"),this.render(),n(this.element,this.changeMonth.bind(this),this.handleDayClick.bind(this))},this.render=function(){var e=this;this.element.innerHTML="";var t=document.createElement("input");if(t.value=this.options.textInputPlaceholder,t.type="text",t.className="datepicker-input",t.readOnly=!0,this.selectedStartDate)if("single"===this.options.mode)t.value=this.selectedStartDate.toDateString();else if("range"===this.options.mode){var a=this.selectedStartDate.toDateString();this.selectedEndDate&&(a+=" - ".concat(this.selectedEndDate.toDateString())),t.value=a}this.element.appendChild(t);var l=function(e,t,n,a){var l,s,c=document.createElement("div");return c.className="datepicker-calendar-container",c.style.display=a.textInputEnabled?"none":"block",c.innerHTML='\n \n ').concat(a.showDayNames?function(e){var t=o[e];return''.concat(t.map((function(e){return"
".concat(e,"
")})).join(""),"
")}(a.language):"",'\n \n ').concat(function(e,t,n,a){var i=e.getFullYear(),r=e.getMonth(),o=new Date(i,r,1),l=new Date(i,r,0).getDate(),s=new Date(i,r+1,0).getDate();1===r&&function(e){return e%4==0&&e%100!=0||e%400==0}(i)&&(s=29);for(var c='
',u=o.getDay();u>0;u--)c+=d(i,r-1,l-u+1,!1,t,n,a);for(var h=1;h<=s;h++)c+=d(i,r,h,!0,t,n,a);for(var g=new Date(i,r,s).getDay(),p=1;g<6;p++,g++)c+=d(i,r+1,p,!1,t,n,a);return c+"
"}(e,t,n,a),"\n
\n "),c}(this.currentDate,this.isDateSelected.bind(this),this.isDateInRange.bind(this),this.options);this.element.appendChild(l),this.options.textInputEnabled&&(t.addEventListener("click",(function(){l.style.display="block"})),document.addEventListener("click",(function(t){e.element.contains(t.target)||(l.style.display="none")}))),n(this.element,this.changeMonth.bind(this),this.handleDayClick.bind(this))},this.isDateSelected=function(e){return this.selectedStartDate&&this.selectedStartDate.toDateString()===e.toDateString()||this.selectedEndDate&&this.selectedEndDate.toDateString()===e.toDateString()},this.isDateInRange=function(e){return this.selectedStartDate&&this.selectedEndDate&&e>this.selectedStartDate&ðis.selectedStartDate?(this.selectedEndDate=a,this.element.querySelector(".datepicker-input").value="".concat(this.selectedStartDate.toDateString()," ").concat(void 0!==this.selectedEndDate.toDateString()?"- "+this.selectedEndDate.toDateString():""),this.triggerSelectCallback(),this.render(),this.element.querySelector(".datepicker-calendar-container").style.display="none"):(this.selectedStartDate=a,this.render(),this.element.querySelector(".datepicker-calendar-container").style.display="block")))},this.changeMonth=function(e){var t,n={currentYear:(t=this.currentDate).getFullYear(),currentMonth:t.getMonth(),currentDay:t.getDate()},a=n.currentYear,i=n.currentDay,r=new Date(a,n.currentMonth+e,1),o=new Date(a,r.getMonth()+1,0).getDate();r.setDate(Math.min(i,o)),this.currentDate=r,this.render(),this.element.querySelector(".datepicker-calendar-container").style.display="block"},this.triggerSelectCallback=function(){this.options.onSelect&&this.options.onSelect(this.selectedStartDate,this.selectedEndDate)}}return t.default})()));
\ No newline at end of file
+!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.DatePicker=t():e.DatePicker=t()}(this,(()=>(()=>{"use strict";var e={d:(t,n)=>{for(var a in n)e.o(n,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:n[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};function n(e,t,n){var a=e.querySelector(".prev-month"),i=e.querySelector(".next-month"),r=e.querySelector(".datepicker-calendar-container"),o=e.querySelector(".datepicker-days"),l=e.querySelector(".datepicker-input");a.removeEventListener("click",a.listener),i.removeEventListener("click",i.listener),o.removeEventListener("click",o.listener),o.removeEventListener("keydown",o.listener),l.removeEventListener("keydown",l.listener),document.removeEventListener("click",document.listener),a.listener=function(e){e.stopPropagation(),t(-1)},i.listener=function(e){e.stopPropagation(),t(1)},o.listener=function(e){if("click"===e.type||"keydown"===e.type&&"Enter"===e.key){var t=e.target.closest(".datepicker-day");if(t&&!t.classList.contains("blocked")){var a=parseInt(t.getAttribute("data-day"),10),i=parseInt(t.getAttribute("data-month"),10);n(a,i)}}},l.listener=function(t){if("Enter"===t.key){var n=e.querySelector(".datepicker-calendar-container");n.style.display="block"===n.style.display?"none":"block"}},document.listener=function(t){r.contains(t.target)||e.querySelector(".datepicker-input").contains(t.target)||(r.style.display="none")},a.addEventListener("click",a.listener),i.addEventListener("click",i.listener),o.addEventListener("click",o.listener),o.addEventListener("keydown",o.listener),l.addEventListener("keydown",l.listener),document.addEventListener("click",document.listener)}e.d(t,{default:()=>u});var a={en:"Choose a date",fr:"Choisissez une date",es:"Elige una fecha",de:"Wählen Sie ein Datum",it:"Scegli una data",nl:"Kies een datum",pt:"Escolha uma data",ja:"日付を選択","zh-CN":"选择日期","zh-TW":"選擇日期",ru:"Выберите дату"},i={en:{prevMonth:"Go to previous month",nextMonth:"Go to next month"},fr:{prevMonth:"Aller au mois précédent",nextMonth:"Aller au mois suivant"},es:{prevMonth:"Ir al mes anterior",nextMonth:"Ir al siguiente mes"},de:{prevMonth:"Gehe zum vorherigen Monat",nextMonth:"Gehe zum nächsten Monat"},it:{prevMonth:"Vai al mese precedente",nextMonth:"Vai al mese successivo"},nl:{prevMonth:"Ga naar de vorige maand",nextMonth:"Ga naar de volgende maand"},pt:{prevMonth:"Ir para o mês anterior",nextMonth:"Ir para o próximo mês"},ja:{prevMonth:"前の月へ",nextMonth:"次の月へ"},"zh-CN":{prevMonth:"转到上一个月",nextMonth:"转到下一个月"},"zh-TW":{prevMonth:"轉到上一個月",nextMonth:"轉到下一個月"},ru:{prevMonth:"Перейти к предыдущему месяцу",nextMonth:"Перейти к следующему месяцу"}},r={en:["January","February","March","April","May","June","July","August","September","October","November","December"],fr:["Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre"],es:["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"],de:["Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember"],it:["Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre"],nl:["Januari","Februari","Maart","April","Mei","Juni","Juli","Augustus","September","Oktober","November","December"],pt:["Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro"],ja:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],"zh-CN":["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],"zh-TW":["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],ru:["Январь","Февраль","Март","Апрель","Май","Июнь","Июль","Август","Сентяюрь","Октябрь","Ноябрь","Декабрь"]},o={en:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],fr:["Dim","Lun","Mar","Mer","Jeu","Ven","Sam"],es:["Dom","Lun","Mar","Mié","Jue","Vie","Sáb"],de:["So","Mo","Di","Mi","Do","Fr","Sa"],it:["Dom","Lun","Mar","Mer","Gio","Ven","Sab"],nl:["Zo","Ma","Di","Wo","Do","Vr","Za"],pt:["Dom","Seg","Ter","Qua","Qui","Sex","Sáb"],ja:["日","月","火","水","木","金","土"],"zh-CN":["日","一","二","三","四","五","六"],"zh-TW":["日","一","二","三","四","五","六"],ru:["Вс","Пн","Вт","Ср","Чт","Пт","Сб"]},l={1:"一",2:"二",3:"三",4:"四",5:"五",6:"六",7:"七",8:"八",9:"九",10:"十",11:"十一",12:"十二",13:"十三",14:"十四",15:"十五",16:"十六",17:"十七",18:"十八",19:"十九",20:"二十",21:"二十一",22:"二十二",23:"二十三",24:"二十四",25:"二十五",26:"二十六",27:"二十七",28:"二十八",29:"二十九",30:"三十",31:"三十一"},s={"zh-CN":{1:"一",2:"二",3:"三",4:"四",5:"五",6:"六",7:"七",8:"八",9:"九",10:"十",11:"十一",12:"十二",13:"十三",14:"十四",15:"十五",16:"十六",17:"十七",18:"十八",19:"十九",20:"二十",21:"廿一",22:"廿二",23:"廿三",24:"廿四",25:"廿五",26:"廿六",27:"廿七",28:"廿八",29:"廿九",30:"三十",31:"三十一"},"zh-TW":{1:"一",2:"二",3:"三",4:"四",5:"五",6:"六",7:"七",8:"八",9:"九",10:"十",11:"十一",12:"十二",13:"十三",14:"十四",15:"十五",16:"十六",17:"十七",18:"十八",19:"十九",20:"二十",21:"廿一",22:"廿二",23:"廿三",24:"廿四",25:"廿五",26:"廿六",27:"廿七",28:"廿八",29:"廿九",30:"三十",31:"三十一"}};function c(e){var t=document.documentElement.lang;return["en","fr","es","de","it","nl","pt","ja","zh-CN","zh-TW","ru"].includes(t)?t:e}function d(e,t,n,a,i,r,o){var c=new Date(e,t,n),d=n;"ja"===o.language?d=l[n]:"zh-CN"!==o.language&&"zh-TW"!==o.language||(d=s[o.language][n]);var u=a?"datepicker-day current-month":"datepicker-day";return o.blockedDays.includes(c.getDay())?u+=" blocked":a&&i(c)?u+=" selected":a&&r(c)&&(u+=" in-range"),'').concat(d,"
")}function u(e,t){var l,s,u,h,p,v,g,m,D,y,b;this.element=document.getElementById(e),this.currentDate=new Date,this.currentDate.setDate(1),this.selectedStartDate=null,this.selectedEndDate=null,this.options={mode:null!==(l=null==t?void 0:t.mode)&&void 0!==l?l:"single",onSelect:null!==(s=null==t?void 0:t.onSelect)&&void 0!==s?s:null,blockedDays:null!==(u=null==t?void 0:t.blockedDays)&&void 0!==u?u:[],showDayNames:null===(h=null==t?void 0:t.showDayNames)||void 0===h||h,textInputEnabled:null!==(p=null==t?void 0:t.textInputEnabled)&&void 0!==p&&p,darkMode:null!==(v=null==t?void 0:t.darkMode)&&void 0!==v&&v,usePageLanguage:null!==(g=null==t?void 0:t.usePageLanguage)&&void 0!==g&&g,usePageLanguageFallback:null!==(m=null==t?void 0:t.usePageLanguageFallback)&&void 0!==m?m:"en",language:null!=t&&t.usePageLanguage?c(null==t?void 0:t.usePageLanguageFallback):null!==(D=null==t?void 0:t.language)&&void 0!==D?D:"en",textInputPlaceholder:null!==(y=null==t?void 0:t.textInputPlaceholder)&&void 0!==y?y:a[null!=t&&t.usePageLanguage?c():null!==(b=null==t?void 0:t.language)&&void 0!==b?b:"en"]},this.init=function(){this.options.darkMode&&this.element.classList.add("dark"),this.render(),n(this.element,this.changeMonth.bind(this),this.handleDayClick.bind(this))},this.render=function(){var e=this;this.element.innerHTML="";var t=document.createElement("input");if(t.value=this.options.textInputPlaceholder,t.type="text",t.className="datepicker-input",t.readOnly=!0,this.selectedStartDate)if("single"===this.options.mode)t.value=this.selectedStartDate.toDateString();else if("range"===this.options.mode){var a=this.selectedStartDate.toDateString();this.selectedEndDate&&(a+=" - ".concat(this.selectedEndDate.toDateString())),t.value=a}this.element.appendChild(t);var l=function(e,t,n,a){var l,s,c=document.createElement("div");return c.className="datepicker-calendar-container",c.style.display=a.textInputEnabled?"none":"block",c.innerHTML='\n \n ').concat(a.showDayNames?function(e){var t=o[e];return''.concat(t.map((function(e){return"
".concat(e,"
")})).join(""),"
")}(a.language):"",'\n \n ').concat(function(e,t,n,a){var i=e.getFullYear(),r=e.getMonth(),o=new Date(i,r,1),l=new Date(i,r,0).getDate(),s=new Date(i,r+1,0).getDate();1===r&&function(e){return e%4==0&&e%100!=0||e%400==0}(i)&&(s=29);for(var c='
',u=o.getDay();u>0;u--)c+=d(i,r-1,l-u+1,!1,t,n,a);for(var h=1;h<=s;h++)c+=d(i,r,h,!0,t,n,a);for(var p=new Date(i,r,s).getDay(),v=1;p<6;v++,p++)c+=d(i,r+1,v,!1,t,n,a);return c+"
"}(e,t,n,a),"\n
\n "),c}(this.currentDate,this.isDateSelected.bind(this),this.isDateInRange.bind(this),this.options);this.element.appendChild(l),this.options.textInputEnabled&&(t.addEventListener("click",(function(){l.style.display="block"})),document.addEventListener("click",(function(t){e.element.contains(t.target)||(l.style.display="none")}))),n(this.element,this.changeMonth.bind(this),this.handleDayClick.bind(this))},this.isDateSelected=function(e){return this.selectedStartDate&&this.selectedStartDate.toDateString()===e.toDateString()||this.selectedEndDate&&this.selectedEndDate.toDateString()===e.toDateString()},this.isDateInRange=function(e){return this.selectedStartDate&&this.selectedEndDate&&e>this.selectedStartDate&ðis.selectedStartDate?(this.selectedEndDate=a,this.element.querySelector(".datepicker-input").value="".concat(this.selectedStartDate.toDateString()," ").concat(void 0!==this.selectedEndDate.toDateString()?"- "+this.selectedEndDate.toDateString():""),this.triggerSelectCallback(),this.render(),this.element.querySelector(".datepicker-calendar-container").style.display="none"):(this.selectedStartDate=a,this.render(),this.element.querySelector(".datepicker-calendar-container").style.display="block")))},this.changeMonth=function(e){var t,n={currentYear:(t=this.currentDate).getFullYear(),currentMonth:t.getMonth(),currentDay:t.getDate()},a=n.currentYear,i=n.currentDay,r=new Date(a,n.currentMonth+e,1),o=new Date(a,r.getMonth()+1,0).getDate();r.setDate(Math.min(i,o)),this.currentDate=r,this.render(),this.element.querySelector(".datepicker-calendar-container").style.display="block"},this.triggerSelectCallback=function(){this.options.onSelect&&this.options.onSelect(this.selectedStartDate,this.selectedEndDate)}}return t.default})()));
\ No newline at end of file
diff --git a/package.json b/package.json
index 27a79bd..0398b37 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "easy-dates-picker",
- "version": "0.0.17",
+ "version": "0.0.18",
"description": "A super lightweight vanilla JS date picker",
"main": "dist/datepicker.bundle.js",
"files": [
@@ -9,7 +9,8 @@
"scripts": {
"build": "webpack",
"test": "jest",
- "prettier": "prettier src/ --write && prettier example --write"
+ "prettier": "prettier src/ --write && prettier example --write",
+ "prepare": "npm run prettier && npm run build && git status"
},
"jest": {
"moduleNameMapper": {
diff --git a/src/calendarGenerator.js b/src/calendarGenerator.js
index ad43277..6777f80 100644
--- a/src/calendarGenerator.js
+++ b/src/calendarGenerator.js
@@ -59,5 +59,5 @@ export function generateDayCell(year, month, day, isCurrentMonth, isDateSelected
className += ' in-range';
}
- return `${dayDisplay}
`;
+ return `${dayDisplay}
`;
}
diff --git a/src/eventHandlers.js b/src/eventHandlers.js
index b8db2b1..fcf5f44 100644
--- a/src/eventHandlers.js
+++ b/src/eventHandlers.js
@@ -3,11 +3,14 @@ export function attachEventListeners(element, changeMonth, handleDayClick) {
const nextMonthButton = element.querySelector('.next-month');
const calendarContainer = element.querySelector('.datepicker-calendar-container');
const daysContainer = element.querySelector('.datepicker-days');
+ const input = element.querySelector('.datepicker-input');
// Remove any existing event listeners to prevent duplication
prevMonthButton.removeEventListener('click', prevMonthButton.listener);
nextMonthButton.removeEventListener('click', nextMonthButton.listener);
daysContainer.removeEventListener('click', daysContainer.listener);
+ daysContainer.removeEventListener('keydown', daysContainer.listener);
+ input.removeEventListener('keydown', input.listener);
document.removeEventListener('click', document.listener);
// Define new listeners
@@ -20,11 +23,19 @@ export function attachEventListeners(element, changeMonth, handleDayClick) {
changeMonth(1);
};
daysContainer.listener = event => {
- const target = event.target.closest('.datepicker-day');
- if (target && !target.classList.contains('blocked')) {
- const day = parseInt(target.getAttribute('data-day'), 10);
- const month = parseInt(target.getAttribute('data-month'), 10);
- handleDayClick(day, month);
+ if (event.type === 'click' || (event.type === 'keydown' && event.key === 'Enter')) {
+ const target = event.target.closest('.datepicker-day');
+ if (target && !target.classList.contains('blocked')) {
+ const day = parseInt(target.getAttribute('data-day'), 10);
+ const month = parseInt(target.getAttribute('data-month'), 10);
+ handleDayClick(day, month);
+ }
+ }
+ };
+ input.listener = event => {
+ if (event.key === 'Enter') {
+ const calendarContainer = element.querySelector('.datepicker-calendar-container');
+ calendarContainer.style.display = calendarContainer.style.display === 'block' ? 'none' : 'block';
}
};
document.listener = event => {
@@ -37,5 +48,7 @@ export function attachEventListeners(element, changeMonth, handleDayClick) {
prevMonthButton.addEventListener('click', prevMonthButton.listener);
nextMonthButton.addEventListener('click', nextMonthButton.listener);
daysContainer.addEventListener('click', daysContainer.listener);
+ daysContainer.addEventListener('keydown', daysContainer.listener);
+ input.addEventListener('keydown', input.listener);
document.addEventListener('click', document.listener);
}