Browse Source

fix(ui): optimize formatOptions with computed signal

pull/6290/head
KenTandrian 3 days ago
parent
commit
e7b55138e6
  1. 31
      libs/ui/src/lib/value/value.component.ts

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

@ -7,6 +7,7 @@ import {
Component, Component,
Input, Input,
OnChanges, OnChanges,
computed,
input input
} from '@angular/core'; } from '@angular/core';
import { IonIcon } from '@ionic/angular/standalone'; import { IonIcon } from '@ionic/angular/standalone';
@ -44,6 +45,15 @@ export class GfValueComponent implements OnChanges {
public readonly precision = input<number>(); public readonly precision = input<number>();
private readonly formatOptions = computed<Intl.NumberFormatOptions>(() => {
const digits = this.hasPrecision ? this.precision() : 2;
return {
maximumFractionDigits: digits,
minimumFractionDigits: digits
};
});
private get hasPrecision(): boolean { private get hasPrecision(): boolean {
const precision = this.precision(); const precision = this.precision();
return precision !== undefined && precision >= 0; return precision !== undefined && precision >= 0;
@ -58,21 +68,19 @@ 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 this.formatOptions()
); );
} 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 this.formatOptions()
); );
} catch {} } catch {}
} }
@ -80,21 +88,21 @@ export class GfValueComponent implements OnChanges {
try { try {
this.formattedValue = this.value?.toLocaleString( this.formattedValue = this.value?.toLocaleString(
this.locale, this.locale,
options this.formatOptions()
); );
} 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 this.formatOptions()
); );
} catch {} } catch {}
} else if (this.hasPrecision) { } else if (this.hasPrecision) {
try { try {
this.formattedValue = this.value?.toLocaleString( this.formattedValue = this.value?.toLocaleString(
this.locale, this.locale,
options this.formatOptions()
); );
} catch {} } catch {}
} else { } else {
@ -129,15 +137,6 @@ 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 = '';

Loading…
Cancel
Save