Skip to content

Commit

Permalink
fix(module:input-number): use input event instead of change event
Browse files Browse the repository at this point in the history
  • Loading branch information
HyperLife1119 committed Jan 21, 2025
1 parent 2d8968d commit 98f6fac
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
1 change: 0 additions & 1 deletion components/input-number-legacy/input-number.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ export class NzInputNumberLegacyComponent implements ControlValueAccessor, After
.monitor(this.elementRef, true)
.pipe(takeUntil(this.destroy$))
.subscribe(focusOrigin => {
console.log(focusOrigin);
if (!focusOrigin) {
this.isFocused = false;
this.updateDisplayValue(this.value!);
Expand Down
21 changes: 8 additions & 13 deletions components/input-number/demo/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ import { NzInputNumberModule } from 'ng-zorro-antd/input-number';
selector: 'nz-demo-input-number-formatter',
imports: [FormsModule, NzInputNumberModule],
template: `
<nz-input-number [(ngModel)]="dollarValue" [nzFormatter]="formatterDollar" [nzParser]="parserDollar" />
<nz-input-number
[(ngModel)]="demoValue"
nzMin="1"
nzMax="100"
[nzFormatter]="formatterDollar"
[nzParser]="parserDollar"
/>
<nz-input-number
[(ngModel)]="demoValue"
[(ngModel)]="percentValue"
nzMin="1"
nzMax="100"
[nzFormatter]="formatterPercent"
Expand All @@ -31,9 +25,10 @@ import { NzInputNumberModule } from 'ng-zorro-antd/input-number';
]
})
export class NzDemoInputNumberFormatterComponent {
demoValue = 100;
formatterPercent = (value: number): string => `${value} %`;
parserPercent = (value: string): number => +value.replace(' %', '');
formatterDollar = (value: number): string => `$ ${value}`;
parserDollar = (value: string): number => +value.replace('$ ', '');
dollarValue = 1000;
percentValue = 100;
formatterDollar = (value: number): string => `$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
parserDollar = (value: string): number => parseFloat(value?.replace(/\$\s?|(,*)/g, ''));
formatterPercent = (value: number): string => `${value}%`;
parserPercent = (value: string): number => parseFloat(value?.replace('%', ''));
}
14 changes: 12 additions & 2 deletions components/input-number/input-number.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ describe('Input number', () => {
});

it('should be update value through user typing', () => {
userTypingInput('123');
expect(component.value).toBe(123);
userTypingInput('');
expect(component.value).toBe(null);
userTypingInput('NonNumber');
expect(component.value).toBe(null);
});

it('should be update value through user typing with range', () => {
component.min = 1;
component.max = 2;
fixture.detectChanges();
Expand All @@ -140,7 +149,9 @@ describe('Input number', () => {
expect(component.value).toBe(1);
userTypingInput('1');
expect(component.value).toBe(1);
userTypingInput('abc');
userTypingInput('');
expect(component.value).toBe(null);
userTypingInput('NonNumber');
expect(component.value).toBe(null);
});

Expand Down Expand Up @@ -288,7 +299,6 @@ describe('Input number', () => {
const input = hostElement.querySelector('input')!;
input.value = text;
input.dispatchEvent(new Event('input'));
input.dispatchEvent(new Event('change'));
}
});

Expand Down
37 changes: 25 additions & 12 deletions components/input-number/input-number.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

import { NzFormItemFeedbackIconComponent, NzFormStatusService } from 'ng-zorro-antd/core/form';
import { NzSizeLDSType, NzStatus, NzValidateStatus, OnChangeType, OnTouchedType } from 'ng-zorro-antd/core/types';
import { getStatusClassNames, isNil } from 'ng-zorro-antd/core/util';
import { getStatusClassNames, isNil, isNotNil } from 'ng-zorro-antd/core/util';
import { NzIconModule } from 'ng-zorro-antd/icon';
import {
NzInputAddonAfterDirective,
Expand Down Expand Up @@ -147,13 +147,13 @@ import { NZ_SPACE_COMPACT_ITEM_TYPE, NZ_SPACE_COMPACT_SIZE, NzSpaceCompactItemDi
[attr.aria-valuemin]="nzMin()"
[attr.aria-valuemax]="nzMax()"
[attr.id]="nzId()"
[value]="displayValue()"
[attr.step]="nzStep()"
[attr.value]="displayValue()"
[value]="displayValue()"
[placeholder]="nzPlaceHolder() ?? ''"
[disabled]="finalDisabled()"
[readOnly]="nzReadOnly()"
(input)="displayValue.set(input.value)"
(change)="onInputChange($event)"
(input)="onInputChange(input.value)"
/>
</div>
</ng-template>
Expand Down Expand Up @@ -307,6 +307,7 @@ export class NzInputNumberComponent implements OnInit, ControlValueAccessor {
this.focused.set(!!origin);

if (!origin) {
this.fixValue();
this.onTouched();
}
});
Expand Down Expand Up @@ -381,18 +382,20 @@ export class NzInputNumberComponent implements OnInit, ControlValueAccessor {
private setValue(value: number | string | null, userTyping?: boolean): void {
let parsedValue: number | null = null;

if (!isNil(value)) {
if (isNotNil(value) && value !== '' && !Number.isNaN(value)) {
parsedValue = this.nzParser()(value.toString());

// If the user is typing, we need to make sure the value is in the range.
// Instead, we allow values to be set out of range programmatically,
// and display out-of-range values as errors.
if (userTyping) {
if (Number.isNaN(parsedValue)) {
parsedValue = null;
} else {
parsedValue = getRangeValueWithPrecision(parsedValue, this.nzMin(), this.nzMax(), this.nzPrecision());
// If the value is not complete number, or it's NaN after parsed, ignore this change.
// And then call `fixValue` when on blur.
if (isNotCompleteNumber(value) || Number.isNaN(parsedValue)) {
return;
}

parsedValue = getRangeValueWithPrecision(parsedValue, this.nzMin(), this.nzMax(), this.nzPrecision());
}
}

Expand All @@ -401,6 +404,13 @@ export class NzInputNumberComponent implements OnInit, ControlValueAccessor {
this.onChange(parsedValue);
}

private fixValue(): void {
const parsedValue = this.nzParser()(this.displayValue());
// Use parseFloat to try to fix the value.
this.setValue(parseFloat(parsedValue.toString()), true);
this.inputRef().nativeElement.value = this.displayValue();
}

protected stopAutoStep(): void {
if (this.autoStepTimer !== null) {
clearTimeout(this.autoStepTimer);
Expand Down Expand Up @@ -437,9 +447,8 @@ export class NzInputNumberComponent implements OnInit, ControlValueAccessor {
}
}

protected onInputChange(value: Event): void {
const target = value.target as HTMLInputElement;
this.setValue(target.value, true);
protected onInputChange(value: string): void {
this.setValue(value, true);
}
}

Expand Down Expand Up @@ -503,3 +512,7 @@ function getRangeValueWithPrecision(value: number, min: number, max: number, pre
function getDecimalPlaces(num: number): number {
return num.toString().split('.')[1]?.length || 0;
}

function isNotCompleteNumber(value: string | number): boolean {
return /[.]$/.test(value.toString());
}

0 comments on commit 98f6fac

Please sign in to comment.