MCPcopy
hub / github.com/flatpickr/flatpickr / selectDate

Function selectDate

src/index.ts:2319–2414  ·  view source on GitHub ↗
(e: MouseEvent | KeyboardEvent)

Source from the content-addressed store, hash-verified

2317 }
2318
2319 function selectDate(e: MouseEvent | KeyboardEvent) {
2320 e.preventDefault();
2321 e.stopPropagation();
2322
2323 const isSelectable = (day: Element) =>
2324 day.classList &&
2325 day.classList.contains("flatpickr-day") &&
2326 !day.classList.contains("flatpickr-disabled") &&
2327 !day.classList.contains("notAllowed");
2328
2329 const t = findParent(getEventTarget(e) as Element, isSelectable);
2330
2331 if (t === undefined) return;
2332
2333 const target = t as DayElement;
2334
2335 const selectedDate = (self.latestSelectedDateObj = new Date(
2336 target.dateObj.getTime()
2337 ));
2338
2339 const shouldChangeMonth =
2340 (selectedDate.getMonth() < self.currentMonth ||
2341 selectedDate.getMonth() >
2342 self.currentMonth + self.config.showMonths - 1) &&
2343 self.config.mode !== "range";
2344
2345 self.selectedDateElem = target;
2346
2347 if (self.config.mode === "single") self.selectedDates = [selectedDate];
2348 else if (self.config.mode === "multiple") {
2349 const selectedIndex = isDateSelected(selectedDate);
2350
2351 if (selectedIndex) self.selectedDates.splice(parseInt(selectedIndex), 1);
2352 else self.selectedDates.push(selectedDate);
2353 } else if (self.config.mode === "range") {
2354 if (self.selectedDates.length === 2) {
2355 self.clear(false, false);
2356 }
2357 self.latestSelectedDateObj = selectedDate;
2358 self.selectedDates.push(selectedDate);
2359
2360 // unless selecting same date twice, sort ascendingly
2361 if (compareDates(selectedDate, self.selectedDates[0], true) !== 0)
2362 self.selectedDates.sort((a, b) => a.getTime() - b.getTime());
2363 }
2364
2365 setHoursFromInputs();
2366
2367 if (shouldChangeMonth) {
2368 const isNewYear = self.currentYear !== selectedDate.getFullYear();
2369 self.currentYear = selectedDate.getFullYear();
2370 self.currentMonth = selectedDate.getMonth();
2371
2372 if (isNewYear) {
2373 triggerEvent("onYearChange");
2374 buildMonthSwitch();
2375 }
2376

Callers 1

onKeyDownFunction · 0.85

Calls 13

findParentFunction · 0.90
getEventTargetFunction · 0.90
compareDatesFunction · 0.90
isDateSelectedFunction · 0.85
setHoursFromInputsFunction · 0.85
triggerEventFunction · 0.85
buildMonthSwitchFunction · 0.85
buildDaysFunction · 0.85
updateValueFunction · 0.85
focusOnDayElemFunction · 0.85
focusAndCloseFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…