npm.io
1.1.0 • Published 9 years ago

zent-input

Licence
MIT
Version
1.1.0
Deps
1
Vulns
0
Weekly
0
Stars
2.2K

Input 输入框

表单的输入组件,对原生input的包装,通过鼠标或键盘输入内容。

使用指南

  • 在表单输入时使用,可带前缀或后缀。
  • 可与其它组件组合使用,如组合成一个搜索输入框
代码演示

:::demo 基础用法

import { Input } from 'zent';

ReactDOM.render(
    <div>
        <Input placeholder="Please input your name" autoFocus />
        <Input type="password" placeholder="Please input your password" />
    </div>
    , mountNode
);

:::

:::demo 带前后缀的输入框

import { Input } from 'zent';

ReactDOM.render(
    <div>
        <Input addonBefore="$" />
        <Input addonAfter="%" />
        <Input addonBefore="Buy" addonAfter="Apple" />
    </div>
    , mountNode
);

:::

:::demo textarea输入框

import { Input } from 'zent';

ReactDOM.render(
    <div>
        <Input type="textarea" />
    </div>
    , mountNode
);

:::

:::demo 事件处理

import { Input } from 'zent';

class EventTest extends React.Component {
    constructor() {
        super();
        this.state = {
            logs: []
        }
    }
    onPressEnter = (e) => {
        this.addLog('enter pressed');
    }

    onKeyDown = (e) => {
        this.addLog('key down');
    }

    addLog(msg) {
        const { logs } = this.state;
        logs.push(msg)
        this.setState({logs})
    }

    render() {
        return (
            <div>
                <Input onPressEnter={this.onPressEnter} placeholder="press enter"/>
                <Input onKeyDown={this.onKeyDown} placeholder="key down"/>
                <div>{this.state.logs.map((log, index) => <p key={index}>{log}</p>)}</div>
            </div>
        );
    }
}

ReactDOM.render(
    <EventTest />
    , mountNode
);

:::

API
参数 说明 类型 默认值 备选值 是否必填
className 自定义额外类名 string ''
prefix 自定义类前缀 string 'zent'
type 自定义类前缀 string 'text' 'number''password''textarea'
defaultValue 默认值 string
value 输入值 string
readOnly 是否只读 bool false
disabled 是否禁用 bool false
placeholder 原生placeholder文案 string ''
addonBefore 前置标签 node
addonAfter 后置标签 node
autoFocus 自动focus bool
onChange change事件 func(e:Event)
onPressEnter 回车事件 func(e:Event)

除了以上属性外,所有react支持的input属性,Input组件都支持

focus

focus(): function

手动聚焦到输入框