Input 输入框
如果项目中使用的是 0.x 版本的基础组件(@icedesign/base, @ali/ice, @alife/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
npm install @alifd/next@latest -S
开发指南
何时使用
表单输入,一般配合Form使用
API
Input
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
value | 当前值 | String/Number | - |
size | 尺寸可选值:'small'(小)'medium'(中)'large'(大) | Enum | 'medium' |
defaultValue | 初始化值 | String/Number | - |
onChange | 发生改变的时候触发的回调签名:Function(value: String, e: Event) => void参数:value: {String} 数据e: {Event} DOM事件对象 | Function | func.noop |
onKeyDown | 键盘按下的时候触发的回调签名:Function(e: Event, opts: Object) => void参数:e: {Event} DOM事件对象opts: {Object} 可扩展的附加信息: - opts.overMaxLength: {Boolean} 已超出最大长度 - opts.beTrimed: {Boolean} 输入的空格被清理 | Function | func.noop |
disabled | 禁用状态 | Boolean | false |
maxLength | 最大长度 | Number | null |
hasLimitHint | 是否展现最大长度样式 | Boolean | false |
cutString | 当设置了maxLength时,是否截断超出字符串 | Boolean | true |
readOnly | 只读 | Boolean | false |
trim | onChange返回会自动去除头尾空字符 | Boolean | false |
placeholder | 输入提示 | String | - |
onFocus | 获取焦点时候触发的回调签名:Function(e: Event) => void参数:e: {Event} DOM事件对象 | Function | func.noop |
onBlur | 失去焦点时候触发的回调签名:Function(e: Event) => void参数:e: {Event} DOM事件对象 | Function | func.noop |
getValueLength | 自定义字符串计算长度方式签名:Function(value: String) => Number参数:value: {String} 数据返回值:{Number} 自定义长度 | Function | func.noop |
htmlType | 原生type | String | - |
name | name | String | - |
state | 状态可选值:'error'(错误)'loading'(校验中)'success'(成功) | Enum | - |
label | label | ReactNode | - |
hasClear | 是否出现clear按钮 | Boolean | - |
hasBorder | 是否有边框 | Boolean | true |
onPressEnter | 按下回车的回调签名:Function() => void | Function | func.noop |
hint | 水印 (Icon的type类型,和hasClear占用一个地方) | String | - |
innerBefore | 文字前附加内容 | ReactNode | - |
innerAfter | 文字后附加内容 | ReactNode | - |
addonBefore | 输入框前附加内容 | ReactNode | - |
addonAfter | 输入框后附加内容 | ReactNode | - |
addonTextBefore | 输入框前附加文字 | ReactNode | - |
addonTextAfter | 输入框后附加文字 | ReactNode | - |
autoComplete | (原生input支持) | String | 'off' |
autoFocus | 自动聚焦(原生input支持) | Boolean | - |
Input.TextArea
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
value | 当前值 | String/Number | - |
defaultValue | 初始化值 | String/Number | - |
onChange | 发生改变的时候触发的回调签名:Function(value: String, e: Event) => void参数:value: {String} 数据e: {Event} DOM事件对象 | Function | func.noop |
onKeyDown | 键盘按下的时候触发的回调签名:Function(e: Event, opts: Object) => void参数:e: {Event} DOM事件对象opts: {Object} 可扩展的附加信息: - opts.overMaxLength: {Boolean} 已超出最大长度 - opts.beTrimed: {Boolean} 输入的空格被清理 | Function | func.noop |
disabled | 禁用状态 | Boolean | false |
maxLength | 最大长度 | Number | null |
hasLimitHint | 是否展现最大长度样式 | Boolean | false |
cutString | 当设置了maxLength时,是否截断超出字符串 | Boolean | true |
readOnly | 只读 | Boolean | false |
trim | onChange返回会自动去除头尾空字符 | Boolean | false |
placeholder | 输入提示 | String | - |
onFocus | 获取焦点时候触发的回调签名:Function(e: Event) => void参数:e: {Event} DOM事件对象 | Function | func.noop |
onBlur | 失去焦点时候触发的回调签名:Function(e: Event) => void参数:e: {Event} DOM事件对象 | Function | func.noop |
getValueLength | 自定义字符串计算长度方式签名:Function(value: String) => Number参数:value: {String} 数据返回值:{Number} 自定义长度 | Function | func.noop |
htmlType | 原生type | String | - |
name | name | String | - |
state | 状态可选值:'error'(错误) | Enum | - |
hasBorder | 是否有边框 | Boolean | true |
autoHeight | 自动高度 true / {minRows: 2, maxRows: 4} | Boolean/Object | false |
rows | 多行文本框高度 (不要直接用height设置多行文本框的高度, ie9 10会有兼容性问题) | Number | 4 |
Input.Group
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
addonBefore | 输入框前附加内容 | ReactNode | - |
addonBeforeClassName | 输入框前附加内容css | String | - |
addonAfter | 输入框后附加内容 | ReactNode | - |
addonAfterClassName | 输入框后额外css | String | - |
rtl | rtl | Boolean | - |
Input/TextArea 内部函数(通过refs获取)
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
getInputNode | 获取真正input节点 | Function | |
focus | 获取焦点签名: Function(start:Number, end: Number)参数:start: {Number} 光标起始位置end: {Number} 选择结束位置 | Function |
ARIA and KeyBoard
按键 | 说明 |
---|---|
Enter | 触发onKeyDown事件 |
Any | 触发onChange事件 |
代码示例
查看源码在线预览
import { Input } from '@alifd/next';
function onChange(v) {
console.log(v);
}
ReactDOM.render(<div>
<Input size="large" placeholder="Large" onChange={onChange} aria-label="Large" /><br /><br />
<span id="J_InputMedium" style={{display: 'none'}}>Aria Labelby Demo </span>
<Input placeholder="Medium" aria-label="Medium" aria-labelledby="J_InputMedium" /><br /><br />
<Input placeholder="Small" size="small" label="SIZE :" id="J_InputSmall" /><br /><br />
<Input.TextArea placeholder="TextArea" aria-label="TextArea" />
</div>, mountNode);
设置 Input
为 密码类型;
查看源码在线预览
import { Input } from '@alifd/next';
ReactDOM.render(
<div>
<Input htmlType="password" defaultValue="whoami" aria-label="Please input password"/>
</div>
, mountNode);
查看源码在线预览
import { Input } from '@alifd/next';
ReactDOM.render(<div>
<Input hasLimitHint
addonTextBefore="http://"
addonTextAfter=".com"
size="large"
defaultValue="alibaba"
maxLength={2}
aria-label="input with config of addonTextBefore and addonTextAfter" /><br /><br />
<Input
addonTextBefore="http://"
addonTextAfter=".com"
size="medium"
value="alibaba"
aria-label="input with config of addonTextBefore and addonTextAfter" /><br /><br />
<Input
addonTextBefore="http://"
addonTextAfter=".com"
size="small"
value="alibaba"
aria-label="input with config of addonTextBefore and addonTextAfter" />
</div>
, mountNode);
通过设置 hasClear 添加清除按钮.
hint为水印按钮,和hasClear占用同一个地方配合使用
查看源码在线预览
import { Input } from '@alifd/next';
const onChange = (value) => {
console.log(value);
};
const onBlur = (e) => {
console.log(e);
};
ReactDOM.render(
<div>
<Input
hasClear
defaultValue="clear by click"
size="small"
aria-label="input with config of hasClear" onChange={onChange} /><br/><br/>
<Input
hasClear
defaultValue="2019-09-10 10:10:20"
aria-label="input with config of hasClear"
onChange={onChange}
onBlur={onBlur}
hint="calendar"/><br/><br/>
<Input
hasClear
defaultValue="clear by click"
size="large"
aria-label="input with config of hasClear"
onChange={onChange} /><br/><br/>
</div>
, mountNode);
为 Input
设置 error
状态;
查看源码在线预览
import { Input } from '@alifd/next';
ReactDOM.render(
<div>
<Input state="error"
placeholder="error"
size="small"
aria-live="assertive"
aria-label="error" /><br/><br/>
<Input state="error"
hasLimitHint
maxLength={100}
placeholder="error"
size="medium"
aria-live="assertive"
aria-label="error" /><br/><br/>
<Input state="error"
multiple
placeholder="error"
size="large"
aria-live="assertive"
aria-label="error" /><br/><br/>
<Input state="success"
size="small"
value="success"
aria-live="assertive"
aria-label="success"/><br/><br/>
<Input state="success"
maxLength={100}
hasLimitHint
placeholder="success"
size="medium"
aria-live="assertive"
aria-label="success"/><br/><br/>
<Input state="success"
placeholder="success"
size="large"
aria-live="assertive"
aria-label="success"/><br/><br/>
<Input state="loading"
placeholder="loading"
size="small"
aria-live="assertive"
aria-label="loading"/><br/><br/>
<Input state="loading"
placeholder="loading"
size="medium"
aria-live="assertive"
aria-label="loading"/><br/><br/>
<Input state="loading"
placeholder="loading"
size="large"
aria-live="assertive"
aria-label="loading"/><br/><br/>
</div>
, mountNode);
最大长度 hasLimitHint 会展现限制数字; cutString 可控制是否要切割字符串, 用于只展示最大长度
查看源码在线预览
import { Input } from '@alifd/next';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
maxLength: 10,
control: 'maxLen control'
};
}
onChange(v) {
console.log(v);
this.setState({
control: v
});
}
onKeyDown(e, opts) {
console.log('onKeyDown', opts);
}
render() {
return (<div>
<Input
maxLength={10}
size="large"
placeholder="Large"
value={this.state.control}
hasLimitHint
aria-label="input max length 10"
onChange={this.onChange.bind(this)}
onKeyDown={this.onKeyDown.bind(this)} /><br /><br />
<Input
maxLength={20}
placeholder="medium"
hasLimitHint
cutString={false}
aria-label="input max length 20"
onChange={(v) => {
this.setState({control: v});
console.log(v);
}}
onKeyDown={(e, opts) => {
console.log('onKeyDown', opts);
}} /><br /><br />
<Input
hasLimitHint
size="small"
placeholder="small"
maxLength={100}
aria-label="input max length 100" /><br /><br />
<Input.TextArea
placeholder="TextArea"
maxLength={100}
rows={4}
hasLimitHint
aria-label="input max length 100" /><br /><br />
<Input maxLength={5} placeholder="Original maxLength" aria-label="input max length 5" />
</div>);
}
}
ReactDOM.render(<App/>, mountNode);
onChange返回会自动去除头尾空字符
查看源码在线预览
import { Input } from '@alifd/next';
class App extends React.Component {
state = {
value: ''
}
onChange(value) {
console.log('onChange', value);
this.setState({
value
});
}
onKeyDown(e, opts) {
console.log('onKeyDown', opts);
}
render() {
return (<div>
<Input trim placeholder="cant not input space" aria-label="cant not input space"
onChange={this.onChange.bind(this)}
onKeyDown={this.onKeyDown.bind(this)} />
</div>);
}
}
ReactDOM.render(<App/>, mountNode);
为 Input
设置 disabled
状态;
查看源码在线预览
import { Input } from '@alifd/next';
ReactDOM.render(
<div>
<Input disabled aria-label="disabled" placeholder="disabled" size="small"/><br /><br />
<Input
disabled
aria-label="disabled"
addonTextBefore="http://"
addonTextAfter=".com"
size="medium"
value="alibaba"/><br /><br />
<Input disabled aria-label="disabled" placeholder="medium" maxLength={10} hasLimitHint/><br /><br />
<Input.TextArea disabled aria-label="disabled" placeholder="medium" maxLength={10} hasLimitHint/>
</div>
, mountNode);
可以添加水印, 为input前后端增加额外内容
查看源码在线预览
import { Input, Icon } from '@alifd/next';
class App extends React.Component {
state = {
v: ''
};
onChange = (v) => {
this.setState({
v
});
};
onClick = () => {
console.log(this.state.v);
};
render() {
return (<div>
<Input
innerBefore={<Icon type="search" style={{margin: 4}} onClick={this.onClick} />}
placeholder="search"
value={this.state.v}
aria-label="input with config of innerBefore"
onChange={this.onChange}
/><br /><br />
<Input
innerAfter={<Icon type="search" size="xs" onClick={this.onClick} style={{margin: 4}}/>}
placeholder="search"
value={this.state.v}
aria-label="input with config of innerAfter"
onChange={this.onChange}
/><br /><br />
<Input
disabled
defaultValue="hi"
innerAfter={<Icon type="calendar" style={{margin: 4}}/>}
aria-label="input with config of innerAfter and disabled" />
</div>);
}
}
ReactDOM.render(<App/>, mountNode);
设置 Input
为 多行文本域;
查看源码在线预览
import { Input } from '@alifd/next';
ReactDOM.render(
<div >
<Input.TextArea
autoHeight
trim
aria-label="auto height"
placeholder="autoHeight"
onKeyDown={(e, opts) => {
console.log('onKeyDown', opts);
}} /><br/><br/>
<Input.TextArea aria-label="auto height" autoHeight={{ minRows: 2, maxRows: 6 }} />
</div>
, mountNode);
Group 具有两边长度固定中间组件任意伸缩的能力。在Input中可以用addonBefore/addonAfter快速使用
查看源码在线预览
import { Input, Select, Button } from '@alifd/next';
const select = (<Select aria-label="please select" >
<option value="https">https</option>
<option value="http">http</option>
</Select>);
const button = (<Button>search</Button>);
ReactDOM.render(<div>
<Input.Group addonBefore={select} addonAfter={button}>
<Input hasClear defaultValue="abc" style={{width: '100%'}} aria-label="please input" />
</Input.Group>
<br/><br/>
<Input addonTextAfter=".com" addonBefore={select} aria-label="please input" />
</div>, mountNode);
通过style设置宽度
查看源码在线预览
import { Input } from '@alifd/next';
ReactDOM.render(
<div>
<Input placeholder="width:400" style={{width: 400}} aria-label="style width 400" /><br/><br/>
<Input
addonTextBefore="http://"
addonTextAfter=".com"
size="medium"
value="alibaba"
style={{width: 400}} aria-label="style width 400" /><br/><br/>
<Input
placeholder="medium"
maxLength={10}
hasLimitHint
style={{width: 400}} aria-label="style width 400" /><br/><br/>
<Input
placeholder="medium"
hasClear maxLength={10}
hasLimitHint style={{width: 400}}
className="my-input-class"
state="success"
aria-label="style width 400" /><br/><br/>
<Input placeholder="className" className="my-input-class" aria-label="custom my input class" />
<Input htmlType="hidden" aria-label="hidden input" />
</div>
, mountNode);
body .my-input-class {
width:500px;
}
通过aria-label
对Input
组件进行描述。关于键盘操作请参考ARIA and KeyBoard
。
查看源码在线预览
import { Input } from '@alifd/next';
function onChange(v) {
console.log(v);
}
function onKeyDown(v) {
console.log(v);
}
ReactDOM.render(<div>
<Input size="large" placeholder="please input" onChange={onChange} onKeyDown={onKeyDown} aria-label="this is input" />
</div>
, mountNode);