-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
Expand file tree
/
Copy pathinput.jsx
More file actions
122 lines (101 loc) · 3.24 KB
/
input.jsx
File metadata and controls
122 lines (101 loc) · 3.24 KB
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
/** @jsx React.DOM */
var React = require('react');
var Classable = require('./mixins/classable.js');
var Input = React.createClass({
propTypes: {
multiline: React.PropTypes.bool,
required: React.PropTypes.bool,
min: React.PropTypes.number,
max: React.PropTypes.number,
step: React.PropTypes.number,
inputStyle: React.PropTypes.string,
error: React.PropTypes.string,
description: React.PropTypes.string,
placeholder: React.PropTypes.string,
type: React.PropTypes.string,
name: React.PropTypes.string.isRequired,
onChange: React.PropTypes.func,
onKeyDown: React.PropTypes.func
},
mixins: [Classable],
getInitialState: function() {
return {
value: this.props.defaultValue,
rows: 1
};
},
getDefaultProps: function() {
return {
multiline: false,
required: true,
type: "text"
};
},
setError: function(error) {
this.setProps({ error: error });
},
removeError: function() {
this.setProps({error: undefined});
},
render: function() {
var classes = this.getClasses('mui-input', {
'mui-floating': this.props.inputStyle === 'floating',
'mui-text': this.props.type === 'text',
'mui-error': this.props.error !== undefined && this.props.error !== null
}),
inputElement = this.props.multiline ?
this.props.valueLink ?
<textarea {...this.props} className="mui-input-textarea" placeholder=""
rows={this.state.rows} /> :
<textarea {...this.props} value={this.state.value} className="mui-input-textarea"
placeholder="" rows={this.state.rows} onChange={this._onTextAreaChange} onKeyDown={this._onKeyDown} /> :
this.props.valueLink ?
<input {...this.props} ref="input" placeholder="" /> :
<input {...this.props} ref="input" value={this.state.value} placeholder=""
onChange={this._onInputChange} onKeyDown={this._onKeyDown} />;
return (
<div ref={this.props.ref} className={classes}>
{inputElement}
<span className="mui-input-placeholder"
onClick={this._onPlaceholderClick}>{this.props.placeholder}</span>
<span className="mui-input-highlight"></span>
<span className="mui-input-bar"></span>
<span className="mui-input-description">{this.props.description}</span>
<span className="mui-input-error">{this.props.error}</span>
</div>
);
},
getValue: function() {
return this.state.value;
},
setValue: function(txt) {
this.setState({value: txt});
},
clearValue: function() {
this.setValue("");
},
_onInputChange: function(e) {
var value = e.target.value;
this.setState({value: value});
if (this.props.onChange) this.props.onChange(e, value);
},
_onKeyDown: function(e) {
if (this.props.onKeyDown) this.props.onKeyDown(e);
},
_onPlaceholderClick: function(e) {
this.refs.input.getDOMNode().focus();
},
_onTextAreaChange: function(e) {
this._onInputChange(e);
this._onLineBreak(e);
},
_onLineBreak: function(e) {
var input = (e.target.value.slice(-1));
if(input.match(/\n/gm)) {
if(this.state.rows != 20) {
this.setState({ rows: ((this.state.rows) + 1)});
}
}
}
});
module.exports = Input;