Files
psp_panel/src/app/shared/components/key-value.component/key-value.component.ts
T

100 lines
2.8 KiB
TypeScript
Raw Normal View History

2026-03-29 18:07:10 +03:30
import { formatDurationToText, formatJalali, JALALI_DATE_FORMATS } from '@/utils';
import priceMaskUtils from '@/utils/price-mask.utils';
2025-12-04 21:07:18 +03:30
import { CommonModule } from '@angular/common';
2026-03-29 18:07:10 +03:30
import { Component, computed, Input, signal } from '@angular/core';
import { Tag } from 'primeng/tag';
import { TDataType } from '../pageDataList/page-data-list.component';
2025-12-04 21:07:18 +03:30
@Component({
selector: 'app-key-value',
standalone: true,
imports: [CommonModule, Tag],
2025-12-04 21:07:18 +03:30
templateUrl: './key-value.component.html',
host: {
class: 'w-full block',
},
})
export class KeyValueComponent {
@Input() label!: string;
2026-03-29 18:07:10 +03:30
@Input() value?: string | boolean | number;
@Input() hint?: string;
@Input() alignment?: 'start' | 'center' | 'end' = 'start';
@Input() type: TDataType = 'simple';
2026-04-23 01:22:44 +03:30
2026-03-29 18:07:10 +03:30
@Input() trueValueToShow?: string;
@Input() falseValueToShow?: string;
@Input() variant?: 'tag' | 'text';
tagSeverity = computed(() => (this.value ? 'success' : 'danger'));
2026-03-29 18:07:10 +03:30
valueToShow = signal(this.setValueToShow());
ngOnChanges() {
this.valueToShow.set(this.setValueToShow());
}
setValueToShow() {
let value = this.value;
if (this.type !== 'simple') {
if (this.value) {
if (this.trueValueToShow) {
value = this.trueValueToShow;
} else {
switch (this.type) {
case 'date':
if (typeof this.value === 'boolean') return '-';
// @ts-ignore
return formatJalali(this.value);
case 'dateTime':
if (typeof this.value === 'boolean') return '-';
// @ts-ignore
return formatJalali(this.value, JALALI_DATE_FORMATS.NUMERIC_WITH_TIME);
case 'duration':
return formatDurationToText(this.value as string);
case 'price':
return priceMaskUtils.formatWithCurrency(this.value as number);
case 'active':
value = 'فعال';
break;
case 'boolean':
value = 'بله';
break;
case 'has':
value = 'دارد';
break;
}
}
} else {
if (this.falseValueToShow) {
value = this.falseValueToShow;
} else {
switch (this.type) {
case 'active':
value = 'غیر فعال';
break;
case 'boolean':
value = 'خیر';
break;
case 'has':
value = 'ندارد';
break;
2026-05-04 20:02:10 +03:30
case 'number':
value = '0';
break;
2026-03-29 18:07:10 +03:30
}
}
}
}
switch (this.type) {
case 'simple':
2026-05-26 12:06:43 +03:30
return this.value || '-----';
2026-03-29 18:07:10 +03:30
}
return value;
}
valueType = computed(() =>
2026-05-26 12:06:43 +03:30
this.variant || ['boolean', 'has', 'active'].includes(this.type) ? 'tag' : 'text'
2026-03-29 18:07:10 +03:30
);
2025-12-04 21:07:18 +03:30
}