Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(close for contributing guide): limit integer, decimal input #44

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/components/input-number/nz-input-number.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
[disabled]="nzDisabled"
[(ngModel)]="nzValue"
(ngModelChange)="_userInputChange()"
(keypress)="_onKeyPress($event)"
[attr.min]="nzMin"
[attr.max]="nzMax"
[attr.step]="_step"
Expand Down Expand Up @@ -186,6 +187,24 @@ export class NzInputNumberComponent implements ControlValueAccessor {
}
}

/**
* limit integer, decimal input
*/
_onKeyPress($event) {
let value = this._inputNumber.nativeElement.value;
if ($event.keyCode === 46) {
if (value.indexOf('.') !== -1) {
$event.returnValue = false;
}
} else if ($event.keyCode === 45) {
if (this.nzMin >= 0 || (value.indexOf('-') !== -1 && value.lastIndexOf('-') >=0)) {
$event.returnValue = false;
}
} else if ($event.keyCode < 48 || $event.keyCode > 57) {
$event.returnValue = false;
}
}

toPrecisionAsStep(num) {
if (isNaN(num) || num === '') {
return num;
Expand Down
7 changes: 7 additions & 0 deletions src/components/input/nz-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
<ng-template [ngIf]="nzType!='textarea'">
<input
(blur)="_emitBlur($event)"
(focus)="_emitFocus($event)"
[attr.id]="nzId"
[disabled]="nzDisabled"
[attr.type]="nzType"
Expand All @@ -40,6 +41,7 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
<ng-template [ngIf]="nzType=='textarea'">
<textarea
(blur)="_emitBlur($event)"
(focus)="_emitFocus($event)"
[attr.id]="nzId"
#inputTextarea
[disabled]="nzDisabled"
Expand Down Expand Up @@ -100,6 +102,7 @@ export class NzInputComponent implements AfterContentInit, ControlValueAccessor
}

@Output() nzBlur: EventEmitter<MouseEvent> = new EventEmitter();
@Output() nzFocus: EventEmitter<MouseEvent> = new EventEmitter();
@ContentChild('addOnBefore') _addOnContentBefore: TemplateRef<any>;
@ContentChild('addOnAfter') _addOnContentAfter: TemplateRef<any>;

Expand Down Expand Up @@ -136,6 +139,10 @@ export class NzInputComponent implements AfterContentInit, ControlValueAccessor
this.nzBlur.emit($event);
}

_emitFocus($event) {
this.nzFocus.emit($event);
}

constructor(private _elementRef: ElementRef, private _renderer: Renderer2) {
this._el = this._elementRef.nativeElement;
}
Expand Down
10 changes: 9 additions & 1 deletion src/showcase/nz-demo-input/nz-demo-input-basic.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component, OnInit } from '@angular/core';
@Component({
selector: 'nz-demo-input-basic',
template: `
<nz-input [(ngModel)]="inputValue" [nzPlaceHolder]="'Basic usage'" (ngModelChange)="_console($event)"></nz-input>
<nz-input [(ngModel)]="inputValue" (nzFocus)="_focus($event)" (nzBlur)="_blur($event)" [nzPlaceHolder]="'Basic usage'" (ngModelChange)="_console($event)"></nz-input>
<p>{{inputValue}}</p>`,

styles: []
Expand All @@ -20,5 +20,13 @@ export class NzDemoInputBasicComponent implements OnInit {

ngOnInit() {
}

_focus($event) {
console.log($event);
}

_blur($event) {
console.log($event);
}
}

18 changes: 18 additions & 0 deletions src/showcase/nz-demo-input/nz-demo-input.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ <h3 id="Input"><span>nz-input</span>
<td>bool</td>
<td>false</td>
</tr>
<tr>
<td>nzBlur</td>
<td>失去焦点回调</td>
<td>EventEmitter</td>
<td>无</td>
</tr>
<tr>
<td>nzFocus</td>
<td>获取焦点回调</td>
<td>EventEmitter</td>
<td>无</td>
</tr>
<tr>
<td>nzDisabled</td>
<td>是否禁用状态,默认为 false</td>
<td>bool</td>
<td>false</td>
</tr>
<tr>
<td>#addonBefore</td>
<td>设置前置标签</td>
Expand Down