|
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"; |
2 | 9 | import { FlatpickrOptions } from "ng2-flatpickr"; |
3 | 10 | import rangePlugin from "flatpickr/dist/plugins/rangePlugin"; |
4 | 11 |
|
| 12 | + |
5 | 13 | @Component({ |
6 | 14 | selector: "ibm-date-picker", |
7 | 15 | template: ` |
@@ -45,22 +53,18 @@ import rangePlugin from "flatpickr/dist/plugins/rangePlugin"; |
45 | 53 | </div> |
46 | 54 | ` |
47 | 55 | }) |
48 | | -export class DatePicker { |
| 56 | +export class DatePicker implements OnDestroy { |
49 | 57 | private static datePickerCount = 0; |
50 | 58 |
|
51 | 59 | /** |
52 | 60 | * Select calendar range mode |
53 | | - * |
54 | | - * @memberof Datepicker |
55 | 61 | */ |
56 | 62 | @Input() range: boolean; |
57 | 63 |
|
58 | 64 | /** |
59 | 65 | * Format of date |
60 | 66 | * |
61 | 67 | * For reference: https://flatpickr.js.org/formatting/ |
62 | | - * |
63 | | - * @memberof Datepicker |
64 | 68 | */ |
65 | 69 | @Input() dateFormat = "m/d/Y"; |
66 | 70 |
|
@@ -95,36 +99,59 @@ export class DatePicker { |
95 | 99 | value: this.value |
96 | 100 | }; |
97 | 101 |
|
| 102 | + constructor(protected elementRef: ElementRef) { } |
| 103 | + |
98 | 104 | doSelect(selectedValue) { |
99 | 105 | this.valueChange.emit(selectedValue); |
100 | 106 | } |
101 | 107 |
|
102 | 108 | 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"); |
106 | 116 | const weekdayContainer = document.querySelectorAll(".flatpickr-weekday"); |
107 | | - const daysContainer = document.querySelector(".flatpickr-days"); |
| 117 | + const daysContainer = document.querySelectorAll(".flatpickr-days"); |
108 | 118 | const dayContainer = document.querySelectorAll(".flatpickr-day"); |
109 | 119 |
|
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"); |
119 | 139 | }); |
120 | 140 |
|
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"); |
127 | 148 | } |
128 | 149 | }); |
129 | 150 | } |
| 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 | + } |
130 | 157 | } |
0 commit comments