forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput.jsx
107 lines (91 loc) · 2.1 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
/** @jsx React.DOM */
import React from './react-es6';
import classSet from './react-es6/lib/cx';
var INPUT_TYPES = [
'text',
'password',
'datetime',
'datetime-local',
'date',
'month',
'time',
'week',
'number',
'email',
'url',
'search',
'tel',
'color'
];
var Input = React.createClass({
propTypes: {
name: React.PropTypes.string.isRequired,
type: React.PropTypes.oneOf(INPUT_TYPES).isRequired,
id: React.PropTypes.string,
className: React.PropTypes.string,
placeholder: React.PropTypes.string,
label: React.PropTypes.string,
required: React.PropTypes.bool,
oneOf: React.PropTypes.array
//minLength: React.PropTypes.int
},
getInitialState: function () {
return {
error: false
};
},
getValue: function () {
return this.refs.input.getDOMNode().value;
},
renderInput: function () {
var classes = {
'form-control': true,
'input-md': true
};
return (
<input
id={this.props.id}
type={this.props.type}
className={classSet(classes)}
placeholder={this.props.placeholder}
ref="input"
/>
);
},
renderLabel: function () {
return this.props.label ? <label htmlFor={this.props.id}>{this.props.label}</label> : null;
},
render: function () {
var classes = {
'form-group': true,
'has-error': !!this.state.error
};
return (
<div className={classSet(classes)} onBlur={this.handleBlur} onFocus={this.handleFocus}>
{this.renderInput()}
{this.renderLabel()}
</div>
);
},
handleBlur: function (e) {
var value = this.getValue();
var error;
if (this.props.required && !value) {
error = 'required';
} else if (this.props.oneOf && !(value in this.props.oneOf)) {
error = 'oneOf';
} else if (this.props.minLength && value.length < this.props.minLength) {
error = 'minLength';
}
this.setState({
error: error
});
},
handleFocus: function(e) {
this.setState({
error: false
});
e.stopPropagation();
}
});
export default = Input;