Skip to content

Commit e203ca4

Browse files
CSimoesJrrafaellmarques
authored andcommitted
fix(decimal): corrige o uso de hifens, pontos e espaços
Bloqueia espaços e limpa input havendo hífen sem valor númerico fixes DTHFUI-6099
1 parent 43d97dd commit e203ca4

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

projects/ui/src/lib/components/po-field/po-decimal/po-decimal.component.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,27 @@ describe('PoDecimalComponent:', () => {
249249
expect(component['callOnChange']).toHaveBeenCalled();
250250
});
251251

252+
it('blur event must be called if has only hyphen', () => {
253+
const fakeEvent = {
254+
target: {
255+
value: '-'
256+
}
257+
};
258+
component['onTouched'] = () => {};
259+
260+
spyOn(component, <any>'setViewValue');
261+
spyOn(component, <any>'onTouched');
262+
spyOn(component, <any>'callOnChange');
263+
spyOn(component.blur, 'emit');
264+
265+
component.onBlur(fakeEvent);
266+
267+
expect(component['onTouched']).toHaveBeenCalled();
268+
expect(component['setViewValue']).toHaveBeenCalled();
269+
expect(component['callOnChange']).toHaveBeenCalled();
270+
expect(component.blur.emit).not.toHaveBeenCalled();
271+
});
272+
252273
it('should have a call onInput method', () => {
253274
const fakeEvent = {
254275
target: {
@@ -1402,6 +1423,18 @@ describe('PoDecimalComponent:', () => {
14021423
expect(component.hasLetters(undefined)).toBeFalsy();
14031424
});
14041425

1426+
it('hasNotSpace: should return false with whitespace value.', () => {
1427+
expect(component.hasNotSpace(' ')).toBeFalsy();
1428+
});
1429+
1430+
it('hasNotSpace: should return true with number.', () => {
1431+
expect(component.hasNotSpace('211')).toBeTruthy();
1432+
});
1433+
1434+
it('hasNotSpace: should return true with undefined value.', () => {
1435+
expect(component.hasNotSpace(undefined)).toBeTruthy();
1436+
});
1437+
14051438
it('isGreaterThanTotalLengthLimit: should return `false` if total sum is 16.', () => {
14061439
let decimalsMaxLength = 1;
14071440
let thousandMaxlength = 15;

projects/ui/src/lib/components/po-field/po-decimal/po-decimal.component.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,16 @@ export class PoDecimalComponent extends PoInputBaseComponent implements AfterVie
327327
return value.match(/[a-zA-Z:;+=_´`^~"'?!@#$%¨&*()><{}çÇ\[\]/\\|]+/);
328328
}
329329

330+
hasNotSpace(value: string = '') {
331+
return value.match(/^\S*$/);
332+
}
333+
330334
isValidNumber(event: any): boolean {
331335
// - event.key não existia em alguns browsers, como Samsung browser e Firefox.
332336
const keyValue = <any>String.fromCharCode(event.which);
333337
const validKey = event.which !== 8 && event.which !== 0;
334338

335-
return !this.hasLetters(keyValue) && validKey;
339+
return !this.hasLetters(keyValue) && this.hasNotSpace(keyValue) && validKey;
336340
}
337341

338342
// função responsável por adicionar os zeroes com as casa decimais ao sair do campo.
@@ -348,7 +352,12 @@ export class PoDecimalComponent extends PoInputBaseComponent implements AfterVie
348352
}
349353

350354
const valueWithoutThousandSeparator = this.formatValueWithoutThousandSeparator(value);
351-
this.setViewValue(this.formatToViewValue(valueWithoutThousandSeparator));
355+
const formatedViewValue = this.formatToViewValue(valueWithoutThousandSeparator);
356+
this.setViewValue(formatedViewValue);
357+
if (!formatedViewValue) {
358+
this.callOnChange(undefined);
359+
return;
360+
}
352361
}
353362

354363
this.blur.emit();
@@ -427,16 +436,17 @@ export class PoDecimalComponent extends PoInputBaseComponent implements AfterVie
427436
}
428437

429438
writeValueModel(value) {
439+
let formatedViewValue;
430440
if (this.inputEl) {
431441
if (value || value === 0) {
432-
const formatedViewValue = this.formatToViewValue(value);
442+
formatedViewValue = this.formatToViewValue(value);
433443
this.setViewValue(formatedViewValue);
434444
} else {
435445
this.setViewValue('');
436446
}
437447
}
438448

439-
if (value) {
449+
if (formatedViewValue) {
440450
this.change.emit(value);
441451
}
442452
}
@@ -507,6 +517,10 @@ export class PoDecimalComponent extends PoInputBaseComponent implements AfterVie
507517

508518
const formatedNumber = this.formatMask(valueBeforeDot);
509519

520+
if (formatedNumber === 'NaN') {
521+
return '';
522+
}
523+
510524
if (this.decimalsLength === 0) {
511525
return formatedNumber;
512526
} else {

0 commit comments

Comments
 (0)