23 lines
669 B
TypeScript
23 lines
669 B
TypeScript
|
|
import { formatJalali } from '@/utils';
|
||
|
|
import { Directive, ElementRef, Input, OnChanges, Renderer2, SimpleChanges } from '@angular/core';
|
||
|
|
|
||
|
|
@Directive({
|
||
|
|
selector: '[jalaliDate]',
|
||
|
|
})
|
||
|
|
export class JalaliDateDirective implements OnChanges {
|
||
|
|
@Input('jalaliDate') dateValue!: string | number | Date;
|
||
|
|
@Input() jalaliFormat: string = 'YYYY/MM/DD';
|
||
|
|
|
||
|
|
constructor(
|
||
|
|
private el: ElementRef,
|
||
|
|
private renderer: Renderer2,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
ngOnChanges(changes: SimpleChanges): void {
|
||
|
|
if (this.dateValue) {
|
||
|
|
const formatted = formatJalali(this.dateValue, this.jalaliFormat);
|
||
|
|
this.renderer.setProperty(this.el.nativeElement, 'innerText', formatted);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|