- InputItem文本输入
- 规则
- 代码演示
- API
- InputItem methods
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'"
>光标在左</InputItem>
<InputItem [type]="'money'"
[clear]="true"
[placeholder]="'start from rigth'"
>光标在右</InputItem>
<InputItem [type]="'money'"
[clear]="true"
[placeholder]="'money format'"
[focus]="numberFocus"
>数字键盘</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"
>标题</InputItem>
<InputItem [clear]="true"
[placeholder]="'click the button below to focus'"
[focus]="inputFocus"
>标题</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">
<span (click)="clickTitle()">标题</span>
</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'">标题</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'"
>标题过长超过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'">
<div style="background-image:url(https://zos.alipayobjects.com/rmsportal/DfkJHaJGgMghpXdqNaKF.png); background-size:cover;height:22px; width: 22px "></div>
</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]="'¥'">价格</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'">银行卡</InputItem>
<InputItem [type]="'phone'" [placeholder]="'186 1234 1234'" (onChange)="inputChange($event)">手机号码</InputItem>
<InputItem [type]="'password'" [placeholder]="'****'">密码</InputItem>
<InputItem [type]="'number'" [placeholder]="'click to show number keyboard'">数字键盘</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'">姓名</InputItem>
<InputItem [disabled]="true" [value]="'style of disabled InputItem'">姓名</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"
(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) {
ToastService.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 | 是否可编辑 | bool | true |
disabled | 是否禁用 | bool | false |
clear | 是否带清除功能(仅editable 为true ,disabled 为false 才生效) | bool | false |
maxLength | 最大长度 | number | 无 |
onChange | change 事件触发的回调函数 | (val: string): void | - |
onBlur | blur 事件触发的回调函数 | (val: string): void | - |
onFocus | focus 事件触发的回调函数 | (val: string): void | - |
error | 报错样式 | bool | false |
onErrorClick | 点击报错 icon 触发的回调函数 | (e: Object): void | 无 |
extra | 右边注释 | string or TemplateRef | '' |
onExtraClick | extra 点击事件触发的回调函数 | (e: Object): void | 无 |
labelNumber | 标签的文字个数,可用2-7 之间的数字 | number | 5 |
updatePlaceholder | 当清除内容时,是否将清除前的内容替换到 placeholder 中 | bool | false |
prefixListCls | 列表 className 前缀 | String | am-list |
moneyKeyboardAlign | 文字排版起始方向, 只有 type='money' 支持, 可选为 'left' , 'right' | String | 'right' |
locale | 国际化,可覆盖全局的配置, 当type 为money ,可以自定义确认按钮的文案。 | Object: { confirmLabel } | 无 |
注意:
InputItem
当type=number
时不支持输入负号, 你可以利用type=text
来自己实现。
InputItem methods
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
focus | 使 input 聚焦 | (): void | - |