2025-12-04 21:07:18 +03:30
|
|
|
import { Maybe } from '@/core';
|
2026-05-28 16:37:58 +03:30
|
|
|
import { nowJalali } from '@/utils';
|
2025-12-04 21:07:18 +03:30
|
|
|
import { CommonModule } from '@angular/common';
|
|
|
|
|
import {
|
2026-05-23 18:09:44 +03:30
|
|
|
AfterViewInit,
|
2025-12-04 21:07:18 +03:30
|
|
|
Component,
|
|
|
|
|
ElementRef,
|
|
|
|
|
EventEmitter,
|
|
|
|
|
Input,
|
2026-05-23 18:09:44 +03:30
|
|
|
OnDestroy,
|
2025-12-04 21:07:18 +03:30
|
|
|
Output,
|
|
|
|
|
ViewChild,
|
|
|
|
|
} from '@angular/core';
|
|
|
|
|
import { FormControl, ReactiveFormsModule } from '@angular/forms';
|
2025-12-15 18:00:45 +03:30
|
|
|
import dayjs from 'dayjs';
|
2025-12-04 21:07:18 +03:30
|
|
|
import flatpickr from 'flatpickr-wrap';
|
2026-05-23 18:09:44 +03:30
|
|
|
import { Persian } from 'flatpickr-wrap/dist/l10n/fa';
|
2026-04-16 22:19:46 +03:30
|
|
|
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',
|
|
|
|
|
})
|
2026-05-23 18:09:44 +03:30
|
|
|
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> = {};
|
2025-12-10 16:54:25 +03:30
|
|
|
@Input() showLabel: boolean = true;
|
|
|
|
|
@Input() showErrors: boolean = true;
|
2026-04-06 13:31:30 +03:30
|
|
|
@Input() hint?: string;
|
2026-04-16 22:19:46 +03:30
|
|
|
@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;
|
|
|
|
|
|
2026-05-23 18:09:44 +03:30
|
|
|
ngAfterViewInit(): void {
|
2025-12-04 21:07:18 +03:30
|
|
|
this.fpInstance = flatpickr(this.wrapperRef.nativeElement, {
|
2026-05-23 18:09:44 +03:30
|
|
|
wrap: true,
|
2025-12-04 21:07:18 +03:30
|
|
|
...this.options,
|
2026-05-23 18:09:44 +03:30
|
|
|
locale: Persian,
|
|
|
|
|
disableMobile: true,
|
|
|
|
|
clickOpens: !this.disabled,
|
2026-04-16 22:19:46 +03:30
|
|
|
minDate: this.minDate,
|
|
|
|
|
maxDate: this.maxDate,
|
2026-05-23 18:09:44 +03:30
|
|
|
now: nowJalali(),
|
|
|
|
|
altInputClass: 'flatpicker-field w-full',
|
|
|
|
|
altInput: true,
|
2025-12-23 20:35:20 +03:30
|
|
|
dateFormat: 'Y-m-d',
|
2026-05-23 18:09:44 +03:30
|
|
|
altFormat: 'Y/m/d',
|
|
|
|
|
|
2025-12-04 21:07:18 +03:30
|
|
|
onChange: (selectedDates: Date[], dateStr: string) => {
|
|
|
|
|
this.valueChange.emit(dateStr);
|
2025-12-23 20:35:20 +03:30
|
|
|
if (this.control) this.control.setValue(dayjs(dateStr).toISOString());
|
2025-12-04 21:07:18 +03:30
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-05-23 18:09:44 +03:30
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
|
if (this.fpInstance?.destroy) {
|
|
|
|
|
this.fpInstance.destroy();
|
|
|
|
|
this.fpInstance = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-04 21:07:18 +03:30
|
|
|
}
|