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

InputItem文本输入 - 图1

API

参数说明类型默认值
[type]可以是银行卡bankCard; 手机号phone(此时最大长度固定为11,maxLength设置无效); 密码password; 数字number(为了尽量唤起带小数点的数字键盘,此类型并不是原生 number,而是<input type=”text” pattern=”[0-9]*” />); digit(表示原生的 number 类型); money 以及其他标准 html input type 类型string‘text’
[value]valuestring-
[defaultValue]设置初始默认值string-
[placeholder]placeholderstring-
[editable]是否可编辑booleantrue
[disabled]是否禁用booleanfalse
[clear]是否带清除功能(仅editabletrue,disabledfalse才生效)booleanfalse
[maxLength]最大长度number-
[error]是否显示报错样式booleanfalse
[extra]右边注释string | TemplateRef-
[labelNumber]标签的文字个数,可用2-7之间的数字number5
[content]标签内容支持string和模版string | TemplateRef-
[updatePlaceholder]当清除内容时,是否将清除前的内容替换到 placeholder 中booleanfalse
[prefixListCls]列表 className 前缀string‘am-list’
[moneyKeyboardAlign]文字排版起始方向, 只有 type=’money’ 支持‘left’ | ‘right’‘right’
[locale]国际化,可覆盖全局的配置, 当typemoney,可以自定义确认按钮的文案{ confirmLabel }-
(onErrorClick)点击报错 icon 触发的回调函数EventEmitter<object>-
(onExtraClick)extra 点击事件触发的回调函数EventEmitter<object>-
(onChange)change 事件触发的回调函数EventEmitter<string>-
(onBlur)blur 事件触发的回调函数EventEmitter<string>-
(onFocus)focus 事件触发的回调函数EventEmitter<string>-
(focus)使 input 聚焦EventEmitter<void>-

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