Skip to content

Commit 7567bd8

Browse files
feat(module:input-number): accepted numbers with commas (#9256)
1 parent 12d58bd commit 7567bd8

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

components/input-number/input-number.component.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,44 @@ describe('Input number', () => {
427427
expect(component.displayValue).toBe('1.10');
428428
});
429429

430+
it('should be accepted numbers with commas', () => {
431+
input('1,234');
432+
expect(component.value).toBe(1234);
433+
blur();
434+
expect(component.value).toBe(1234);
435+
436+
input('1,,2');
437+
expect(component.value).toBe(12);
438+
blur();
439+
expect(component.value).toBe(12);
440+
441+
input(',1,2,3,');
442+
expect(component.value).toBe(123);
443+
blur();
444+
expect(component.value).toBe(123);
445+
446+
// Illegal value will fall back to the last legal value
447+
input(',');
448+
expect(component.value).toBe(123);
449+
blur();
450+
expect(component.value).toBe(123);
451+
// Illegal value will fall back to the last legal value
452+
input(',,,');
453+
expect(component.value).toBe(123);
454+
blur();
455+
expect(component.value).toBe(123);
456+
457+
input('1,234,567');
458+
expect(component.value).toBe(1234567);
459+
blur();
460+
expect(component.value).toBe(1234567);
461+
462+
input('1,234,567.01');
463+
expect(component.value).toBe(1234567.01);
464+
blur();
465+
expect(component.value).toBe(1234567.01);
466+
});
467+
430468
function upStepByHandler(eventInit?: MouseEventInit): void {
431469
const handler = hostElement.querySelector('.ant-input-number-handler-up')!;
432470
handler.dispatchEvent(new MouseEvent('mousedown', eventInit));

components/input-number/input-number.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ const STEP_INTERVAL = 200;
532532
const STEP_DELAY = 600;
533533

534534
function defaultParser(value: string): number {
535-
return +value.trim().replace(//g, '.');
535+
return parseFloat(value.trim().replace(/,/g, '').replace(//g, '.'));
536536
}
537537

538538
function isInRange(value: number, min: number, max: number): boolean {

0 commit comments

Comments
 (0)