Files
psp_panel/src/app/uikit/datepicker/datepicker.component.ts
T

78 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-12-04 21:07:18 +03:30
import { Maybe } from '@/core';
import { jalaliToGregorian, nowJalali } from '@/utils';
2025-12-04 21:07:18 +03:30
import { CommonModule } from '@angular/common';
import {
AfterViewInit,
2025-12-04 21:07:18 +03:30
Component,
ElementRef,
EventEmitter,
Input,
OnDestroy,
2025-12-04 21:07:18 +03:30
Output,
ViewChild,
} from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import dayjs from 'dayjs';
2025-12-04 21:07:18 +03:30
import flatpickr from 'flatpickr-wrap';
import { Persian } from 'flatpickr-wrap/dist/l10n/fa';
import { BaseOptions, DateOption } from 'flatpickr-wrap/dist/types/options';
2025-12-04 21:07:18 +03:30
import { InputTextModule } from 'primeng/inputtext';
import { UikitFieldComponent } from '../uikit-field.component';
@Component({
selector: 'uikit-datepicker',
standalone: true,
imports: [CommonModule, ReactiveFormsModule, InputTextModule, UikitFieldComponent],
templateUrl: './datepicker.component.html',
})
export class UikitFlatpickrJalaliComponent implements AfterViewInit, OnDestroy {
2025-12-04 21:07:18 +03:30
@Input() control?: FormControl<Maybe<string>>;
@Input() placeholder = 'انتخاب تاریخ';
@Input() label = 'تاریخ';
@Input() disabled = false;
@Input() name: string = 'datepicker';
@Input() options: Partial<BaseOptions> = {};
@Input() showLabel: boolean = true;
@Input() showErrors: boolean = true;
2026-04-06 13:31:30 +03:30
@Input() hint?: string;
@Input() minDate?: DateOption;
@Input() maxDate?: DateOption;
2025-12-04 21:07:18 +03:30
@Output() valueChange = new EventEmitter<string>();
@ViewChild('wrapper', { static: true }) wrapperRef!: ElementRef<HTMLElement>;
private fpInstance: any | null = null;
ngAfterViewInit(): void {
2025-12-04 21:07:18 +03:30
this.fpInstance = flatpickr(this.wrapperRef.nativeElement, {
wrap: true,
2025-12-04 21:07:18 +03:30
...this.options,
locale: Persian,
disableMobile: true,
clickOpens: !this.disabled,
minDate: this.minDate,
maxDate: this.maxDate,
now: nowJalali(),
altInputClass: 'flatpicker-field w-full',
altInput: true,
dateFormat: 'Y-m-d',
altFormat: 'Y/m/d',
2025-12-04 21:07:18 +03:30
onChange: (selectedDates: Date[], dateStr: string) => {
console.log('dateStr', jalaliToGregorian(dateStr));
2025-12-04 21:07:18 +03:30
this.valueChange.emit(dateStr);
if (this.control) this.control.setValue(dayjs(dateStr).toISOString());
2025-12-04 21:07:18 +03:30
},
});
}
ngOnDestroy(): void {
if (this.fpInstance?.destroy) {
this.fpInstance.destroy();
this.fpInstance = null;
}
}
2025-12-04 21:07:18 +03:30
}