Browse Source

feat(ui): improve type safety for GfValueComponent

pull/6290/head
KenTandrian 3 days ago
parent
commit
9424a67b08
  1. 60
      libs/ui/src/lib/value/value.component.ts

60
libs/ui/src/lib/value/value.component.ts

@ -30,7 +30,7 @@ export class GfValueComponent implements OnChanges {
@Input() isPercent = false; @Input() isPercent = false;
@Input() locale: string; @Input() locale: string;
@Input() position = ''; @Input() position = '';
@Input() precision: number; @Input() precision?: number;
@Input() size: 'large' | 'medium' | 'small' = 'small'; @Input() size: 'large' | 'medium' | 'small' = 'small';
@Input() subLabel = ''; @Input() subLabel = '';
@Input() unit = ''; @Input() unit = '';
@ -42,6 +42,14 @@ export class GfValueComponent implements OnChanges {
public isString = false; public isString = false;
public useAbsoluteValue = false; public useAbsoluteValue = false;
get hasPrecision(): boolean {
return (
this.precision !== undefined &&
this.precision !== null &&
this.precision >= 0
);
}
public ngOnChanges() { public ngOnChanges() {
this.initializeVariables(); this.initializeVariables();
@ -51,55 +59,44 @@ export class GfValueComponent implements OnChanges {
this.isString = false; this.isString = false;
this.absoluteValue = Math.abs(this.value); this.absoluteValue = Math.abs(this.value);
const options = this.getFormatOptions();
if (this.colorizeSign) { if (this.colorizeSign) {
if (this.isCurrency) { if (this.isCurrency) {
try { try {
this.formattedValue = this.absoluteValue.toLocaleString( this.formattedValue = this.absoluteValue.toLocaleString(
this.locale, this.locale,
{ options
maximumFractionDigits:
this.precision >= 0 ? this.precision : 2,
minimumFractionDigits:
this.precision >= 0 ? this.precision : 2
}
); );
} catch {} } catch {}
} else if (this.isPercent) { } else if (this.isPercent) {
try { try {
this.formattedValue = (this.absoluteValue * 100).toLocaleString( this.formattedValue = (this.absoluteValue * 100).toLocaleString(
this.locale, this.locale,
{ options
maximumFractionDigits:
this.precision >= 0 ? this.precision : 2,
minimumFractionDigits:
this.precision >= 0 ? this.precision : 2
}
); );
} catch {} } catch {}
} }
} else if (this.isCurrency) { } else if (this.isCurrency) {
try { try {
this.formattedValue = this.value?.toLocaleString(this.locale, { this.formattedValue = this.value?.toLocaleString(
maximumFractionDigits: this.precision >= 0 ? this.precision : 2, this.locale,
minimumFractionDigits: this.precision >= 0 ? this.precision : 2 options
}); );
} catch {} } catch {}
} else if (this.isPercent) { } else if (this.isPercent) {
try { try {
this.formattedValue = (this.value * 100).toLocaleString( this.formattedValue = (this.value * 100).toLocaleString(
this.locale, this.locale,
{ options
maximumFractionDigits: this.precision >= 0 ? this.precision : 2,
minimumFractionDigits: this.precision >= 0 ? this.precision : 2
}
); );
} catch {} } catch {}
} else if (this.precision >= 0) { } else if (this.hasPrecision) {
try { try {
this.formattedValue = this.value?.toLocaleString(this.locale, { this.formattedValue = this.value?.toLocaleString(
maximumFractionDigits: this.precision, this.locale,
minimumFractionDigits: this.precision options
}); );
} catch {} } catch {}
} else { } else {
this.formattedValue = this.value?.toLocaleString(this.locale); this.formattedValue = this.value?.toLocaleString(this.locale);
@ -133,13 +130,22 @@ export class GfValueComponent implements OnChanges {
} }
} }
private getFormatOptions(): Intl.NumberFormatOptions {
const digits = this.hasPrecision ? this.precision : 2;
return {
maximumFractionDigits: digits,
minimumFractionDigits: digits
};
}
private initializeVariables() { private initializeVariables() {
this.absoluteValue = 0; this.absoluteValue = 0;
this.formattedValue = ''; this.formattedValue = '';
this.isNumber = false; this.isNumber = false;
this.isString = false; this.isString = false;
this.locale = this.locale || getLocale(); this.locale = this.locale || getLocale();
this.precision = this.precision >= 0 ? this.precision : undefined; this.precision = this.hasPrecision ? this.precision : undefined;
this.useAbsoluteValue = false; this.useAbsoluteValue = false;
} }
} }

Loading…
Cancel
Save