diff --git a/src-ui/src/app/components/common/input/monetary/monetary.component.html b/src-ui/src/app/components/common/input/monetary/monetary.component.html index ff3d7b9b5..a21932c59 100644 --- a/src-ui/src/app/components/common/input/monetary/monetary.component.html +++ b/src-ui/src/app/components/common/input/monetary/monetary.component.html @@ -12,9 +12,9 @@
- {{monetaryValue | currency: currencyCode }} - - + {{ monetaryValue | currency: currency }} + +
{{error}} diff --git a/src-ui/src/app/components/common/input/monetary/monetary.component.spec.ts b/src-ui/src/app/components/common/input/monetary/monetary.component.spec.ts index 4106c01ec..60df71742 100644 --- a/src-ui/src/app/components/common/input/monetary/monetary.component.spec.ts +++ b/src-ui/src/app/components/common/input/monetary/monetary.component.spec.ts @@ -11,7 +11,6 @@ import { MonetaryComponent } from './monetary.component' describe('MonetaryComponent', () => { let component: MonetaryComponent let fixture: ComponentFixture - let input: HTMLInputElement beforeEach(async () => { await TestBed.configureTestingModule({ @@ -24,37 +23,22 @@ describe('MonetaryComponent', () => { fixture.debugElement.injector.get(NG_VALUE_ACCESSOR) component = fixture.componentInstance fixture.detectChanges() - input = component.inputField.nativeElement }) - it('should set the currency code correctly', () => { - expect(component.currencyCode).toEqual('USD') // default - component.currencyCode = 'EUR' - expect(component.currencyCode).toEqual('EUR') + it('should set the currency code and monetary value correctly', () => { + expect(component.currency).toEqual('USD') // default + component.writeValue('G123.4') + expect(component.currency).toEqual('G') - component.value = 'G123.4' - jest - .spyOn(document, 'activeElement', 'get') - .mockReturnValue(component.currencyField.nativeElement) - expect(component.currencyCode).toEqual('G') + component.writeValue('EUR123.4') + expect(component.currency).toEqual('EUR') + expect(component.monetaryValue).toEqual('123.40') }) - it('should parse monetary value only when out of focus', () => { - component.monetaryValue = 10.5 - jest.spyOn(document, 'activeElement', 'get').mockReturnValue(null) + it('should set monetary value to fixed decimals', () => { + component.monetaryValue = '10.5' + component.monetaryValueChange(true) expect(component.monetaryValue).toEqual('10.50') - - component.value = 'GBP123.4' - jest - .spyOn(document, 'activeElement', 'get') - .mockReturnValue(component.inputField.nativeElement) - expect(component.monetaryValue).toEqual('123.4') - }) - - it('should report value including currency code and monetary value', () => { - component.currencyCode = 'EUR' - component.monetaryValue = 10.5 - expect(component.value).toEqual('EUR10.50') }) it('should set the default currency code based on LOCALE_ID', () => { diff --git a/src-ui/src/app/components/common/input/monetary/monetary.component.ts b/src-ui/src/app/components/common/input/monetary/monetary.component.ts index db190c59d..edaad3859 100644 --- a/src-ui/src/app/components/common/input/monetary/monetary.component.ts +++ b/src-ui/src/app/components/common/input/monetary/monetary.component.ts @@ -1,11 +1,4 @@ -import { - Component, - ElementRef, - forwardRef, - Inject, - LOCALE_ID, - ViewChild, -} from '@angular/core' +import { Component, forwardRef, Inject, LOCALE_ID } from '@angular/core' import { NG_VALUE_ACCESSOR } from '@angular/forms' import { AbstractInputComponent } from '../abstract-input' import { getLocaleCurrencyCode } from '@angular/common' @@ -23,40 +16,47 @@ import { getLocaleCurrencyCode } from '@angular/common' styleUrls: ['./monetary.component.scss'], }) export class MonetaryComponent extends AbstractInputComponent { - @ViewChild('currencyField') - currencyField: ElementRef + public currency: string = '' + public monetaryValue: string = '' defaultCurrencyCode: string constructor(@Inject(LOCALE_ID) currentLocale: string) { super() - this.defaultCurrencyCode = getLocaleCurrencyCode(currentLocale) + this.currency = this.defaultCurrencyCode = + getLocaleCurrencyCode(currentLocale) } - get currencyCode(): string { - const focused = document.activeElement === this.currencyField?.nativeElement - if (focused && this.value) - return this.value.toUpperCase().match(/^([A-Z]{0,3})/)?.[0] + writeValue(newValue: any): void { + this.currency = this.parseCurrencyCode(newValue) + this.monetaryValue = this.parseMonetaryValue(newValue, true) + + this.value = this.currency + this.monetaryValue + } + + public monetaryValueChange(fixed: boolean = false): void { + this.monetaryValue = this.parseMonetaryValue(this.monetaryValue, fixed) + this.onChange(this.currency + this.monetaryValue) + } + + public currencyChange(): void { + if (this.currency.length) { + this.currency = this.parseCurrencyCode(this.currency) + this.onChange(this.currency + this.monetaryValue?.toString()) + } + } + + private parseCurrencyCode(value: string): string { return ( - this.value + value ?.toString() .toUpperCase() .match(/^([A-Z]{1,3})/)?.[0] ?? this.defaultCurrencyCode ) } - set currencyCode(value: string) { - this.value = value.toUpperCase() + this.monetaryValue?.toString() - } - - get monetaryValue(): string { - if (!this.value) return null - const focused = document.activeElement === this.inputField?.nativeElement - const val = parseFloat(this.value.toString().replace(/[^0-9.,-]+/g, '')) - return focused ? val.toString() : val.toFixed(2) - } - - set monetaryValue(value: number) { - this.value = this.currencyCode + value.toFixed(2) + private parseMonetaryValue(value: string, fixed: boolean = false): string { + const val: number = parseFloat(value.toString().replace(/[^0-9.,-]+/g, '')) + return fixed ? val.toFixed(2) : val.toString() } } diff --git a/src-ui/src/app/components/document-detail/document-detail.component.html b/src-ui/src/app/components/document-detail/document-detail.component.html index aed4d67da..64646c0b1 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.html +++ b/src-ui/src/app/components/document-detail/document-detail.component.html @@ -112,7 +112,7 @@ - @for (fieldInstance of document?.custom_fields; track fieldInstance; let i = $index) { + @for (fieldInstance of document?.custom_fields; track fieldInstance.field; let i = $index) {
@switch (getCustomFieldFromInstance(fieldInstance)?.data_type) { @case (CustomFieldDataType.String) {