Skip to content

Commit d075f5e

Browse files
committed
refactor(Datepicker) Implemented multiple global for handling single day or multi day date selection; Implemented setDateFromInput for logical navigation within the calendar by mouse/keypress interaction on input change, fixes issue where calendar only navigated to latest changed field; Refactored handleCalendarClick, split up single date click and date range click for the sake of code readability and simplicity; Updated JSDoc, removed unused parameter; Deprecated _setToStartOfDay (function has no effect); #360
1 parent f7c2858 commit d075f5e

1 file changed

Lines changed: 40 additions & 25 deletions

File tree

src/datepicker.ts

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ export class Datepicker extends Component<DatepickerOptions> {
261261
id: string;
262262
/** If the picker is open. */
263263
isOpen: boolean;
264+
multiple: boolean = false;
264265
modal: Modal;
265266
calendarEl: HTMLElement;
266267
/** CLEAR button instance. */
@@ -324,6 +325,7 @@ export class Datepicker extends Component<DatepickerOptions> {
324325
this.gotoDate(new Date());
325326
}
326327
if (this.options.isDateRange) {
328+
this.multiple = true;
327329
let defEndDate = this.options.defaultEndDate;
328330
if(Datepicker._isDate(defEndDate))
329331
{
@@ -370,6 +372,9 @@ export class Datepicker extends Component<DatepickerOptions> {
370372
return day === 0 || day === 6;
371373
}
372374

375+
/**
376+
* @deprecated since it does nothing as it's only updating the encapsulated variable (no return/global param change)
377+
*/
373378
static _setToStartOfDay(date) {
374379
if (Datepicker._isDate(date)) date.setHours(0, 0, 0, 0);
375380
}
@@ -486,6 +491,14 @@ export class Datepicker extends Component<DatepickerOptions> {
486491
return formattedDate;
487492
}
488493

494+
/**
495+
* Sets date from input field.
496+
*/
497+
setDateFromInput(el: HTMLInputElement) {
498+
const date = new Date(Date.parse(el.value));
499+
this.setDate(date, false, el == this.endDateEl);
500+
}
501+
489502
/**
490503
* Set a date on the datepicker.
491504
* @param date Date to set on the datepicker.
@@ -574,7 +587,6 @@ export class Datepicker extends Component<DatepickerOptions> {
574587
/**
575588
* Change date view to a specific date on the datepicker.
576589
* @param date Date to show on the datepicker.
577-
* @param endDate
578590
*/
579591
gotoDate(date: Date) {
580592
let newCalendar = true;
@@ -1047,12 +1059,14 @@ export class Datepicker extends Component<DatepickerOptions> {
10471059
e.preventDefault()
10481060
}
10491061
this.open();
1062+
this.setDateFromInput(e.target as HTMLInputElement);
10501063
this.gotoDate(<HTMLElement>(e.target) === this.el ? this.date : this.endDate);
10511064
}
10521065

10531066
_handleInputKeydown = (e: KeyboardEvent) => {
10541067
if (Utils.keys.ENTER.includes(e.key)) {
10551068
e.preventDefault();
1069+
this.setDateFromInput(e.target as HTMLInputElement);
10561070
this.open();
10571071
}
10581072
}
@@ -1072,34 +1086,17 @@ export class Datepicker extends Component<DatepickerOptions> {
10721086
e.target.getAttribute('data-day')
10731087
);
10741088

1075-
if (this.endDate == null || !Datepicker._compareDates(selectedDate, this.endDate)) {
1076-
if (
1077-
Datepicker._isDate(this.date) &&
1078-
this.options.isDateRange &&
1079-
Datepicker._comparePastDate(selectedDate, this.date)
1080-
) {
1081-
return;
1082-
}
1083-
1084-
this.setDate(
1085-
selectedDate,
1086-
false,
1087-
Datepicker._isDate(this.date) && this.options.isDateRange
1088-
);
1089-
1090-
if (this.options.autoClose) {
1091-
this._finishSelection();
1092-
}
1093-
1094-
return;
1089+
if (!this.multiple) {
1090+
this._handleSingleDateCalendarClick(selectedDate);
10951091
}
10961092

1097-
if (!this.options.isDateRange) {
1098-
return;
1093+
if (this.options.isDateRange) {
1094+
this._handleDateRangeCalendarClick(selectedDate);
10991095
}
11001096

1101-
this._clearDates();
1102-
this.draw();
1097+
if (this.options.autoClose) {
1098+
this._finishSelection();
1099+
}
11031100
}
11041101
else if (target.closest('.month-prev')) {
11051102
this.prevMonth();
@@ -1110,6 +1107,24 @@ export class Datepicker extends Component<DatepickerOptions> {
11101107
}
11111108
}
11121109

1110+
_handleSingleDateCalendarClick = (date: Date) => {
1111+
this.setDate(date);
1112+
}
1113+
1114+
_handleDateRangeCalendarClick = (date: Date) => {
1115+
if (this.endDate == null || !Datepicker._compareDates(date, this.endDate)) {
1116+
if (Datepicker._isDate(this.date) && Datepicker._comparePastDate(date, this.date)) {
1117+
return;
1118+
}
1119+
1120+
this.setDate(date, false, Datepicker._isDate(this.date));
1121+
return;
1122+
}
1123+
1124+
this._clearDates();
1125+
this.draw();
1126+
}
1127+
11131128
_handleClearClick = () => {
11141129
this._clearDates();
11151130
this.setInputValues();

0 commit comments

Comments
 (0)