Skip to content

Commit ec9cf11

Browse files
authored
Merge pull request #466 from cal-smith/issue#449
fix(datepicker): fixes issue#449
2 parents 57ad4f3 + 0fb9936 commit ec9cf11

File tree

1 file changed

+52
-25
lines changed

1 file changed

+52
-25
lines changed

src/datepicker/datepicker.component.ts

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { Component, Input, Output, EventEmitter } from "@angular/core";
1+
import {
2+
Component,
3+
Input,
4+
Output,
5+
EventEmitter,
6+
ElementRef,
7+
OnDestroy
8+
} from "@angular/core";
29
import { FlatpickrOptions } from "ng2-flatpickr";
310
import rangePlugin from "flatpickr/dist/plugins/rangePlugin";
411

12+
513
@Component({
614
selector: "ibm-date-picker",
715
template: `
@@ -45,22 +53,18 @@ import rangePlugin from "flatpickr/dist/plugins/rangePlugin";
4553
</div>
4654
`
4755
})
48-
export class DatePicker {
56+
export class DatePicker implements OnDestroy {
4957
private static datePickerCount = 0;
5058

5159
/**
5260
* Select calendar range mode
53-
*
54-
* @memberof Datepicker
5561
*/
5662
@Input() range: boolean;
5763

5864
/**
5965
* Format of date
6066
*
6167
* For reference: https://flatpickr.js.org/formatting/
62-
*
63-
* @memberof Datepicker
6468
*/
6569
@Input() dateFormat = "m/d/Y";
6670

@@ -95,36 +99,59 @@ export class DatePicker {
9599
value: this.value
96100
};
97101

102+
constructor(protected elementRef: ElementRef) { }
103+
98104
doSelect(selectedValue) {
99105
this.valueChange.emit(selectedValue);
100106
}
101107

102108
updateClassNames() {
103-
const calendarContainer = document.querySelector(".flatpickr-calendar");
104-
const monthContainer = document.querySelector(".flatpickr-month");
105-
const weekdaysContainer = document.querySelector(".flatpickr-weekdays");
109+
const ng2FlatPickrElement = this.elementRef.nativeElement.querySelector(".ng2-flatpickr-input-container");
110+
ng2FlatPickrElement._flatpickr._positionCalendar();
111+
112+
// get all the possible flatpickrs in the document - we need to add classes to (potentially) all of them
113+
const calendarContainer = document.querySelectorAll(".flatpickr-calendar");
114+
const monthContainer = document.querySelectorAll(".flatpickr-month");
115+
const weekdaysContainer = document.querySelectorAll(".flatpickr-weekdays");
106116
const weekdayContainer = document.querySelectorAll(".flatpickr-weekday");
107-
const daysContainer = document.querySelector(".flatpickr-days");
117+
const daysContainer = document.querySelectorAll(".flatpickr-days");
108118
const dayContainer = document.querySelectorAll(".flatpickr-day");
109119

110-
calendarContainer.classList.add("bx--date-picker__calendar");
111-
monthContainer.classList.add("bx--date-picker__month");
112-
weekdaysContainer.classList.add("bx--date-picker__weekdays");
113-
daysContainer.classList.add("bx--date-picker__days");
114-
115-
Array.from(weekdayContainer).forEach(item => {
116-
const currentItem = item;
117-
currentItem.innerHTML = currentItem.innerHTML.replace(/\s+/g, "");
118-
currentItem.classList.add("bx--date-picker__weekday");
120+
// add classes to lists of elements
121+
const addClassIfNotExists = (classname: string, elementList: NodeListOf<Element>) => {
122+
Array.from(elementList).forEach(element => {
123+
if (!element.classList.contains(classname)) {
124+
element.classList.add(classname);
125+
}
126+
});
127+
};
128+
129+
// add classes (but only if they don't exist, small perf win)
130+
addClassIfNotExists("bx--date-picker__calendar", calendarContainer);
131+
addClassIfNotExists("bx--date-picker__month", monthContainer);
132+
addClassIfNotExists("bx--date-picker__weekdays", weekdaysContainer);
133+
addClassIfNotExists("bx--date-picker__days", daysContainer);
134+
135+
// add weekday classes and format the text
136+
Array.from(weekdayContainer).forEach(element => {
137+
element.innerHTML = element.innerHTML.replace(/\s+/g, "");
138+
element.classList.add("bx--date-picker__weekday");
119139
});
120140

121-
Array.from(dayContainer).forEach(item => {
122-
item.classList.add("bx--date-picker__day");
123-
if (item.classList.contains("today") && this.value.length > 0) {
124-
item.classList.add("no-border");
125-
} else if (item.classList.contains("today") && this.value.length === 0) {
126-
item.classList.remove("no-border");
141+
// add day classes and special case the "today" element based on `this.value`
142+
Array.from(dayContainer).forEach(element => {
143+
element.classList.add("bx--date-picker__day");
144+
if (element.classList.contains("today") && this.value.length > 0) {
145+
element.classList.add("no-border");
146+
} else if (element.classList.contains("today") && this.value.length === 0) {
147+
element.classList.remove("no-border");
127148
}
128149
});
129150
}
151+
152+
ngOnDestroy() {
153+
// clean up our flatpickr element - needed because the wrapper doesn't handle this
154+
const ng2FlatPickrElement = this.elementRef.nativeElement.querySelector(".ng2-flatpickr-input-container");
155+
ng2FlatPickrElement._flatpickr.destroy();
156+
}
130157
}

0 commit comments

Comments
 (0)