Skip to content

Commit

Permalink
Feat: Optionally prevent past dates from being selected
Browse files Browse the repository at this point in the history
  • Loading branch information
sandypockets committed Dec 9, 2023
1 parent 058917a commit 80de6a7
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/datepicker.bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/datepicker.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
}

const options = {
mode: 'single', // 'single' or 'range', defaults to 'single'
mode: 'range', // 'single' or 'range', defaults to 'single'
onSelect: datePickerCallback, // Callback for whenever a date or date range is selected - Required
blockedDays: [0, 6], // Prevent Saturday and Sunday from being selected
showDayNames: true,
textInputEnabled: true, // Show input field, calendar displays on click
darkMode: true,
darkMode: false,
language: 'en', // language ISO code, defaults to en -> ignored if usePageLanguage is true
usePageLanguage: true, // Looks for a lang attribute on the page, and if the language is supported, uses it
usePageLanguageFallback: 'en', // If usePageLanguage is true, and the page language is not supported, use this language
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easy-dates-picker",
"version": "0.0.23",
"version": "0.0.24",
"description": "A super lightweight vanilla JS date picker",
"main": "dist/datepicker.bundle.js",
"files": [
Expand Down
11 changes: 11 additions & 0 deletions src/calendarGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function generateDayNames(language) {
export function generateCalendar(currentDate, isDateSelected, isDateInRange, options) {
const year = currentDate.getFullYear();
const month = currentDate.getMonth();
const currentDay = currentDate.getDate();

const firstDayOfMonth = new Date(year, month, 1);
const lastDayOfLastMonth = new Date(year, month, 0).getDate();
Expand Down Expand Up @@ -63,5 +64,15 @@ export function generateDayCell(year, month, day, isCurrentMonth, isDateSelected
className += ' in-range';
}

const today = new Date();
today.setHours(0, 0, 0, 0); // Set to start of today for fair comparison

if (date.getTime() === today.getTime()) {
className += ' today';
}
if (!options.selectPastDatesEnabled && date < today) {
className += ' past-date';
}

return `<div tabindex="0" class="${className}" data-day="${day}" data-month="${month}"><span>${dayDisplay}</span></div>`;
}
12 changes: 12 additions & 0 deletions src/datePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function DatePicker(elementId, options) {
options?.textInputPlaceholder ??
inputPlaceholderTranslations[options?.usePageLanguage ? getPageLanguage() : options?.language ?? 'en'],
cornerStyle: options?.cornerStyle ?? 'round',
selectPastDatesEnabled: options?.selectPastDatesEnabled ?? false,
};
this.init = function () {
if (this.options.darkMode) this.element.classList.add('dark');
Expand Down Expand Up @@ -142,6 +143,17 @@ export default function DatePicker(elementId, options) {

const clickedDate = new Date(year, month, day);

const today = new Date();
today.setHours(0, 0, 0, 0);

console.log('TODAY: ', today);
console.log('CLICKED DATE: ', clickedDate);

if (!this.options.selectPastDatesEnabled && clickedDate < today) {
console.log('CLICK PREVENTED');
return;
}

if (this.options.mode === 'single') {
this.selectedStartDate = clickedDate;
this.selectedEndDate = null;
Expand Down
4 changes: 4 additions & 0 deletions src/styles/datepicker.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
margin: 0 0 5px 5px;
}

.datepicker-day.past-date {
cursor: not-allowed;
}

.datepicker-day > span {
display: block;
}
Expand Down
8 changes: 8 additions & 0 deletions src/styles/lightmode.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
.datepicker-day {
background-color: #f0f0f0;
}
.datepicker-day.past-date,
.datepicker-day.past-date.blocked {
background-color: transparent;
}

.datepicker-day.today {
box-shadow: inset 0 0 0 2px #4caf50;
}

.datepicker-day:hover {
background-color: #e0e0e0;
Expand Down

0 comments on commit 80de6a7

Please sign in to comment.