Skip to content

Commit

Permalink
feat(module:input-number): trigger ngModelChange at once (NG-ZORRO#4769)
Browse files Browse the repository at this point in the history
close NG-ZORRO#3039 close NG-ZORRO#3773

BREAKING CHANGES:
ngModelChange trigger at once when user typing
  • Loading branch information
Yadong Xie authored Feb 11, 2020
1 parent 3c41a7b commit a585f69
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 243 deletions.
15 changes: 13 additions & 2 deletions components/core/testing/event-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ export function createTouchEvent(type: string, pageX: number = 0, pageY: number
}

/** Dispatches a keydown event from an element. */
export function createKeyboardEvent(type: string, keyCode: number, target?: Element, key?: string): KeyboardEvent {
export function createKeyboardEvent(
type: string,
keyCode: number,
target?: Element,
key?: string,
ctrlKey?: boolean,
metaKey?: boolean,
shiftKey?: boolean
): KeyboardEvent {
// tslint:disable-next-line:no-any
const event = document.createEvent('KeyboardEvent') as any;
const originalPreventDefault = event.preventDefault;
Expand All @@ -71,7 +79,10 @@ export function createKeyboardEvent(type: string, keyCode: number, target?: Elem
Object.defineProperties(event, {
keyCode: { get: () => keyCode },
key: { get: () => key },
target: { get: () => target }
target: { get: () => target },
ctrlKey: { get: () => ctrlKey },
metaKey: { get: () => metaKey },
shiftKey: { get: () => shiftKey }
});

// IE won't set `defaultPrevented` on synthetic events so we need to do it manually.
Expand Down
13 changes: 4 additions & 9 deletions components/input-number/demo/precision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import { Component } from '@angular/core';
selector: 'nz-demo-input-number-precision',
template: `
<nz-input-number [(ngModel)]="toFixedValue" [nzPrecision]="precision" nzPlaceHolder="toFixed"></nz-input-number>
<nz-input-number
[(ngModel)]="cutValue"
[nzPrecision]="precision"
nzPrecisionMode="cut"
nzPlaceHolder="cut off"
></nz-input-number>
<nz-input-number [(ngModel)]="cutValue" [nzPrecision]="precision" nzPrecisionMode="cut" nzPlaceHolder="cut off"></nz-input-number>
<nz-input-number
[(ngModel)]="customFnValue"
[nzPrecision]="precision"
Expand All @@ -26,9 +21,9 @@ import { Component } from '@angular/core';
]
})
export class NzDemoInputNumberPrecisionComponent {
toFixedValue = 2.128;
cutValue = 2.128;
customFnValue = 2.128;
toFixedValue = 2;
cutValue = 2;
customFnValue = 2;
precision = 2;
customPrecisionFn(value: number, precision: number): number {
return +Number(value).toFixed(precision + 1);
Expand Down
6 changes: 1 addition & 5 deletions components/input-number/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ title: InputNumber

Enter a number within certain range with the mouse or keyboard.

> Note:InputNumber will validate the input value only when `(blur)` and `(keydown.enter)` happened other than when user input character to avoid error `ngModelChange` output (-0.02001 or -1.0e28)
## When To Use

When a numeric value needs to be provided.
Expand All @@ -20,8 +18,6 @@ import { NzInputNumberModule } from 'ng-zorro-antd/input-number';

### nz-input-number

The value entered in `nz-input-number` will not be verified at the time of input, but will be fed back to `[ngModel]` and `(ngModelChange)` at a specific timing (Enter key, up and down keys, blur, etc.), otherwise input data such as `-0.12` or `1e10`, the `ngModel` will always be `undefined`.

| property | description | type | default |
| -------- | ----------- | ---- | ------- |
| `[ngModel]` | current value, double binding | `number \| string` \| `string` | - |
Expand All @@ -30,7 +26,7 @@ The value entered in `nz-input-number` will not be verified at the time of input
| `[nzMax]` | max value | `number` | `Infinity` |
| `[nzMin]` | min value | `number` | `-Infinity` |
| `[nzFormatter]` | Specifies the format of the value presented | `(value: number \| string) => string \| number` | - |
| `[nzParser]` | Specifies the value extracted from nzFormatter | `(value: string) => string \| number` | - |
| `[nzParser]` | Specifies the value extracted from nzFormatter | `(value: string) => string \| number` | `(value: string) => value.trim().replace(/。/g, '.').replace(/[^\w\.-]+/g, '')` |
| `[nzPrecision]` | precision of input value | `number` | - |
| `[nzPrecisionMode]` | The method for calculating the precision of input value | `'cut' \| 'toFixed' \| ((value: number \| string, precision?: number) => number)` | `'toFixed'` |
| `[nzSize]` | width of input box | `'large' \| 'small' \| 'default'` | `'default'` |
Expand Down
6 changes: 1 addition & 5 deletions components/input-number/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ title: InputNumber

通过鼠标或键盘,输入范围内的数值。

> 注意:InputNumber 会在 `(blur)``(keydown.enter)` 时触发校验,而不是在用户输入每个字符时立刻进行校验,以此来避免反复输出错误校验结果的情况(例如输入 -0.02001 或者 -1.0e28)
## 何时使用

当需要获取标准数值时。
Expand All @@ -21,8 +19,6 @@ import { NzInputNumberModule } from 'ng-zorro-antd/input-number';

### nz-input-number

`nz-input-number` 中输入的数值不会在输入时进行校验,而是在特定时机(回车键,上下键,失去焦点等)时才会校验后反馈到 `[ngModel]``(ngModelChange)` 中,否则输入 `-0.12` 或者 `1e10` 这种类型数据时,双向绑定的数据会永远是 `undefined`

| 成员 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| `[ngModel]` | 当前值,可双向绑定 | `number \| string` \| `string` | - |
Expand All @@ -31,7 +27,7 @@ import { NzInputNumberModule } from 'ng-zorro-antd/input-number';
| `[nzMax]` | 最大值 | `number` | `Infinity` |
| `[nzMin]` | 最小值 | `number` | `-Infinity` |
| `[nzFormatter]` | 指定输入框展示值的格式 | `(value: number \| string) => string \| number` | - |
| `[nzParser]` | 指定从 nzFormatter 里转换回数字的方式,和 nzFormatter 搭配使用 | `(value: string) => string \| number` | - |
| `[nzParser]` | 指定从 nzFormatter 里转换回数字的方式,和 nzFormatter 搭配使用 | `(value: string) => string \| number` | `(value: string) => value.trim().replace(/。/g, '.').replace(/[^\w\.-]+/g, '')` |
| `[nzPrecision]` | 数值精度 | `number` | - |
| `[nzPrecisionMode]` | 数值精度的取值方式 | `'cut' \| 'toFixed' \| ((value: number \| string, precision?: number) => number)` | `'toFixed'` |
| `[nzSize]` | 输入框大小 | `'large' \| 'small' \| 'default'` | `'default'` |
Expand Down
Loading

0 comments on commit a585f69

Please sign in to comment.