Skip to content

Commit

Permalink
refactor: minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlewis92 committed Aug 21, 2024
1 parent a61d2e6 commit 95ecb38
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions projects/angularx-flatpickr/src/lib/flatpickr.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
OnDestroy,
forwardRef,
HostListener,
Renderer2, PLATFORM_ID, Inject
Renderer2,
PLATFORM_ID,
Inject,
} from '@angular/core';
import {
FlatpickrDefaults,
Expand Down Expand Up @@ -88,7 +90,6 @@ export class FlatpickrDirective
*/
@Input() allowInput: boolean;


/**
* Allows the preloading of an invalid date. When disabled, the field will be cleared if the provided date is invalid
*/
Expand Down Expand Up @@ -346,7 +347,6 @@ export class FlatpickrDirective

private isDisabled = false;
private initialValue: any;
private isBrowser: boolean;

onChangeFn: (value: any) => void = () => {};

Expand All @@ -357,10 +357,8 @@ export class FlatpickrDirective
private elm: ElementRef,
private defaults: FlatpickrDefaults,
private renderer: Renderer2,
@Inject(PLATFORM_ID) platformId: Object
) {
this.isBrowser = isPlatformBrowser(platformId);
}
@Inject(PLATFORM_ID) private platformId: Object
) {}

ngAfterViewInit(): void {
const options: any = {
Expand Down Expand Up @@ -477,7 +475,7 @@ export class FlatpickrDirective
delete options.enable;
}

if (this.isBrowser) {
if (isPlatformBrowser(this.platformId)) {
this.instance = flatpickr(
this.elm.nativeElement,
options
Expand All @@ -487,15 +485,15 @@ export class FlatpickrDirective
}

ngOnChanges(changes: SimpleChanges): void {
if (this.instance && this.isBrowser) {
if (this.instance) {
Object.keys(changes).forEach((inputKey) => {
this.instance.set(inputKey as any, (this as any)[inputKey]);
});
}
}

ngOnDestroy(): void {
if (this.instance && this.isBrowser) {
if (this.instance) {
this.instance.destroy();
}
}
Expand All @@ -506,7 +504,7 @@ export class FlatpickrDirective
convertedValue = [value.from, value.to];
}

if (this.instance && this.isBrowser) {
if (this.instance) {
this.instance.setDate(convertedValue);
} else {
// flatpickr hasn't been initialised yet, store the value for later use
Expand Down

0 comments on commit 95ecb38

Please sign in to comment.