Input输入框
通过鼠标或键盘输入内容,是最基础的表单域的包装。
何时使用
- 需要用户输入表单域内容时。
- 提供组合型输入框,带搜索的输入框,还可以进行大小选择。
import { NzInputModule } from 'ng-zorro-antd/input';
代码演示
基本使用
基本使用。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-input-basic',
template: `
<input nz-input placeholder="Basic usage" [(ngModel)]="value" />
<br />
<br />
<input nz-input placeholder="Basic usage" [(ngModel)]="value" [disabled]="true" />
`
})
export class NzDemoInputBasicComponent {
value?: string;
}
前置/后置标签
用于配置一些固定组合。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-input-addon',
template: `
<div>
<nz-input-group nzAddOnBefore="Http://" nzAddOnAfter=".com">
<input type="text" nz-input [(ngModel)]="inputValue" />
</nz-input-group>
</div>
<div>
<nz-input-group [nzAddOnBefore]="addOnBeforeTemplate" [nzAddOnAfter]="addOnAfterTemplate">
<input type="text" nz-input [(ngModel)]="inputValue" />
</nz-input-group>
<ng-template #addOnBeforeTemplate>
<nz-select [ngModel]="'Http://'">
<nz-option nzLabel="Http://" nzValue="Http://"></nz-option>
<nz-option nzLabel="Https://" nzValue="Https://"></nz-option>
</nz-select>
</ng-template>
<ng-template #addOnAfterTemplate>
<nz-select [ngModel]="'.com'">
<nz-option nzLabel=".com" nzValue=".com"></nz-option>
<nz-option nzLabel=".jp" nzValue=".jp"></nz-option>
<nz-option nzLabel=".cn" nzValue=".cn"></nz-option>
<nz-option nzLabel=".org" nzValue=".org"></nz-option>
</nz-select>
</ng-template>
</div>
<div>
<nz-input-group nzAddOnAfterIcon="setting">
<input type="text" nz-input [(ngModel)]="inputValue" />
</nz-input-group>
</div>
`,
styles: [
`
div {
margin-bottom: 16px;
}
`
]
})
export class NzDemoInputAddonComponent {
inputValue: string = 'my site';
}
搜索框
带有搜索按钮的输入框。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-input-search-input',
template: `
<nz-input-group [nzSuffix]="suffixIconSearch">
<input type="text" nz-input placeholder="input search text" />
</nz-input-group>
<ng-template #suffixIconSearch>
<i nz-icon nzType="search"></i>
</ng-template>
<br />
<br />
<nz-input-group nzSearch [nzAddOnAfter]="suffixIconButton">
<input type="text" nz-input placeholder="input search text" />
</nz-input-group>
<ng-template #suffixIconButton>
<button nz-button nzType="primary" nzSearch><i nz-icon nzType="search"></i></button>
</ng-template>
<br />
<br />
<nz-input-group nzSearch nzSize="large" [nzAddOnAfter]="suffixButton">
<input type="text" nz-input placeholder="input search text" />
</nz-input-group>
<ng-template #suffixButton>
<button nz-button nzType="primary" nzSize="large" nzSearch>Search</button>
</ng-template>
`
})
export class NzDemoInputSearchInputComponent {}
适应文本高度的文本域
nzAutosize
属性适用于 textarea
节点,并且只有高度会自动变化。另外 nzAutosize
可以设定为一个对象,指定最小行数和最大行数。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-input-autosize-textarea',
template: `
<div>
<textarea nz-input placeholder="Autosize height based on content lines" nzAutosize></textarea>
<textarea
nz-input
placeholder="Autosize height with minimum and maximum number of lines"
[nzAutosize]="{ minRows: 2, maxRows: 6 }"
></textarea>
<textarea nz-input placeholder="Controlled autosize" [nzAutosize]="{ minRows: 3, maxRows: 5 }"></textarea>
</div>
`,
styles: [
`
textarea + textarea {
margin-top: 24px;
}
`
]
})
export class NzDemoInputAutosizeTextareaComponent {}
前缀和后缀
在输入框上添加前缀或后缀图标。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-input-presuffix',
template: `
<nz-input-group [nzSuffix]="suffixTemplateInfo" [nzPrefix]="prefixTemplateUser">
<input type="text" nz-input placeholder="Enter your username" />
</nz-input-group>
<ng-template #prefixTemplateUser><i nz-icon nzType="user"></i></ng-template>
<ng-template #suffixTemplateInfo><i nz-icon nz-tooltip nzTooltipTitle="Extra information" nzType="info-circle"></i></ng-template>
<br />
<br />
<nz-input-group nzSuffix="RMB" nzPrefix="¥">
<input type="text" nz-input />
</nz-input-group>
`
})
export class NzDemoInputPresuffixComponent {}
带移除图标
带移除图标的输入框,点击图标删除所有内容。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-input-allow-clear',
template: `
<nz-input-group [nzSuffix]="inputClearTpl">
<input type="text" nz-input [(ngModel)]="inputValue" placeholder="input with clear icon" />
</nz-input-group>
<ng-template #inputClearTpl
><i nz-icon class="ant-input-clear-icon" nzTheme="fill" nzType="close-circle" *ngIf="inputValue" (click)="inputValue = null"></i
></ng-template>
<br />
<br />
<nz-input-group [nzSuffix]="textAreaClearTpl" class="ant-input-affix-wrapper-textarea-with-clear-btn">
<textarea nz-input [(ngModel)]="textValue" placeholder="textarea with clear icon"></textarea>
</nz-input-group>
<ng-template #textAreaClearTpl
><i
nz-icon
class="ant-input-textarea-clear-icon"
nzTheme="fill"
nzType="close-circle"
*ngIf="textValue"
(click)="textValue = null"
></i
></ng-template>
`
})
export class NzDemoInputAllowClearComponent {
inputValue: string | null = null;
textValue: string | null = null;
}
三种大小
我们为 nz-input
输入框定义了三种尺寸(大、默认、小),高度分别为 40px
、32px
和 24px
。
注意: 在表单里面,我们只使用大尺寸的输入框。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-input-size',
template: `
<div class="example-input">
<input nz-input placeholder="large size" nzSize="large" />
<input nz-input placeholder="default size" nzSize="default" />
<input nz-input placeholder="small size" nzSize="small" />
</div>
`,
styles: [
`
.example-input .ant-input {
width: 200px;
margin: 0 8px 8px 0;
}
`
]
})
export class NzDemoInputSizeComponent {}
输入框组合
输入框的组合展现。
注意:使用 nzCompact
模式时,不需要通过 nz-col
来控制宽度。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-input-group',
template: `
<nz-input-group [nzSize]="'large'">
<div nz-row [nzGutter]="8">
<div nz-col nzSpan="5">
<input type="text" nz-input [ngModel]="'0571'" />
</div>
<div nz-col nzSpan="8">
<input type="text" nz-input [ngModel]="'26888888'" />
</div>
</div>
</nz-input-group>
<br />
<nz-input-group nzCompact>
<input type="text" nz-input [ngModel]="'0571'" style="width: 20%;" />
<input type="text" nz-input [ngModel]="'26888888'" style="width:30%;" />
</nz-input-group>
<br />
<nz-input-group nzCompact>
<nz-select [ngModel]="'Zhejiang'">
<nz-option [nzLabel]="'Zhejiang'" [nzValue]="'Zhejiang'"></nz-option>
<nz-option [nzLabel]="'Jiangsu'" [nzValue]="'Jiangsu'"></nz-option>
</nz-select>
<input type="text" nz-input [ngModel]="'Xihu District, Hangzhou'" style="width:50%;" />
</nz-input-group>
<br />
<nz-input-group nzCompact>
<nz-select [ngModel]="'Option1'">
<nz-option [nzLabel]="'Option1'" [nzValue]="'Option1'"></nz-option>
<nz-option [nzLabel]="'Option2'" [nzValue]="'Option2'"></nz-option>
</nz-select>
<input type="text" nz-input [ngModel]="'input content'" style="width:50%;" />
<nz-input-number></nz-input-number>
</nz-input-group>
<br />
<nz-input-group nzCompact>
<input type="text" nz-input [ngModel]="'input content'" style="width:50%;" />
<nz-date-picker></nz-date-picker>
</nz-input-group>
<br />
<nz-input-group nzCompact>
<nz-select [ngModel]="'Option1-1'">
<nz-option [nzLabel]="'Option1-1'" [nzValue]="'Option1-1'"></nz-option>
<nz-option [nzLabel]="'Option1-2'" [nzValue]="'Option1-2'"></nz-option>
</nz-select>
<nz-select [ngModel]="'Option2-1'">
<nz-option [nzLabel]="'Option2-1'" [nzValue]="'Option2-1'"></nz-option>
<nz-option [nzLabel]="'Option2-2'" [nzValue]="'Option2-2'"></nz-option>
</nz-select>
</nz-input-group>
<br />
<nz-input-group nzCompact>
<nz-select [ngModel]="'Between'">
<nz-option [nzLabel]="'Between'" [nzValue]="'Between'"></nz-option>
<nz-option [nzLabel]="'Except'" [nzValue]="'Except'"></nz-option>
</nz-select>
<input type="text" nz-input placeholder="Minimum" style="width:100px; text-align: center;" />
<input
type="text"
disabled
nz-input
placeholder="~"
style="width: 30px; border-left: 0px; pointer-events: none; background-color: rgb(255, 255, 255);"
/>
<input type="text" nz-input placeholder="Maximum" style="width: 100px; text-align: center; border-left: 0px;" />
</nz-input-group>
<br />
<nz-input-group nzCompact>
<nz-select [ngModel]="'Sign Up'">
<nz-option [nzLabel]="'Sign Up'" [nzValue]="'Sign Up'"></nz-option>
<nz-option [nzLabel]="'Sign In'" [nzValue]="'Sign In'"></nz-option>
</nz-select>
<input type="email" nz-input placeholder="Email" style="width: 200px;" />
</nz-input-group>
<br />
<nz-input-group nzCompact>
<nz-select [ngModel]="'Home'" style="width: 30%;">
<nz-option [nzLabel]="'Home'" [nzValue]="'Home'"></nz-option>
<nz-option [nzLabel]="'Company'" [nzValue]="'Company'"></nz-option>
</nz-select>
<nz-cascader [nzOptions]="options" style="width: 70%;"></nz-cascader>
</nz-input-group>
`
})
export class NzDemoInputGroupComponent {
options = [
{
value: 'zhejiang',
label: 'Zhejiang',
children: [
{
value: 'hangzhou',
label: 'Hangzhou',
children: [
{
value: 'xihu',
label: 'West Lake',
isLeaf: true
}
]
},
{
value: 'ningbo',
label: 'Ningbo',
isLeaf: true
}
]
},
{
value: 'jiangsu',
label: 'Jiangsu',
children: [
{
value: 'nanjing',
label: 'Nanjing',
children: [
{
value: 'zhonghuamen',
label: 'Zhong Hua Men',
isLeaf: true
}
]
}
]
}
];
}
文本域
用于多行输入。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-input-textarea',
template: `
<textarea rows="4" nz-input [(ngModel)]="inputValue"></textarea>
`
})
export class NzDemoInputTextareaComponent {
inputValue?: string;
}
输入时格式化展示
结合 Tooltip 组件,实现一个数值输入框,方便内容超长时的全量展现。
import { Component, ElementRef, ViewChild, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'nz-demo-input-tooltip',
encapsulation: ViewEncapsulation.None,
template: `
<input
#inputElement
style="width: 120px"
nz-input
nz-tooltip
nzTooltipTrigger="focus"
nzTooltipPlacement="topLeft"
nzOverlayClassName="numeric-input"
[ngModel]="value"
[nzTooltipTitle]="title"
placeholder="Input a number"
(ngModelChange)="onChange($event)"
(blur)="onBlur()"
/>
`,
styles: [
`
.numeric-input .ant-tooltip-inner {
min-width: 32px;
min-height: 37px;
}
.numeric-input .numeric-input-title {
font-size: 14px;
}
`
]
})
export class NzDemoInputTooltipComponent {
value = '';
title = 'Input a number';
@ViewChild('inputElement', { static: false }) inputElement?: ElementRef;
onChange(value: string): void {
this.updateValue(value);
}
// '.' at the end or only '-' in the input box.
onBlur(): void {
if (this.value.charAt(this.value.length - 1) === '.' || this.value === '-') {
this.updateValue(this.value.slice(0, -1));
}
}
updateValue(value: string): void {
const reg = /^-?(0|[1-9][0-9]*)(\.[0-9]*)?$/;
if ((!isNaN(+value) && reg.test(value)) || value === '' || value === '-') {
this.value = value;
}
this.inputElement!.nativeElement.value = this.value;
this.updateTitle();
}
updateTitle(): void {
this.title = (this.value !== '-' ? this.formatNumber(this.value) : '-') || 'Input a number';
}
formatNumber(value: string): string {
const stringValue = `${value}`;
const list = stringValue.split('.');
const prefix = list[0].charAt(0) === '-' ? '-' : '';
let num = prefix ? list[0].slice(1) : list[0];
let result = '';
while (num.length > 3) {
result = `,${num.slice(-3)}${result}`;
num = num.slice(0, num.length - 3);
}
if (num) {
result = num + result;
}
return `${prefix}${result}${list[1] ? `.${list[1]}` : ''}`;
}
}
密码框
密码框。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-input-password-input',
template: `
<nz-input-group [nzSuffix]="suffixTemplate">
<input [type]="passwordVisible ? 'text' : 'password'" nz-input placeholder="input password" [(ngModel)]="password" />
</nz-input-group>
<ng-template #suffixTemplate>
<i nz-icon [nzType]="passwordVisible ? 'eye-invisible' : 'eye'" (click)="passwordVisible = !passwordVisible"></i>
</ng-template>
`,
styles: [
`
i {
cursor: pointer;
}
`
]
})
export class NzDemoInputPasswordInputComponent {
passwordVisible = false;
password?: string;
}
无边框
没有边框。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-input-borderless',
template: `
<input nz-input placeholder="borderless" nzBorderless />
`
})
export class NzDemoInputBorderlessComponent {}
API
[nz-input]directive
nz-input 可以使用所有的W3C标准下的所有 使用方式 和 Angular对 input 的全部额外功能支持。
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
[nzSize] | 控件大小。注:标准表单内的输入框大小限制为 large | ‘large’ | ‘small’ | ‘default’ | ‘default’ |
[nzAutosize] | 只可以用于 textarea ,自适应内容高度,可设置为 boolean 或对象:{ minRows: 2, maxRows: 6 } | boolean | { minRows: number, maxRows: number } | false |
[nzBorderless] | 是否隐藏边框 | boolean | false |
nz-input-groupcomponent
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
[nzAddOnAfter] | 带标签的 input,设置后置标签,可以与 nzAddOnBefore 配合使用 | string | TemplateRef<void> | - |
[nzAddOnBefore] | 带标签的 input,设置前置标签,可以与 nzAddOnAfter 配合使用 | string | TemplateRef<void> | - |
[nzPrefix] | 带有前缀图标的 input,可以与 nzSuffix 配合使用 | string | TemplateRef<void> | - |
[nzSuffix] | 带有后缀图标的 input,可以与 nzPrefix 配合使用 | string | TemplateRef<void> | - |
[nzCompact] | 是否用紧凑模式 | boolean | false |
[nzSearch] | 是否用搜索框 | boolean | false |
[nzSize] | nz-input-group 中所有的 nz-input 的大小 | ‘large’ | ‘small’ | ‘default’ | ‘default’ |