-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInputView.jsx
79 lines (77 loc) · 2.01 KB
/
InputView.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React from 'react';
export default class InputView extends React.Component {
constructor(props) {
super(props);
this.state={
inputValue:""
}
this.inputRef = "";
this.isCompositions = true;
this.onChangeInput = this.onChangeInput.bind(this)
}
componentDidMount (){
this.inputRef.addEventListener('compositionstart', function(){//非直接的文字输入时(键盘输入中文的拼音)
this.isCompositions = false;
}.bind(this))
this.inputRef.addEventListener('compositionend', function(){//直接输入文字后(键盘选择真实的汉字)
this.isCompositions = true;
}.bind(this))
this.inputRef.addEventListener('input', function(){
setTimeout(function(){
if(this.isCompositions){
this.props.onChange({target:{value:this.inputRef.value}})
}
}.bind(this),100)
}.bind(this));
}
onChangeInput(e){
this.setState({
inputValue: e.target.value
});
}
componentWillReceiveProps (newProps){
if (newProps.value!=this.state.inputValue) {
this.setState({
inputValue: newProps.value
});
}
}
render() {
let type = "";
let value = "";
let maxLength = "";
let placeholder = "";
let className = "";
let style = {};
if (this.props.type) {
type = this.props.type
}
if (this.props.value) {
value = this.props.value
}
if (this.props.maxLength) {
maxLength = this.props.maxLength
}
if (this.props.placeholder) {
placeholder = this.props.placeholder
}
if (this.props.className) {
className = this.props.className
}
if (this.props.style) {
style = this.props.style
}
return(
<input
type={type}
maxLength={maxLength}
value={this.state.inputValue}
placeholder={placeholder}
onChange = {this.onChangeInput}
className={className}
style={style}
ref = {ref=>{this.inputRef=ref}}
/>
)
}
}