Enhancement: allow specifying default currency for Monetary custom field (#7381)

This commit is contained in:
shamoon
2024-08-05 17:02:03 -07:00
committed by GitHub
parent 0ee85aae21
commit 15554322dd
13 changed files with 168 additions and 29 deletions

View File

@@ -52,6 +52,11 @@ describe('MonetaryComponent', () => {
expect(component.defaultCurrencyCode).toEqual('BRL')
})
it('should support setting a default currency code', () => {
component.defaultCurrency = 'EUR'
expect(component.defaultCurrencyCode).toEqual('EUR')
})
it('should parse monetary value correctly', () => {
expect(component['parseMonetaryValue']('123.4')).toEqual('123.4')
expect(component['parseMonetaryValue']('123.4', true)).toEqual('123.40')

View File

@@ -1,4 +1,4 @@
import { Component, forwardRef, Inject, LOCALE_ID } from '@angular/core'
import { Component, forwardRef, Inject, Input, LOCALE_ID } from '@angular/core'
import { NG_VALUE_ACCESSOR } from '@angular/forms'
import { AbstractInputComponent } from '../abstract-input'
import { getLocaleCurrencyCode } from '@angular/common'
@@ -29,11 +29,16 @@ export class MonetaryComponent extends AbstractInputComponent<string> {
defaultCurrencyCode: string
@Input()
set defaultCurrency(currency: string) {
if (currency) this.defaultCurrencyCode = currency
}
constructor(@Inject(LOCALE_ID) currentLocale: string) {
super()
this.currency = this.defaultCurrencyCode =
getLocaleCurrencyCode(currentLocale)
this.defaultCurrency ?? getLocaleCurrencyCode(currentLocale)
}
writeValue(newValue: any): void {