• InputItem文本输入
    • 规则
  • 代码演示
  • API
  • InputItem methods

    InputItem文本输入

    用于接受单行文本。

    规则

    • 支持通过键盘或者剪切板输入文本。
    • 通过光标可以在水平方向进行移动。
    • 对特定格式的文本进行处理,eg:隐藏密码。

    代码演示

    基本用法

    最简单的用法。

    1. import { Component } from '@angular/core';
    2. import { ToastService } from 'ng-zorro-antd-mobile';
    3. @Component({
    4. selector: 'demo-input-item-basic',
    5. template: `
    6. <div class="am-demo-page">
    7. <div style="padding: 15px;font-size: 16px; color:#000">Basic</div>
    8. <div class="am-list" style="margin:0;">
    9. <div class="am-list-header">Money input</div>
    10. <div class="am-list-body">
    11. <InputItem [type]="'money'"
    12. [clear]="true"
    13. [placeholder]="'start from left'"
    14. [moneyKeyboardAlign]="'left'"
    15. >光标在左</InputItem>
    16. <InputItem [type]="'money'"
    17. [clear]="true"
    18. [placeholder]="'start from rigth'"
    19. >光标在右</InputItem>
    20. <InputItem [type]="'money'"
    21. [clear]="true"
    22. [placeholder]="'money format'"
    23. [focus]="numberFocus"
    24. >数字键盘</InputItem>
    25. <div class="am-list-item am-list-item-middle">
    26. <div class="am-list-line">
    27. <div class="am-list-content"
    28. style="width:100%;color:#108ee9;text-align:center"
    29. (click)="clickFocus()"
    30. >click to focus</div>
    31. </div>
    32. <div class="am-list-ripple" style="display: none;"></div>
    33. </div>
    34. </div>
    35. </div>
    36. <div class="am-list" style="margin:0;">
    37. <div class="am-list-header">Customize to focus</div>
    38. <div class="am-list-body">
    39. <InputItem [clear]="true"
    40. [placeholder]="'auto focus'"
    41. [focus]="autoFocus"
    42. >标题</InputItem>
    43. <InputItem [clear]="true"
    44. [placeholder]="'click the button below to focus'"
    45. [focus]="inputFocus"
    46. >标题</InputItem>
    47. <div class="am-list-item am-list-item-middle">
    48. <div class="am-list-line">
    49. <div class="am-list-content"
    50. style="width:100%;color:#108ee9;text-align:center"
    51. (click)="clickFocusInput()"
    52. >click to focus</div>
    53. </div>
    54. <div class="am-list-ripple" style="display: none;"></div>
    55. </div>
    56. </div>
    57. </div>
    58. <div class="am-list" style="margin:0;">
    59. <div class="am-list-header">Click label to focus input</div>
    60. <div class="am-list-body">
    61. <InputItem [placeholder]="'Click label to focus input'" [focus]="titleFocus">
    62. <span (click)="clickTitle()">标题</span>
    63. </InputItem>
    64. </div>
    65. </div>
    66. <div class="am-list" style="margin:0;">
    67. <div class="am-list-header">Show clear</div>
    68. <div class="am-list-body">
    69. <InputItem [clear]="true" [placeholder]="'displayed clear while typing'">标题</InputItem>
    70. </div>
    71. </div>
    72. <div class="am-list" style="margin:0;">
    73. <div class="am-list-header">Number of words for the title</div>
    74. <div class="am-list-body">
    75. <InputItem [clear]="true"
    76. [labelNumber]="5"
    77. [placeholder]="'limited title length'"
    78. >标题过长超过5个字符</InputItem>
    79. </div>
    80. </div>
    81. <div class="am-list" style="margin:0;">
    82. <div class="am-list-header">Custom title(text / image / empty)</div>
    83. <div class="am-list-body">
    84. <InputItem [placeholder]="'no label'"></InputItem>
    85. <InputItem [placeholder]="'title can be icon,image or text'">
    86. <div style="background-image:url(https://zos.alipayobjects.com/rmsportal/DfkJHaJGgMghpXdqNaKF.png); background-size:cover;height:22px; width: 22px "></div>
    87. </InputItem>
    88. </div>
    89. </div>
    90. <div class="am-list" style="margin:0;">
    91. <div class="am-list-header">Customize the extra content in the right</div>
    92. <div class="am-list-body">
    93. <InputItem [placeholder]="'0.00'" [extra]="'¥'">价格</InputItem>
    94. </div>
    95. </div>
    96. <div class="am-list" style="margin:0;">
    97. <div class="am-list-header">Format</div>
    98. <div class="am-list-body">
    99. <InputItem [type]="'bankCard'" [defaultValue]="'8888 8888 8888 8888'">银行卡</InputItem>
    100. <InputItem [type]="'phone'" [placeholder]="'186 1234 1234'" (onChange)="inputChange($event)">手机号码</InputItem>
    101. <InputItem [type]="'password'" [placeholder]="'****'">密码</InputItem>
    102. <InputItem [type]="'number'" [placeholder]="'click to show number keyboard'">数字键盘</InputItem>
    103. </div>
    104. </div>
    105. <div class="am-list" style="margin:0;">
    106. <div class="am-list-header">Not editable / Disabled</div>
    107. <div class="am-list-body">
    108. <InputItem [editable]="false" [value]="'not editable'">姓名</InputItem>
    109. <InputItem [disabled]="true" [value]="'style of disabled InputItem'">姓名</InputItem>
    110. </div>
    111. </div>
    112. <div class="am-list" style="margin:0;">
    113. <div class="am-list-header">Confirm when typing</div>
    114. <div class="am-list-body">
    115. <InputItem [type]="'phone'"
    116. [placeholder]="'input your phone'"
    117. [value]="value"
    118. [error]="error"
    119. (onErrorClick)="inputErrorClick($event)"
    120. (onChange)="inputChange($event)"
    121. >手机号码</InputItem>
    122. </div>
    123. </div>
    124. </div>
    125. `,
    126. styles: [
    127. `
    128. .am-list-body {
    129. position: relative;
    130. background-color: #fff;
    131. border-top: 1px solid #ddd;
    132. border-bottom: 1px solid #ddd;
    133. }
    134. .am-list-header {
    135. padding: 15px 15px 9px 15px;
    136. font-size: 14px;
    137. color: #888;
    138. width: 100%;
    139. -webkit-box-sizing: border-box;
    140. box-sizing: border-box;
    141. }
    142. /deep/ .am-list-body InputItem:not(:last-child) .am-list-line {
    143. border-bottom: 1px solid #ddd;
    144. }
    145. .am-list-item .am-list-line .am-list-content {
    146. -webkit-box-flex: 1;
    147. -webkit-flex: 1;
    148. -ms-flex: 1;
    149. flex: 1;
    150. color: #000;
    151. font-size: 17px;
    152. line-height: 1.5;
    153. text-align: left;
    154. width: auto;
    155. overflow: hidden;
    156. text-overflow: ellipsis;
    157. white-space: nowrap;
    158. padding-top: 7px;
    159. padding-bottom: 7px;
    160. }
    161. `
    162. ]
    163. })
    164. export class DemoInputItemBasicComponent {
    165. value = '';
    166. error = false;
    167. numberFocus = {
    168. focus: false,
    169. date: new Date()
    170. };
    171. inputFocus = {
    172. focus: false,
    173. date: new Date()
    174. };
    175. titleFocus = {
    176. focus: false,
    177. date: new Date()
    178. };
    179. autoFocus = { focus: true, date: new Date() };
    180. constructor(private _toast: ToastService) {}
    181. inputErrorClick(e) {
    182. ToastService.info('Please enter 11 digits');
    183. }
    184. inputChange(e) {
    185. const value = e.replace(/\s/g, '');
    186. if (value.length < 11 && value.length > 0) {
    187. this.error = true;
    188. } else {
    189. this.error = false;
    190. }
    191. this.value = e;
    192. }
    193. clickFocus() {
    194. this.numberFocus = {
    195. focus: true,
    196. date: new Date()
    197. };
    198. }
    199. clickFocusInput() {
    200. this.inputFocus = {
    201. focus: true,
    202. date: new Date()
    203. };
    204. }
    205. clickTitle() {
    206. this.titleFocus = {
    207. focus: true,
    208. date: new Date()
    209. };
    210. }
    211. }

    InputItem 文本输入 - 图1

    API

    属性说明类型默认值
    type可以是银行卡bankCard; 手机号phone(此时最大长度固定为11,maxLength设置无效); 密码password; 数字number(为了尽量唤起带小数点的数字键盘,此类型并不是原生 number,而是<input type="text" pattern="[0-9]*" />); digit(表示原生的 number 类型); money 以及其他标准 html input type 类型Stringtext
    valuevalueString
    defaultValue设置初始默认值String-
    placeholderplaceholderString''
    editable是否可编辑booltrue
    disabled是否禁用boolfalse
    clear是否带清除功能(仅editabletrue,disabledfalse才生效)boolfalse
    maxLength最大长度number
    onChangechange 事件触发的回调函数(val: string): void-
    onBlurblur 事件触发的回调函数(val: string): void-
    onFocusfocus 事件触发的回调函数(val: string): void-
    error报错样式boolfalse
    onErrorClick点击报错 icon 触发的回调函数(e: Object): void
    extra右边注释string or TemplateRef''
    onExtraClickextra 点击事件触发的回调函数(e: Object): void
    labelNumber标签的文字个数,可用2-7之间的数字number5
    updatePlaceholder当清除内容时,是否将清除前的内容替换到 placeholder 中boolfalse
    prefixListCls列表 className 前缀Stringam-list
    moneyKeyboardAlign文字排版起始方向, 只有 type='money' 支持, 可选为 'left', 'right'String'right'
    locale国际化,可覆盖全局的配置, 当typemoney,可以自定义确认按钮的文案。Object: { confirmLabel }

    注意: InputItemtype=number 时不支持输入负号, 你可以利用 type=text 来自己实现。

    InputItem methods

    属性说明类型默认值
    focus使 input 聚焦(): void-