forked from WebbyLab/react-components
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInput.jsx
126 lines (106 loc) · 3.86 KB
/
Input.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
var React = require('react/addons');
var cx = React.addons.classSet;
require('./Input.less');
var Input = React.createClass({
propTypes: {
placeholder: React.PropTypes.string,
type: React.PropTypes.string,
label: React.PropTypes.string,
error: React.PropTypes.string,
warning: React.PropTypes.string,
disabled: React.PropTypes.bool,
onChange: React.PropTypes.func
},
getInitialState() {
return {
value: this.props.value ? this.props.value : '',
error: this.props.error ? this.props.error : '',
warning: this.props.warning ? this.props.warning : '',
};
},
componentWillReceiveProps(nextProps) {
this.setState({
value: nextProps.value,
warning: nextProps.warning ? nextProps.warning : '',
error: nextProps.error ? nextProps.error : '',
});
},
handleChange(event) {
var value = event.target.value;
this.setState({
value: value,
});
if (this.props.onChange) {
if (this.props.dataMap) {
this.props.onChange({
newValue: value,
path: this.props.dataMap,
valueToSend:value
});
} else this.props.onChange({newValue:value});
}
},
handleBlur(event){
if (this.props.onBlur) {
this.props.onBlur(event);
}
},
handleFocus(){
if (this.props.onFocus) {
this.props.onFocus(event);
}
},
onKeyDown(event){
if (this.props.onKeyDown) {
this.props.onKeyDown(event);
}
},
handleTouchStart(event) {
if (this.props.onTouchStart) {
this.props.onTouchStart(event);
}
},
render() {
var warningNode = this.state.warning
? (<div className="animated fadeInUp">
<label className="WLC-warning-text WLC-warning">{this.state.warning}</label>
<div className="WLC-appendix WLC-warning"></div>
</div>)
: '';
var errorNode = this.state.error
? (<div className="animated fadeInUp">
<label className="WLC-warning-text WLC-error">{this.state.error}</label>
<div className="WLC-appendix WLC-error"></div>
</div>)
: '';
var inputClasses = cx({
'WLC-warning': this.state.warning,
'WLC-error': this.state.error,
});
var inputNode = <input ref = "input"
style = {this.props.style}
className = {inputClasses}
type = {this.props.type}
placeholder = {this.props.placeholder}
value = {this.state.value.toString()}
data-name = {this.props.name}
onFocus = {this.handleFocus}
onKeyDown = {this.onKeyDown}
autoCapitalize = "off"
autoCorrect = "off"
onTouchStart = {this.handleTouchStart}
onBlur = {this.handleBlur}
onChange = {this.handleChange}
data-map = {this.props.dataMap}
disabled = {this.props.disabled} />;
return (
<div className="WLC-Input">
{inputNode}
{warningNode}
{errorNode}
</div>
);
}
});
module.exports = Input;