Input文本框 - 图1

Input 文本框

基本用法

  1. import { Input, Cell } from 'zarm';
  2. class Demo extends React.Component {
  3. state = {
  4. inputValue: '',
  5. };
  6. handleInputChange = (value) => {
  7. this.setState({
  8. inputValue: value,
  9. });
  10. }
  11. render() {
  12. return (
  13. <>
  14. <Cell title="单行文本">
  15. <Input
  16. clearable
  17. type="text"
  18. placeholder="请输入"
  19. value={this.state.inputValue}
  20. onChange={this.handleInputChange}
  21. />
  22. </Cell>
  23. <Cell title="多行文本"><Input type="text" rows={3} placeholder="请输入" /></Cell>
  24. </>
  25. )
  26. }
  27. }
  28. ReactDOM.render(<Demo />, mountNode);

输入类型

  1. import { Input, Cell, Button } from 'zarm';
  2. class Demo extends React.Component {
  3. state = {
  4. focused: false,
  5. number: '',
  6. };
  7. render() {
  8. return (
  9. <>
  10. <Cell title="数字">
  11. <Input
  12. ref={(ref) => { this.manualFocus = ref; }}
  13. type="number"
  14. placeholder="type is number"
  15. value={this.state.number}
  16. focused={this.state.focused}
  17. onFocus={value => console.log(`onFocus: ${value}`)}
  18. onBlur={value => console.log(`onBlur: ${value}`)}
  19. onChange={value => console.log(`onChange: ${value}`)}
  20. />
  21. </Cell>
  22. <Cell title="金额">
  23. <Input type="price" placeholder="type is price" />
  24. </Cell>
  25. <Cell title="身份证">
  26. <Input type="idcard" placeholder="type is idcard" />
  27. </Cell>
  28. <Cell>
  29. <Button size="xs" theme="primary" onClick={() => this.manualFocus.focus()}>click to focus the first input</Button>
  30. </Cell>
  31. </>
  32. )
  33. }
  34. }
  35. ReactDOM.render(<Demo />, mountNode);

只读 / 禁用状态

  1. import { Input, Cell } from 'zarm';
  2. ReactDOM.render(
  3. <>
  4. <Cell title="单行文本">
  5. <Input readOnly type="text" defaultValue="我是只读状态" />
  6. </Cell>
  7. <Cell title="单行文本">
  8. <Input disabled type="text" value="我是禁用状态" />
  9. </Cell>
  10. <Cell title="多行文本">
  11. <Input readOnly type="text" rows={3} value="我是只读状态,我是只读状态,我是只读状态,我是只读状态。" />
  12. </Cell>
  13. <Cell title="多行文本">
  14. <Input disabled type="text" rows={3} value="我是禁用状态,我是禁用状态,我是禁用状态,我是禁用状态。" />
  15. </Cell>
  16. </>
  17. , mountNode);

高度自适应

  1. import { Input, Cell } from 'zarm';
  2. ReactDOM.render(
  3. <Cell title="多行文本">
  4. <Input autoHeight type="text" rows={3} placeholder="写点啥..." />
  5. </Cell>
  6. , mountNode);

无标签栏

  1. import { Input, Cell } from 'zarm';
  2. ReactDOM.render(
  3. <>
  4. <Cell><Input type="text" placeholder="标题" /></Cell>
  5. <Cell><Input autoHeight type="text" rows={4} maxLength={200} placeholder="摘要" /></Cell>
  6. </>
  7. , mountNode);

显示输入字数

  1. import { Input, Cell } from 'zarm';
  2. ReactDOM.render(
  3. <Cell>
  4. <Input
  5. autoHeight
  6. showLength
  7. type="text"
  8. rows={4}
  9. maxLength={200}
  10. placeholder="摘要"
  11. />
  12. </Cell>
  13. , mountNode);

API

属性类型默认值说明
typestring'text'类型,可选值 textnumberidcardpricepasswordsearch
valuenumber | string-
defaultValuenumber | string-初始值
disabledbooleanfalse是否禁用
readOnlybooleanfalse是否只读
rowsnumber-多行文本时的显示行数。type为text类型时有效。
autoHeightbooleanfalse是否高度自适应
maxLengthnumber-输入字数上限
showLengthbooleanfalse是否显示输入字数。多行文本(type="text"且包含rows属性)时有效。
clearablebooleantrue是否显示清除按钮。多行文本(type="text"且包含rows属性)时无效。必须为受控组件(属性包含value、onChange)时方可生效。
onChange(value?: number | string) => void-值变化时触发的回调函数