Skip to content

Commit

Permalink
fix(module:input-number): fix display value after formatter changed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Sun Zhongfeng authored and vthinkxie committed May 1, 2018
1 parent 20ae3c1 commit 179c1e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 12 additions & 1 deletion components/input-number/nz-input-number.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class NzInputNumberComponent implements ControlValueAccessor, AfterViewIn
private _step = 1;
private autoStepTimer;
private _autoFocus = false;
private _formatter = (value) => value;
displayValue: string | number;
actualValue: string | number;
isFocused = false;
Expand All @@ -82,7 +83,6 @@ export class NzInputNumberComponent implements ControlValueAccessor, AfterViewIn
@Input() nzSize: 'small' | 'default' | 'large' = 'default';
@Input() nzMin: number = -Infinity;
@Input() nzMax: number = Infinity;
@Input() nzFormatter = (value) => value;
@Input() nzParser = (value) => value;
@Input() nzPrecision: number;

Expand Down Expand Up @@ -125,6 +125,17 @@ export class NzInputNumberComponent implements ControlValueAccessor, AfterViewIn
return this._step;
}

@Input()
set nzFormatter(v: (value: number) => string | number) {
this._formatter = v;
const value = this.getCurrentValidValue(this.actualValue);
this.writeValue(value);
}

get nzFormatter(): (value: number) => string | number {
return this._formatter;
}

updateAutoFocus(): void {
if (this.isInit) {
if (this.nzAutoFocus) {
Expand Down
9 changes: 9 additions & 0 deletions components/input-number/nz-input-number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,15 @@ describe('input number', () => {
fixture.detectChanges();
expect(testComponent.value).toBe(-10);
});
it('should update value immediatelly after formatter changed', (() => {
const newFormatter = v => `${v} %`;
const initValue = '1';
testComponent.nzInputNumberComponent.onModelChange(initValue);
fixture.detectChanges();
testComponent.formatter = newFormatter;
fixture.detectChanges();
expect(inputElement.value).toBe(newFormatter(initValue));
}));
});
describe('input number form', () => {
let fixture;
Expand Down

0 comments on commit 179c1e2

Please sign in to comment.