InputItem文本输入
用于接受单行文本。
规则
- 支持通过键盘或者剪切板输入文本。
- 通过光标可以在水平方向进行移动。
- 对特定格式的文本进行处理,eg:隐藏密码。
代码演示
基本用法
最简单的用法。
import { Component } from '@angular/core';
import { ToastService } from 'ng-zorro-antd-mobile';
@Component({
selector: 'demo-input-item-basic',
template: `
<div class="am-demo-page">
<div style="padding: 15px;font-size: 16px; color:#000">Basic</div>
<div class="am-list" style="margin:0;">
<div class="am-list-header">Money input</div>
<div class="am-list-body">
<InputItem
[type]="'money'"
[clear]="true"
[placeholder]="'start from left'"
[moneyKeyboardAlign]="'left'"
[content]="'光标在左'"
>
</InputItem>
<InputItem
[type]="'money'"
[clear]="true"
[placeholder]="'start from rigth'"
[content]="'光标在右'"
></InputItem>
<InputItem
[type]="'money'"
[clear]="true"
[placeholder]="'money format'"
[focus]="numberFocus"
[content]="'数字键盘'"
>
</InputItem>
<div class="am-list-item am-list-item-middle">
<div class="am-list-line">
<div class="am-list-content" style="width:100%;color:#108ee9;text-align:center" (click)="clickFocus()">
click to focus
</div>
</div>
<div class="am-list-ripple" style="display: none;"></div>
</div>
</div>
</div>
<div class="am-list" style="margin:0;">
<div class="am-list-header">Customize to focus</div>
<div class="am-list-body">
<InputItem [clear]="true" [placeholder]="'auto focus'" [focus]="autoFocus" [content]="'标题'"></InputItem>
<InputItem
[clear]="true"
[placeholder]="'click the button below to focus'"
[focus]="inputFocus"
[content]="'标题'"
>
</InputItem>
<div class="am-list-item am-list-item-middle">
<div class="am-list-line">
<div
class="am-list-content"
style="width:100%;color:#108ee9;text-align:center"
(click)="clickFocusInput()"
>
click to focus
</div>
</div>
<div class="am-list-ripple" style="display: none;"></div>
</div>
</div>
</div>
<div class="am-list" style="margin:0;">
<div class="am-list-header">Click label to focus input</div>
<div class="am-list-body">
<InputItem [placeholder]="'Click label to focus input'" [focus]="titleFocus" [content]="contentlabel">
<ng-template #contentlabel>
<span (click)="clickTitle()">标题</span>
</ng-template>
</InputItem>
</div>
</div>
<div class="am-list" style="margin:0;">
<div class="am-list-header">Show clear</div>
<div class="am-list-body">
<InputItem [clear]="true" [placeholder]="'displayed clear while typing'" [content]="'标题'"></InputItem>
</div>
</div>
<div class="am-list" style="margin:0;">
<div class="am-list-header">Number of words for the title</div>
<div class="am-list-body">
<InputItem
[clear]="true"
[labelNumber]="5"
[placeholder]="'limited title length'"
[content]="'标题过长超过5个字符'"
>
</InputItem>
</div>
</div>
<div class="am-list" style="margin:0;">
<div class="am-list-header">Custom title(text / image / empty)</div>
<div class="am-list-body">
<InputItem [placeholder]="'no label'"></InputItem>
<InputItem [placeholder]="'title can be icon,image or text'" [content]="content">
<ng-template #content>
<div
style="background-image:url(https://zos.alipayobjects.com/rmsportal/DfkJHaJGgMghpXdqNaKF.png); background-size:cover;height:22px; width: 22px "
></div>
</ng-template>
</InputItem>
</div>
</div>
<div class="am-list" style="margin:0;">
<div class="am-list-header">Customize the extra content in the right</div>
<div class="am-list-body">
<InputItem [placeholder]="'0.00'" [extra]="'¥'" [content]="'价格'"></InputItem>
</div>
</div>
<div class="am-list" style="margin:0;">
<div class="am-list-header">Format</div>
<div class="am-list-body">
<InputItem [type]="'bankCard'" [defaultValue]="'8888 8888 8888 8888'" [content]="'银行卡'"></InputItem>
<InputItem
[type]="'phone'"
[placeholder]="'186 1234 1234'"
(onChange)="inputChange($event)"
[content]="'手机号码'"
>
</InputItem>
<InputItem [type]="'password'" [placeholder]="'****'" [content]="'密码'"></InputItem>
<InputItem
[type]="'number'"
[placeholder]="'click to show number keyboard'"
[content]="'数字键盘'"
></InputItem>
</div>
</div>
<div class="am-list" style="margin:0;">
<div class="am-list-header">Not editable / Disabled</div>
<div class="am-list-body">
<InputItem [editable]="false" [value]="'not editable'" [content]="'姓名'"></InputItem>
<InputItem [disabled]="true" [value]="'style of disabled InputItem'" [content]="'姓名'"></InputItem>
</div>
</div>
<div class="am-list" style="margin:0;">
<div class="am-list-header">Confirm when typing</div>
<div class="am-list-body">
<InputItem
[type]="'phone'"
[placeholder]="'input your phone'"
[value]="value"
[error]="error"
[content]="'手机号码'"
(onErrorClick)="inputErrorClick($event)"
(onChange)="inputChange($event)"
>
</InputItem>
</div>
</div>
</div>
`,
styles: [
`
.am-list-body {
position: relative;
background-color: #fff;
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
}
.am-list-header {
padding: 15px 15px 9px 15px;
font-size: 14px;
color: #888;
width: 100%;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
/deep/ .am-list-body InputItem:not(:last-child) .am-list-line {
border-bottom: 1px solid #ddd;
}
.am-list-item .am-list-line .am-list-content {
-webkit-box-flex: 1;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
color: #000;
font-size: 17px;
line-height: 1.5;
text-align: left;
width: auto;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
padding-top: 7px;
padding-bottom: 7px;
}
`
]
})
export class DemoInputItemBasicComponent {
value = '';
error = false;
numberFocus = {
focus: false,
date: new Date()
};
inputFocus = {
focus: false,
date: new Date()
};
titleFocus = {
focus: false,
date: new Date()
};
autoFocus = { focus: true, date: new Date() };
constructor(private _toast: ToastService) {}
inputErrorClick(e) {
this._toast.info('Please enter 11 digits');
}
inputChange(e) {
const value = e.replace(/\s/g, '');
if (value.length < 11 && value.length > 0) {
this.error = true;
} else {
this.error = false;
}
this.value = e;
}
clickFocus() {
this.numberFocus = {
focus: true,
date: new Date()
};
}
clickFocusInput() {
this.inputFocus = {
focus: true,
date: new Date()
};
}
clickTitle() {
this.titleFocus = {
focus: true,
date: new Date()
};
}
}
API
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
[type] | 可以是银行卡bankCard ; 手机号phone (此时最大长度固定为11,maxLength 设置无效); 密码password ; 数字number (为了尽量唤起带小数点 的数字键盘,此类型并不是原生 number,而是<input type=”text” pattern=”[0-9]*” /> ); digit (表示原生的 number 类型); money 以及其他标准 html input type 类型 | string | ‘text’ |
[value] | value | string | - |
[defaultValue] | 设置初始默认值 | string | - |
[placeholder] | placeholder | string | - |
[editable] | 是否可编辑 | boolean | true |
[disabled] | 是否禁用 | boolean | false |
[clear] | 是否带清除功能(仅editable 为true ,disabled 为false 才生效) | boolean | false |
[maxLength] | 最大长度 | number | - |
[error] | 是否显示报错样式 | boolean | false |
[extra] | 右边注释 | string | TemplateRef | - |
[labelNumber] | 标签的文字个数,可用2-7 之间的数字 | number | 5 |
[content] | 标签内容支持string和模版 | string | TemplateRef | - |
[updatePlaceholder] | 当清除内容时,是否将清除前的内容替换到 placeholder 中 | boolean | false |
[prefixListCls] | 列表 className 前缀 | string | ‘am-list’ |
[moneyKeyboardAlign] | 文字排版起始方向, 只有 type=’money’ 支持 | ‘left’ | ‘right’ | ‘right’ |
[locale] | 国际化,可覆盖全局的配置, 当type 为money ,可以自定义确认按钮的文案 | { 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> | - |
注意:
InputItem
当type=number
时不支持输入负号, 你可以利用type=text
来自己实现。