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 {

View File

@@ -11,7 +11,7 @@
}
</div>
<div class="position-relative" [class.col-md-9]="horizontal">
<input #inputField type="text" class="form-control" [class.is-invalid]="error" [id]="inputId" [(ngModel)]="value" (change)="onChange(value)" [disabled]="disabled" [autocomplete]="autocomplete">
<input #inputField type="text" class="form-control" [class.is-invalid]="error" [id]="inputId" [(ngModel)]="value" (change)="onChange(value)" [disabled]="disabled" [autocomplete]="autocomplete" [placeholder]="placeholder">
@if (hint) {
<small class="form-text text-muted" [innerHTML]="hint | safeHtml"></small>
}

View File

@@ -18,6 +18,9 @@ export class TextComponent extends AbstractInputComponent<string> {
@Input()
autocomplete: string
@Input()
placeholder: string = ''
constructor() {
super()
}