Input 输入框

如果项目中使用的是 0.x 版本的基础组件(@icedesign/base, @ali/ice, @alife/next),请在左侧导航顶部切换组件版本。

安装方法

  1. 在命令行中执行以下命令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事件对象Functionfunc.noop
onKeyDown键盘按下的时候触发的回调签名:Function(e: Event, opts: Object) => void参数:e: {Event} DOM事件对象opts: {Object} 可扩展的附加信息: - opts.overMaxLength: {Boolean} 已超出最大长度 - opts.beTrimed: {Boolean} 输入的空格被清理Functionfunc.noop
disabled禁用状态Booleanfalse
maxLength最大长度Numbernull
hasLimitHint是否展现最大长度样式Booleanfalse
cutString当设置了maxLength时,是否截断超出字符串Booleantrue
readOnly只读Booleanfalse
trimonChange返回会自动去除头尾空字符Booleanfalse
placeholder输入提示String-
onFocus获取焦点时候触发的回调签名:Function(e: Event) => void参数:e: {Event} DOM事件对象Functionfunc.noop
onBlur失去焦点时候触发的回调签名:Function(e: Event) => void参数:e: {Event} DOM事件对象Functionfunc.noop
getValueLength自定义字符串计算长度方式签名:Function(value: String) => Number参数:value: {String} 数据返回值:{Number} 自定义长度Functionfunc.noop
htmlType原生typeString-
namenameString-
state状态可选值:'error'(错误)'loading'(校验中)'success'(成功)Enum-
labellabelReactNode-
hasClear是否出现clear按钮Boolean-
hasBorder是否有边框Booleantrue
onPressEnter按下回车的回调签名:Function() => voidFunctionfunc.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事件对象Functionfunc.noop
onKeyDown键盘按下的时候触发的回调签名:Function(e: Event, opts: Object) => void参数:e: {Event} DOM事件对象opts: {Object} 可扩展的附加信息: - opts.overMaxLength: {Boolean} 已超出最大长度 - opts.beTrimed: {Boolean} 输入的空格被清理Functionfunc.noop
disabled禁用状态Booleanfalse
maxLength最大长度Numbernull
hasLimitHint是否展现最大长度样式Booleanfalse
cutString当设置了maxLength时,是否截断超出字符串Booleantrue
readOnly只读Booleanfalse
trimonChange返回会自动去除头尾空字符Booleanfalse
placeholder输入提示String-
onFocus获取焦点时候触发的回调签名:Function(e: Event) => void参数:e: {Event} DOM事件对象Functionfunc.noop
onBlur失去焦点时候触发的回调签名:Function(e: Event) => void参数:e: {Event} DOM事件对象Functionfunc.noop
getValueLength自定义字符串计算长度方式签名:Function(value: String) => Number参数:value: {String} 数据返回值:{Number} 自定义长度Functionfunc.noop
htmlType原生typeString-
namenameString-
state状态可选值:'error'(错误)Enum-
hasBorder是否有边框Booleantrue
autoHeight自动高度 true / {minRows: 2, maxRows: 4}Boolean/Objectfalse
rows多行文本框高度 (不要直接用height设置多行文本框的高度, ie9 10会有兼容性问题)Number4

Input.Group

参数说明类型默认值
addonBefore输入框前附加内容ReactNode-
addonBeforeClassName输入框前附加内容cssString-
addonAfter输入框后附加内容ReactNode-
addonAfterClassName输入框后额外cssString-
rtlrtlBoolean-

Input/TextArea 内部函数(通过refs获取)

参数说明类型默认值
getInputNode获取真正input节点Function
focus获取焦点签名: Function(start:Number, end: Number)参数:start: {Number} 光标起始位置end: {Number} 选择结束位置Function

ARIA and KeyBoard

按键说明
Enter触发onKeyDown事件
Any触发onChange事件

代码示例

简单

Input 输入框 - 图1

查看源码在线预览

  1. import { Input } from '@alifd/next';
  2. function onChange(v) {
  3. console.log(v);
  4. }
  5. ReactDOM.render(<div>
  6. <Input size="large" placeholder="Large" onChange={onChange} aria-label="Large" /><br /><br />
  7. <span id="J_InputMedium" style={{display: 'none'}}>Aria Labelby Demo </span>
  8. <Input placeholder="Medium" aria-label="Medium" aria-labelledby="J_InputMedium" /><br /><br />
  9. <Input placeholder="Small" size="small" label="SIZE :" id="J_InputSmall" /><br /><br />
  10. <Input.TextArea placeholder="TextArea" aria-label="TextArea" />
  11. </div>, mountNode);

密码输入框

设置 Input 为 密码类型;

Input 输入框 - 图2

查看源码在线预览

  1. import { Input } from '@alifd/next';
  2. ReactDOM.render(
  3. <div>
  4. <Input htmlType="password" defaultValue="whoami" aria-label="Please input password"/>
  5. </div>
  6. , mountNode);

前后扩展

Input 输入框 - 图3

查看源码在线预览

  1. import { Input } from '@alifd/next';
  2. ReactDOM.render(<div>
  3. <Input hasLimitHint
  4. addonTextBefore="http://"
  5. addonTextAfter=".com"
  6. size="large"
  7. defaultValue="alibaba"
  8. maxLength={2}
  9. aria-label="input with config of addonTextBefore and addonTextAfter" /><br /><br />
  10. <Input
  11. addonTextBefore="http://"
  12. addonTextAfter=".com"
  13. size="medium"
  14. value="alibaba"
  15. aria-label="input with config of addonTextBefore and addonTextAfter" /><br /><br />
  16. <Input
  17. addonTextBefore="http://"
  18. addonTextAfter=".com"
  19. size="small"
  20. value="alibaba"
  21. aria-label="input with config of addonTextBefore and addonTextAfter" />
  22. </div>
  23. , mountNode);

清除按钮

通过设置 hasClear 添加清除按钮.

hint为水印按钮,和hasClear占用同一个地方配合使用

Input 输入框 - 图4

查看源码在线预览

  1. import { Input } from '@alifd/next';
  2. const onChange = (value) => {
  3. console.log(value);
  4. };
  5. const onBlur = (e) => {
  6. console.log(e);
  7. };
  8. ReactDOM.render(
  9. <div>
  10. <Input
  11. hasClear
  12. defaultValue="clear by click"
  13. size="small"
  14. aria-label="input with config of hasClear" onChange={onChange} /><br/><br/>
  15. <Input
  16. hasClear
  17. defaultValue="2019-09-10 10:10:20"
  18. aria-label="input with config of hasClear"
  19. onChange={onChange}
  20. onBlur={onBlur}
  21. hint="calendar"/><br/><br/>
  22. <Input
  23. hasClear
  24. defaultValue="clear by click"
  25. size="large"
  26. aria-label="input with config of hasClear"
  27. onChange={onChange} /><br/><br/>
  28. </div>
  29. , mountNode);

错误状态

Input 设置 error 状态;

Input 输入框 - 图5

查看源码在线预览

  1. import { Input } from '@alifd/next';
  2. ReactDOM.render(
  3. <div>
  4. <Input state="error"
  5. placeholder="error"
  6. size="small"
  7. aria-live="assertive"
  8. aria-label="error" /><br/><br/>
  9. <Input state="error"
  10. hasLimitHint
  11. maxLength={100}
  12. placeholder="error"
  13. size="medium"
  14. aria-live="assertive"
  15. aria-label="error" /><br/><br/>
  16. <Input state="error"
  17. multiple
  18. placeholder="error"
  19. size="large"
  20. aria-live="assertive"
  21. aria-label="error" /><br/><br/>
  22. <Input state="success"
  23. size="small"
  24. value="success"
  25. aria-live="assertive"
  26. aria-label="success"/><br/><br/>
  27. <Input state="success"
  28. maxLength={100}
  29. hasLimitHint
  30. placeholder="success"
  31. size="medium"
  32. aria-live="assertive"
  33. aria-label="success"/><br/><br/>
  34. <Input state="success"
  35. placeholder="success"
  36. size="large"
  37. aria-live="assertive"
  38. aria-label="success"/><br/><br/>
  39. <Input state="loading"
  40. placeholder="loading"
  41. size="small"
  42. aria-live="assertive"
  43. aria-label="loading"/><br/><br/>
  44. <Input state="loading"
  45. placeholder="loading"
  46. size="medium"
  47. aria-live="assertive"
  48. aria-label="loading"/><br/><br/>
  49. <Input state="loading"
  50. placeholder="loading"
  51. size="large"
  52. aria-live="assertive"
  53. aria-label="loading"/><br/><br/>
  54. </div>
  55. , mountNode);

最大长度

最大长度 hasLimitHint 会展现限制数字; cutString 可控制是否要切割字符串, 用于只展示最大长度

Input 输入框 - 图6

查看源码在线预览

  1. import { Input } from '@alifd/next';
  2. class App extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = {
  6. maxLength: 10,
  7. control: 'maxLen control'
  8. };
  9. }
  10. onChange(v) {
  11. console.log(v);
  12. this.setState({
  13. control: v
  14. });
  15. }
  16. onKeyDown(e, opts) {
  17. console.log('onKeyDown', opts);
  18. }
  19. render() {
  20. return (<div>
  21. <Input
  22. maxLength={10}
  23. size="large"
  24. placeholder="Large"
  25. value={this.state.control}
  26. hasLimitHint
  27. aria-label="input max length 10"
  28. onChange={this.onChange.bind(this)}
  29. onKeyDown={this.onKeyDown.bind(this)} /><br /><br />
  30. <Input
  31. maxLength={20}
  32. placeholder="medium"
  33. hasLimitHint
  34. cutString={false}
  35. aria-label="input max length 20"
  36. onChange={(v) => {
  37. this.setState({control: v});
  38. console.log(v);
  39. }}
  40. onKeyDown={(e, opts) => {
  41. console.log('onKeyDown', opts);
  42. }} /><br /><br />
  43. <Input
  44. hasLimitHint
  45. size="small"
  46. placeholder="small"
  47. maxLength={100}
  48. aria-label="input max length 100" /><br /><br />
  49. <Input.TextArea
  50. placeholder="TextArea"
  51. maxLength={100}
  52. rows={4}
  53. hasLimitHint
  54. aria-label="input max length 100" /><br /><br />
  55. <Input maxLength={5} placeholder="Original maxLength" aria-label="input max length 5" />
  56. </div>);
  57. }
  58. }
  59. ReactDOM.render(<App/>, mountNode);

去除空格

onChange返回会自动去除头尾空字符

Input 输入框 - 图7

查看源码在线预览

  1. import { Input } from '@alifd/next';
  2. class App extends React.Component {
  3. state = {
  4. value: ''
  5. }
  6. onChange(value) {
  7. console.log('onChange', value);
  8. this.setState({
  9. value
  10. });
  11. }
  12. onKeyDown(e, opts) {
  13. console.log('onKeyDown', opts);
  14. }
  15. render() {
  16. return (<div>
  17. <Input trim placeholder="cant not input space" aria-label="cant not input space"
  18. onChange={this.onChange.bind(this)}
  19. onKeyDown={this.onKeyDown.bind(this)} />
  20. </div>);
  21. }
  22. }
  23. ReactDOM.render(<App/>, mountNode);

禁用状态

Input 设置 disabled 状态;

Input 输入框 - 图8

查看源码在线预览

  1. import { Input } from '@alifd/next';
  2. ReactDOM.render(
  3. <div>
  4. <Input disabled aria-label="disabled" placeholder="disabled" size="small"/><br /><br />
  5. <Input
  6. disabled
  7. aria-label="disabled"
  8. addonTextBefore="http://"
  9. addonTextAfter=".com"
  10. size="medium"
  11. value="alibaba"/><br /><br />
  12. <Input disabled aria-label="disabled" placeholder="medium" maxLength={10} hasLimitHint/><br /><br />
  13. <Input.TextArea disabled aria-label="disabled" placeholder="medium" maxLength={10} hasLimitHint/>
  14. </div>
  15. , mountNode);

水印和前后缀

可以添加水印, 为input前后端增加额外内容

Input 输入框 - 图9

查看源码在线预览

  1. import { Input, Icon } from '@alifd/next';
  2. class App extends React.Component {
  3. state = {
  4. v: ''
  5. };
  6. onChange = (v) => {
  7. this.setState({
  8. v
  9. });
  10. };
  11. onClick = () => {
  12. console.log(this.state.v);
  13. };
  14. render() {
  15. return (<div>
  16. <Input
  17. innerBefore={<Icon type="search" style={{margin: 4}} onClick={this.onClick} />}
  18. placeholder="search"
  19. value={this.state.v}
  20. aria-label="input with config of innerBefore"
  21. onChange={this.onChange}
  22. /><br /><br />
  23. <Input
  24. innerAfter={<Icon type="search" size="xs" onClick={this.onClick} style={{margin: 4}}/>}
  25. placeholder="search"
  26. value={this.state.v}
  27. aria-label="input with config of innerAfter"
  28. onChange={this.onChange}
  29. /><br /><br />
  30. <Input
  31. disabled
  32. defaultValue="hi"
  33. innerAfter={<Icon type="calendar" style={{margin: 4}}/>}
  34. aria-label="input with config of innerAfter and disabled" />
  35. </div>);
  36. }
  37. }
  38. ReactDOM.render(<App/>, mountNode);

自动高度

设置 Input 为 多行文本域;

Input 输入框 - 图10

查看源码在线预览

  1. import { Input } from '@alifd/next';
  2. ReactDOM.render(
  3. <div >
  4. <Input.TextArea
  5. autoHeight
  6. trim
  7. aria-label="auto height"
  8. placeholder="autoHeight"
  9. onKeyDown={(e, opts) => {
  10. console.log('onKeyDown', opts);
  11. }} /><br/><br/>
  12. <Input.TextArea aria-label="auto height" autoHeight={{ minRows: 2, maxRows: 6 }} />
  13. </div>
  14. , mountNode);

输入框组合

Group 具有两边长度固定中间组件任意伸缩的能力。在Input中可以用addonBefore/addonAfter快速使用

Input 输入框 - 图11

查看源码在线预览

  1. import { Input, Select, Button } from '@alifd/next';
  2. const select = (<Select aria-label="please select" >
  3. <option value="https">https</option>
  4. <option value="http">http</option>
  5. </Select>);
  6. const button = (<Button>search</Button>);
  7. ReactDOM.render(<div>
  8. <Input.Group addonBefore={select} addonAfter={button}>
  9. <Input hasClear defaultValue="abc" style={{width: '100%'}} aria-label="please input" />
  10. </Input.Group>
  11. <br/><br/>
  12. <Input addonTextAfter=".com" addonBefore={select} aria-label="please input" />
  13. </div>, mountNode);

自定义样式

通过style设置宽度

Input 输入框 - 图12

查看源码在线预览

  1. import { Input } from '@alifd/next';
  2. ReactDOM.render(
  3. <div>
  4. <Input placeholder="width:400" style={{width: 400}} aria-label="style width 400" /><br/><br/>
  5. <Input
  6. addonTextBefore="http://"
  7. addonTextAfter=".com"
  8. size="medium"
  9. value="alibaba"
  10. style={{width: 400}} aria-label="style width 400" /><br/><br/>
  11. <Input
  12. placeholder="medium"
  13. maxLength={10}
  14. hasLimitHint
  15. style={{width: 400}} aria-label="style width 400" /><br/><br/>
  16. <Input
  17. placeholder="medium"
  18. hasClear maxLength={10}
  19. hasLimitHint style={{width: 400}}
  20. className="my-input-class"
  21. state="success"
  22. aria-label="style width 400" /><br/><br/>
  23. <Input placeholder="className" className="my-input-class" aria-label="custom my input class" />
  24. <Input htmlType="hidden" aria-label="hidden input" />
  25. </div>
  26. , mountNode);
  1. body .my-input-class {
  2. width:500px;
  3. }

无障碍

通过aria-labelInput组件进行描述。关于键盘操作请参考ARIA and KeyBoard

Input 输入框 - 图13

查看源码在线预览

  1. import { Input } from '@alifd/next';
  2. function onChange(v) {
  3. console.log(v);
  4. }
  5. function onKeyDown(v) {
  6. console.log(v);
  7. }
  8. ReactDOM.render(<div>
  9. <Input size="large" placeholder="please input" onChange={onChange} onKeyDown={onKeyDown} aria-label="this is input" />
  10. </div>
  11. , mountNode);

相关区块

Input 输入框 - 图14

暂无相关区块