Browse Source

Task/improve type safety for GfValueComponent (#6290)

* Improve type safety for GfValueComponent
pull/6264/head^2
Kenrick Tandrian 1 day ago
committed by GitHub
parent
commit
64a6dfd124
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 59
      libs/ui/src/lib/value/value.component.ts

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

@ -6,7 +6,9 @@ import {
ChangeDetectionStrategy, ChangeDetectionStrategy,
Component, Component,
Input, Input,
OnChanges OnChanges,
computed,
input
} from '@angular/core'; } from '@angular/core';
import { IonIcon } from '@ionic/angular/standalone'; import { IonIcon } from '@ionic/angular/standalone';
import { isNumber } from 'lodash'; import { isNumber } from 'lodash';
@ -30,7 +32,6 @@ 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() size: 'large' | 'medium' | 'small' = 'small'; @Input() size: 'large' | 'medium' | 'small' = 'small';
@Input() subLabel = ''; @Input() subLabel = '';
@Input() unit = ''; @Input() unit = '';
@ -42,6 +43,22 @@ export class GfValueComponent implements OnChanges {
public isString = false; public isString = false;
public useAbsoluteValue = false; public useAbsoluteValue = false;
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() {
const precision = this.precision();
return precision !== undefined && precision >= 0;
}
public ngOnChanges() { public ngOnChanges() {
this.initializeVariables(); this.initializeVariables();
@ -56,50 +73,37 @@ export class GfValueComponent implements OnChanges {
try { try {
this.formattedValue = this.absoluteValue.toLocaleString( this.formattedValue = this.absoluteValue.toLocaleString(
this.locale, this.locale,
{ this.formatOptions()
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,
{ this.formatOptions()
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 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,
{ this.formatOptions()
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 this.formatOptions()
}); );
} catch {} } catch {}
} else { } else {
this.formattedValue = this.value?.toLocaleString(this.locale); this.formattedValue = this.value?.toLocaleString(this.locale);
@ -139,7 +143,6 @@ export class GfValueComponent implements OnChanges {
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.useAbsoluteValue = false; this.useAbsoluteValue = false;
} }
} }

Loading…
Cancel
Save