Input 输入框

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

安装方法

  1. 在命令行中执行以下命令npm install @icedesign/base@latest -S

开发指南

何时使用

表单输入,一般配合Form使用

API

输入框

参数说明类型默认值
prefix样式前缀String'next-'
value当前值String/Number-
defaultValue初始化值String/Number-
size尺寸可选值:'small'(小)'medium'(中)'large'(大)Enum'medium'
disabled状态 设置文本域禁用状态Booleanfalse
multiple是否为多行,不选则为单行Booleanfalse
maxLength最大长度Numbernull
hasLimitHint是否展现最大长度样式Booleanfalse
cutString是否允许切割字符串Booleantrue
hasClear是否出现clear按钮Booleanfalse
state状态(multiple模式不支持 loading/success 状态)可选值:'''error'(错误)'loading'(校验中)'success'(成功)Enum''
style自定义内联样式Object-
htmlType原生typeString'text'
readOnly只读Booleanfalse
trimonChange返回会自动去除头尾空字符Booleanfalse
addonBefore文本域前附加内容ReactNode-
addonAfter文本域后附加内容ReactNode-
placeholder输入提示String-
onPressEnter按下回车的回调签名:Function() => voidFunctionfunction() { }
onBlur失去焦点时候触发的回调签名:Function() => voidFunctionfunction() { }
onChange发生改变的时候触发的回调签名:Function(value: String, e: Event) => void参数:value: {String} 数据e: {Event} DOM事件对象Functionfunction() { }
getValueLength自定义字符串计算长度方式签名:Function(value: String) => Number参数:value: {String} 数据返回值:{Number} 自定义长度Functionfunction() { }
rowsmultiple多行文本框高度 (不要直接用height设置多行文本框的高度, ie9 10会有兼容性问题)Number4
textIndent文字缩进Number-
className自定义classString-

代码示例

Input 组合

各类 Input 的组合展现。

Input 输入框 - 图1

查看源码在线预览

  1. import { Input } from "@icedesign/base";
  2. ReactDOM.render(
  3. <div>
  4. <Input
  5. addonBefore="http://"
  6. addonAfter=".com"
  7. size="large"
  8. defaultValue="alibaba"
  9. maxLength={2}
  10. hasLimitHint
  11. />
  12. <br />
  13. <br />
  14. <Input
  15. addonBefore="http://"
  16. addonAfter=".com"
  17. size="medium"
  18. value="alibaba"
  19. />
  20. <br />
  21. <br />
  22. <Input
  23. addonBefore="http://"
  24. addonAfter=".com"
  25. size="small"
  26. value="alibaba"
  27. />
  28. </div>,
  29. mountNode
  30. );

Input 输入框

我们为 <Input /> 输入框定义了三种尺寸(大、默认、小)

Input 输入框 - 图2

查看源码在线预览

  1. import { Input } from "@icedesign/base";
  2. ReactDOM.render(
  3. <div>
  4. <Input size="large" placeholder="Large" />
  5. <br />
  6. <br />
  7. <Input className="textClsName" placeholder="Medium" />
  8. <br />
  9. <br />
  10. <Input placeholder="Small" size="small" />
  11. </div>,
  12. mountNode
  13. );

清除按钮

通过设置 hasClear 添加清除按钮

Input 输入框 - 图3

查看源码在线预览

  1. import { Input } from "@icedesign/base";
  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 defaultValue="一键清除" hasClear onChange={onChange} size="small" />
  11. <br />
  12. <br />
  13. <Input hasClear onChange={onChange} onBlur={onBlur} />
  14. <br />
  15. <br />
  16. <Input defaultValue="一键清除" hasClear onChange={onChange} size="large" />
  17. <br />
  18. <br />
  19. </div>,
  20. mountNode
  21. );

禁用状态

Input 设置 disabled 状态;

Input 输入框 - 图4

查看源码在线预览

  1. import { Input } from "@icedesign/base";
  2. ReactDOM.render(
  3. <div>
  4. <Input disabled placeholder="disabled" size="small" />
  5. <br />
  6. <br />
  7. <Input
  8. disabled
  9. addonBefore="http://"
  10. addonAfter=".com"
  11. size="medium"
  12. value="alibaba"
  13. />
  14. <br />
  15. <br />
  16. <Input disabled placeholder="medium" maxLength={10} hasLimitHint />
  17. <br />
  18. <br />
  19. <Input multiple disabled placeholder="medium" maxLength={10} hasLimitHint />
  20. </div>,
  21. mountNode
  22. );

最大长度

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

Input 输入框 - 图5

查看源码在线预览

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

多行文本域

设置 Input 为 多行文本域;

Input 输入框 - 图6

查看源码在线预览

  1. import { Input } from "@icedesign/base";
  2. ReactDOM.render(
  3. <div>
  4. <Input multiple placeholder="multiple" />
  5. </div>,
  6. mountNode
  7. );

密码输入框

设置 Input 为 密码类型;

Input 输入框 - 图7

查看源码在线预览

  1. import { Input } from "@icedesign/base";
  2. ReactDOM.render(
  3. <div>
  4. <Input htmlType="password" size="small" defaultValue="abcd" />
  5. </div>,
  6. mountNode
  7. );

错误状态

Input 设置 error 状态;

Input 输入框 - 图8

查看源码在线预览

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

自定义style,className

通过style设置宽度

Input 输入框 - 图9

查看源码在线预览

  1. import { Input } from "@icedesign/base";
  2. ReactDOM.render(
  3. <div>
  4. <Input placeholder="width:400" style={{ width: 400 }} />
  5. <br />
  6. <br />
  7. <Input
  8. addonBefore="http://"
  9. addonAfter=".com"
  10. size="medium"
  11. value="alibaba"
  12. style={{ width: 400 }}
  13. />
  14. <br />
  15. <br />
  16. <Input
  17. placeholder="medium"
  18. maxLength={10}
  19. hasLimitHint
  20. style={{ width: 400 }}
  21. />
  22. <br />
  23. <br />
  24. <Input
  25. placeholder="medium"
  26. maxLength={10}
  27. hasLimitHint
  28. style={{ width: 400 }}
  29. className="my-input-class"
  30. state="success"
  31. />
  32. <br />
  33. <br />
  34. <Input placeholder="className" className="my-input-class" />
  35. <Input htmlType="hidden" />
  36. </div>,
  37. mountNode
  38. );
  1. body .my-input-class {
  2. width:500px;
  3. }

trim

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

Input 输入框 - 图10

查看源码在线预览

  1. import { Input } from "@icedesign/base";
  2. class App extends React.Component {
  3. state = {
  4. value: ""
  5. };
  6. onChange(value) {
  7. console.log(value);
  8. this.setState({
  9. value
  10. });
  11. }
  12. render() {
  13. return (
  14. <div>
  15. <Input
  16. trim
  17. value={this.state.value}
  18. onChange={this.onChange.bind(this)}
  19. placeholder="无法输入空格哦!"
  20. />
  21. </div>
  22. );
  23. }
  24. }
  25. ReactDOM.render(<App />, mountNode);

相关区块

Input 输入框 - 图11

暂无相关区块