Input输入框

    • 需要用户输入表单域内容时。
    • 提供组合型输入框,带搜索的输入框,还可以进行大小选择。

    基本使用

    基本使用。

    1. @Component({
    2. selector: 'nz-demo-input-basic',
    3. template: `
    4. <input nz-input placeholder="Basic usage" [(ngModel)]="value" />
    5. <br />
    6. <br />
    7. <input nz-input placeholder="Basic usage" [(ngModel)]="value" [disabled]="true" />
    8. `
    9. })
    10. export class NzDemoInputBasicComponent {
    11. value?: string;
    12. }

    Input输入框 - 图4

    前置/后置标签

    用于配置一些固定组合。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-addon',
    4. template: `
    5. <div>
    6. <nz-input-group nzAddOnBefore="Http://" nzAddOnAfter=".com">
    7. <input type="text" nz-input [(ngModel)]="inputValue" />
    8. </nz-input-group>
    9. </div>
    10. <div>
    11. <nz-input-group [nzAddOnBefore]="addOnBeforeTemplate" [nzAddOnAfter]="addOnAfterTemplate">
    12. <input type="text" nz-input [(ngModel)]="inputValue" />
    13. </nz-input-group>
    14. <ng-template #addOnBeforeTemplate>
    15. <nz-select [ngModel]="'Http://'">
    16. <nz-option nzLabel="Http://" nzValue="Http://"></nz-option>
    17. <nz-option nzLabel="Https://" nzValue="Https://"></nz-option>
    18. </nz-select>
    19. </ng-template>
    20. <ng-template #addOnAfterTemplate>
    21. <nz-select [ngModel]="'.com'">
    22. <nz-option nzLabel=".com" nzValue=".com"></nz-option>
    23. <nz-option nzLabel=".jp" nzValue=".jp"></nz-option>
    24. <nz-option nzLabel=".cn" nzValue=".cn"></nz-option>
    25. <nz-option nzLabel=".org" nzValue=".org"></nz-option>
    26. </nz-select>
    27. </ng-template>
    28. </div>
    29. <div>
    30. <nz-input-group nzAddOnAfterIcon="setting">
    31. <input type="text" nz-input [(ngModel)]="inputValue" />
    32. </nz-input-group>
    33. </div>
    34. `,
    35. styles: [
    36. `
    37. div {
    38. margin-bottom: 16px;
    39. }
    40. `
    41. ]
    42. })
    43. export class NzDemoInputAddonComponent {
    44. inputValue: string = 'my site';
    45. }

    搜索框

    带有搜索按钮的输入框。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-search-input',
    4. template: `
    5. <nz-input-group [nzSuffix]="suffixIconSearch">
    6. <input type="text" nz-input placeholder="input search text" />
    7. </nz-input-group>
    8. <ng-template #suffixIconSearch>
    9. <i nz-icon nzType="search"></i>
    10. </ng-template>
    11. <br />
    12. <br />
    13. <nz-input-group nzSearch [nzAddOnAfter]="suffixIconButton">
    14. <input type="text" nz-input placeholder="input search text" />
    15. </nz-input-group>
    16. <ng-template #suffixIconButton>
    17. <button nz-button nzType="primary" nzSearch><i nz-icon nzType="search"></i></button>
    18. </ng-template>
    19. <br />
    20. <br />
    21. <nz-input-group nzSearch nzSize="large" [nzAddOnAfter]="suffixButton">
    22. <input type="text" nz-input placeholder="input search text" />
    23. </nz-input-group>
    24. <ng-template #suffixButton>
    25. <button nz-button nzType="primary" nzSize="large" nzSearch>Search</button>
    26. `
    27. })
    28. export class NzDemoInputSearchInputComponent {}

    Input输入框 - 图6

    适应文本高度的文本域

    前缀和后缀

    在输入框上添加前缀或后缀图标。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-presuffix',
    4. template: `
    5. <nz-input-group [nzSuffix]="suffixTemplateInfo" [nzPrefix]="prefixTemplateUser">
    6. <input type="text" nz-input placeholder="Enter your username" />
    7. </nz-input-group>
    8. <ng-template #prefixTemplateUser><i nz-icon nzType="user"></i></ng-template>
    9. <ng-template #suffixTemplateInfo><i nz-icon nz-tooltip nzTooltipTitle="Extra information" nzType="info-circle"></i></ng-template>
    10. <br />
    11. <br />
    12. <nz-input-group nzSuffix="RMB" nzPrefix="¥">
    13. </nz-input-group>
    14. `
    15. })
    16. export class NzDemoInputPresuffixComponent {}

    Input输入框 - 图8

    带移除图标

    带移除图标的输入框,点击图标删除所有内容。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-allow-clear',
    4. template: `
    5. <nz-input-group [nzSuffix]="inputClearTpl">
    6. <input type="text" nz-input [(ngModel)]="inputValue" placeholder="input with clear icon" />
    7. </nz-input-group>
    8. <ng-template #inputClearTpl
    9. ><i nz-icon class="ant-input-clear-icon" nzTheme="fill" nzType="close-circle" *ngIf="inputValue" (click)="inputValue = null"></i
    10. ></ng-template>
    11. <br />
    12. <br />
    13. <nz-input-group [nzSuffix]="textAreaClearTpl" class="ant-input-affix-wrapper-textarea-with-clear-btn">
    14. <textarea nz-input [(ngModel)]="textValue" placeholder="textarea with clear icon"></textarea>
    15. </nz-input-group>
    16. <ng-template #textAreaClearTpl
    17. ><i
    18. nz-icon
    19. class="ant-input-textarea-clear-icon"
    20. nzTheme="fill"
    21. nzType="close-circle"
    22. *ngIf="textValue"
    23. (click)="textValue = null"
    24. ></i
    25. ></ng-template>
    26. `
    27. })
    28. export class NzDemoInputAllowClearComponent {
    29. inputValue: string | null = null;
    30. textValue: string | null = null;
    31. }

    三种大小

    我们为 nz-input 输入框定义了三种尺寸(大、默认、小),高度分别为 40px32px24px

    注意: 在表单里面,我们只使用大尺寸的输入框。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-size',
    4. template: `
    5. <div class="example-input">
    6. <input nz-input placeholder="large size" nzSize="large" />
    7. <input nz-input placeholder="default size" nzSize="default" />
    8. <input nz-input placeholder="small size" nzSize="small" />
    9. </div>
    10. `,
    11. styles: [
    12. `
    13. .example-input .ant-input {
    14. width: 200px;
    15. margin: 0 8px 8px 0;
    16. }
    17. `
    18. ]
    19. })
    20. export class NzDemoInputSizeComponent {}

    Input输入框 - 图10

    输入框的组合展现。

    注意:使用 nzCompact 模式时,不需要通过 nz-col 来控制宽度。

    文本域

    用于多行输入。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-textarea',
    4. template: `
    5. <textarea rows="4" nz-input [(ngModel)]="inputValue"></textarea>
    6. `
    7. })
    8. export class NzDemoInputTextareaComponent {
    9. inputValue?: string;
    10. }

    Input输入框 - 图12

    输入时格式化展示

    结合 Tooltip 组件,实现一个数值输入框,方便内容超长时的全量展现。

    1. import { Component, ElementRef, ViewChild, ViewEncapsulation } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-tooltip',
    4. encapsulation: ViewEncapsulation.None,
    5. template: `
    6. <input
    7. #inputElement
    8. nz-input
    9. nz-tooltip
    10. nzTooltipTrigger="focus"
    11. nzTooltipPlacement="topLeft"
    12. nzOverlayClassName="numeric-input"
    13. [ngModel]="value"
    14. [nzTooltipTitle]="title"
    15. placeholder="Input a number"
    16. (ngModelChange)="onChange($event)"
    17. (blur)="onBlur()"
    18. />
    19. `,
    20. styles: [
    21. `
    22. .numeric-input .ant-tooltip-inner {
    23. min-width: 32px;
    24. min-height: 37px;
    25. }
    26. .numeric-input .numeric-input-title {
    27. font-size: 14px;
    28. }
    29. ]
    30. })
    31. export class NzDemoInputTooltipComponent {
    32. value = '';
    33. title = 'Input a number';
    34. @ViewChild('inputElement', { static: false }) inputElement?: ElementRef;
    35. onChange(value: string): void {
    36. this.updateValue(value);
    37. }
    38. // '.' at the end or only '-' in the input box.
    39. onBlur(): void {
    40. if (this.value.charAt(this.value.length - 1) === '.' || this.value === '-') {
    41. this.updateValue(this.value.slice(0, -1));
    42. }
    43. }
    44. updateValue(value: string): void {
    45. const reg = /^-?(0|[1-9][0-9]*)(\.[0-9]*)?$/;
    46. if ((!isNaN(+value) && reg.test(value)) || value === '' || value === '-') {
    47. this.value = value;
    48. }
    49. this.inputElement!.nativeElement.value = this.value;
    50. this.updateTitle();
    51. }
    52. updateTitle(): void {
    53. this.title = (this.value !== '-' ? this.formatNumber(this.value) : '-') || 'Input a number';
    54. }
    55. formatNumber(value: string): string {
    56. const stringValue = `${value}`;
    57. const list = stringValue.split('.');
    58. const prefix = list[0].charAt(0) === '-' ? '-' : '';
    59. let num = prefix ? list[0].slice(1) : list[0];
    60. let result = '';
    61. while (num.length > 3) {
    62. result = `,${num.slice(-3)}${result}`;
    63. num = num.slice(0, num.length - 3);
    64. }
    65. if (num) {
    66. result = num + result;
    67. }
    68. return `${prefix}${result}${list[1] ? `.${list[1]}` : ''}`;
    69. }
    70. }

    密码框

    密码框。

    1. import { Component } from '@angular/core';
    2. @Component({
    3. selector: 'nz-demo-input-password-input',
    4. template: `
    5. <nz-input-group [nzSuffix]="suffixTemplate">
    6. <input [type]="passwordVisible ? 'text' : 'password'" nz-input placeholder="input password" [(ngModel)]="password" />
    7. </nz-input-group>
    8. <ng-template #suffixTemplate>
    9. <i nz-icon [nzType]="passwordVisible ? 'eye-invisible' : 'eye'" (click)="passwordVisible = !passwordVisible"></i>
    10. </ng-template>
    11. `,
    12. styles: [
    13. `
    14. i {
    15. cursor: pointer;
    16. }
    17. `
    18. ]
    19. })
    20. export class NzDemoInputPasswordInputComponent {
    21. passwordVisible = false;
    22. }

    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]是否用紧凑模式booleanfalse
    [nzSearch]是否用搜索框booleanfalse
    [nzSize]nz-input-group 中所有的 nz-input 的大小‘large’ | ‘small’ | ‘default’‘default’