Skip to content

Commit b934da6

Browse files
committed
[AAE-37713] applied pr comments
1 parent 4f78092 commit b934da6

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

lib/core/src/lib/form/components/widgets/amount/amount.widget.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
(ngModelChange)="onFieldChangedAmountWidget()"
2929
[disabled]="field.readOnly"
3030
(focus)="amountWidgetOnFocus()"
31-
(blur)="markAsTouched();amountWidgetOnBlur()"
31+
(blur)="amountWidgetOnBlur()"
3232
/>
3333
</mat-form-field>
3434
<div class="adf-error-messages-container">

lib/core/src/lib/form/components/widgets/amount/amount.widget.spec.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe('AmountWidgetComponent', () => {
7979

8080
it('it should return locale based on browser', () => {
8181
const returnedLanguages: string[] = ['en-GB', 'en-US', 'en', 'de-DE', 'pl'];
82-
const mockLanguages = spyOnProperty(window, 'navigator', 'get').and.returnValue({
82+
const mockLanguages = spyOnProperty(window, 'navigator').and.returnValue({
8383
languages: returnedLanguages
8484
} as any);
8585
const locale = widget.getLocale();
@@ -90,7 +90,7 @@ describe('AmountWidgetComponent', () => {
9090

9191
it('it should return default locale if browser does not return valid value', () => {
9292
const defaultLocale = 'en-US';
93-
spyOnProperty(window, 'navigator', 'get').and.returnValue({
93+
spyOnProperty(window, 'navigator').and.returnValue({
9494
languages: undefined
9595
} as any);
9696
const locale = widget.getLocale();
@@ -100,7 +100,7 @@ describe('AmountWidgetComponent', () => {
100100

101101
it('should set initial values when enableDisplayBasedOnLocale is enabled', () => {
102102
const returnedLanguages: string[] = ['en-GB'];
103-
spyOnProperty(window, 'navigator', 'get').and.returnValue({
103+
spyOnProperty(window, 'navigator').and.returnValue({
104104
languages: returnedLanguages
105105
} as any);
106106
widget.field = new FormFieldModel(null, { id: 1, name: 'test', value: 25, currency: 'USD' });
@@ -116,7 +116,7 @@ describe('AmountWidgetComponent', () => {
116116

117117
it('should set initial values with correct currency', () => {
118118
const returnedLanguages: string[] = ['en-GB'];
119-
spyOnProperty(window, 'navigator', 'get').and.returnValue({
119+
spyOnProperty(window, 'navigator').and.returnValue({
120120
languages: returnedLanguages
121121
} as any);
122122
widget.field = new FormFieldModel(null, { id: 2, name: 'test', value: 25, currency: 'GBP' });
@@ -130,7 +130,7 @@ describe('AmountWidgetComponent', () => {
130130

131131
it('should set initial values with correct currency icon', () => {
132132
const returnedLanguages: string[] = ['en-GB'];
133-
spyOnProperty(window, 'navigator', 'get').and.returnValue({
133+
spyOnProperty(window, 'navigator').and.returnValue({
134134
languages: returnedLanguages
135135
} as any);
136136
widget.field = new FormFieldModel(null, { id: 2, name: 'test', value: 25, currency: '¥' });
@@ -144,7 +144,7 @@ describe('AmountWidgetComponent', () => {
144144

145145
it('should set initial values without currency', () => {
146146
const returnedLanguages: string[] = ['en-GB'];
147-
spyOnProperty(window, 'navigator', 'get').and.returnValue({
147+
spyOnProperty(window, 'navigator').and.returnValue({
148148
languages: returnedLanguages
149149
} as any);
150150
widget.field = new FormFieldModel(null, { id: 3, name: 'test', value: 25, currency: '' });
@@ -199,14 +199,12 @@ describe('AmountWidgetComponent', () => {
199199
widget.amountWidgetOnBlur();
200200

201201
expect(widget.valueAsNumber).toBe(1234.56);
202-
expect(widget.valueAsString).toBe('$1,234.56');
203202
expect(widget.amountWidgetValue).toBe('$1,234.56');
204203

205204
widget.amountWidgetValue = '';
206205
widget.amountWidgetOnBlur();
207206

208207
expect(widget.valueAsNumber).toBe(null);
209-
expect(widget.valueAsString).toBe(null);
210208
expect(widget.amountWidgetValue).toBe(null);
211209
});
212210

@@ -571,7 +569,6 @@ describe('AmountWidgetComponent - rendering', () => {
571569
expect(field).toBeDefined();
572570
expect(widget.field.value).toBe('1234.55');
573571
expect(widget.valueAsNumber).toBeUndefined();
574-
expect(widget.valueAsString).toBeUndefined();
575572
expect(fieldValueBeforeFocus).toBe('1234.55');
576573
expect(focusSpy).toHaveBeenCalled();
577574
expect(fieldValue).toBe('1234.55');
@@ -591,7 +588,6 @@ describe('AmountWidgetComponent - rendering', () => {
591588
expect(field).toBeDefined();
592589
expect(widget.field.value).toBe(newValue);
593590
expect(widget.valueAsNumber).toBeUndefined();
594-
expect(widget.valueAsString).toBeUndefined();
595591
expect(fieldValueBeforeBlur).toBe('1234.55');
596592
expect(blurSpy).toHaveBeenCalled();
597593
expect(widget.valueAsNumber).toBeUndefined();
@@ -623,7 +619,7 @@ describe('AmountWidgetComponent - rendering', () => {
623619
fixture = TestBed.createComponent(AmountWidgetComponent);
624620
widget = fixture.componentInstance;
625621
const returnedLanguages: string[] = ['en-GB', 'en-US', 'en', 'de-DE', 'pl'];
626-
spyOnProperty(window, 'navigator', 'get').and.returnValue({
622+
spyOnProperty(window, 'navigator').and.returnValue({
627623
languages: returnedLanguages
628624
} as any);
629625
fixture.componentRef.setInput('field', mockField);

lib/core/src/lib/form/components/widgets/amount/amount.widget.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ export class AmountWidgetComponent extends WidgetComponent implements OnInit {
6565
decimalProperty: string;
6666
enableDisplayBasedOnLocale: boolean;
6767
locale: string;
68+
notShowDecimalDigits = '1.0-0';
69+
showDecimalDigits = '1.2-2';
6870
showReadonlyPlaceholder: boolean;
6971
valueAsNumber: number;
70-
valueAsString: string;
7172

7273
get placeholder(): string {
7374
return this.showPlaceholder ? this.field.placeholder : '';
@@ -88,11 +89,6 @@ export class AmountWidgetComponent extends WidgetComponent implements OnInit {
8889
}
8990
}
9091

91-
updateSettingsBasedProperties(data: AmountWidgetSettings): void {
92-
this.enableDisplayBasedOnLocale = data?.enableDisplayBasedOnLocale ?? false;
93-
this.showReadonlyPlaceholder = data?.showReadonlyPlaceholder;
94-
}
95-
9692
ngOnInit() {
9793
if (this.field) {
9894
if (this.field.currency) {
@@ -111,45 +107,50 @@ export class AmountWidgetComponent extends WidgetComponent implements OnInit {
111107
}
112108
}
113109

114-
amountWidgetOnBlur() {
110+
amountWidgetOnBlur(): void {
115111
if (this.enableDisplayBasedOnLocale) {
116112
if (this.amountWidgetValue) {
117113
this.valueAsNumber = parseFloat(this.amountWidgetValue);
118-
this.valueAsString = this.currencyPipe.transform(this.amountWidgetValue, this.currency, this.currencyDisplay, this.decimalProperty);
119-
this.amountWidgetValue = this.valueAsString;
114+
this.amountWidgetValue = this.currencyPipe.transform(
115+
this.amountWidgetValue,
116+
this.currency,
117+
this.currencyDisplay,
118+
this.decimalProperty
119+
);
120120
} else {
121121
this.valueAsNumber = null;
122-
this.valueAsString = null;
123122
this.amountWidgetValue = null;
124123
}
125124
}
125+
this.markAsTouched();
126126
}
127127

128-
amountWidgetOnFocus() {
128+
amountWidgetOnFocus(): void {
129129
if (this.enableDisplayBasedOnLocale) {
130-
this.amountWidgetValue = this.valueAsNumber || this.valueAsNumber === 0 ? this.valueAsNumber.toString() : null;
130+
const hasValue = this.valueAsNumber === 0 || this.valueAsNumber;
131+
this.amountWidgetValue = hasValue ? this.valueAsNumber.toString() : null;
131132
}
132133
}
133134

134-
getLocale() {
135+
getLocale(): string {
135136
const defaultLocale = 'en-US';
136137
if (typeof window?.navigator === 'undefined') {
137138
return defaultLocale;
138139
}
139-
const wn = window.navigator as any;
140+
const wn = window.navigator as Navigator;
140141
let lang = wn.languages ? wn.languages[0] : defaultLocale;
141-
lang = lang || wn.language || wn.browserLanguage || wn.userLanguage;
142+
lang = lang || wn.language;
142143
return lang;
143144
}
144145

145-
onFieldChangedAmountWidget() {
146+
onFieldChangedAmountWidget(): void {
146147
this.field.value = this.amountWidgetValue;
147148
super.onFieldChanged(this.field);
148149
}
149150

150151
setInitialValues(): void {
151152
if (this.enableDisplayBasedOnLocale) {
152-
this.decimalProperty = this.field.enableFractions ? '1.2-2' : '1.0-0';
153+
this.decimalProperty = this.field.enableFractions ? this.showDecimalDigits : this.notShowDecimalDigits;
153154
this.locale = this.getLocale();
154155
this.valueAsNumber = this.field.value;
155156
this.amountWidgetValue = this.currencyPipe.transform(
@@ -163,4 +164,9 @@ export class AmountWidgetComponent extends WidgetComponent implements OnInit {
163164
this.amountWidgetValue = this.field.value;
164165
}
165166
}
167+
168+
updateSettingsBasedProperties(data: AmountWidgetSettings): void {
169+
this.enableDisplayBasedOnLocale = data?.enableDisplayBasedOnLocale ?? false;
170+
this.showReadonlyPlaceholder = data?.showReadonlyPlaceholder;
171+
}
166172
}

0 commit comments

Comments
 (0)