1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var React = require("react");
|
11 | var PropTypes = React.PropTypes;
|
12 |
|
13 | var TextInput = React.createClass({
|
14 |
|
15 | propTypes: {
|
16 | className: PropTypes.string,
|
17 | onBlur: PropTypes.func,
|
18 | onChange: PropTypes.func,
|
19 | onFocus: PropTypes.func,
|
20 | value: PropTypes.string,
|
21 | placeholder: PropTypes.string
|
22 | },
|
23 |
|
24 | getInitialState: function() {
|
25 | return {
|
26 | isFocused: false
|
27 | }
|
28 | },
|
29 |
|
30 | getDefaultProps: function() {
|
31 | return {
|
32 | type: 'text'
|
33 | };
|
34 | },
|
35 |
|
36 | render: function() {
|
37 | var labelClass = ( this.props.value ||
|
38 | this.state.isFocused ||
|
39 | this.state.hasValue) ? 'focus' : '';
|
40 | var label = this.props.placeholder ? (
|
41 | <label className={labelClass}>
|
42 | {this.props.placeholder}
|
43 | </label>
|
44 | ) : null;
|
45 |
|
46 | return (
|
47 | <div className={["cb-text-input", this.props.className].join(" ")}>
|
48 | {label}
|
49 | <input
|
50 | ref='input'
|
51 | type={this.props.type}
|
52 | onBlur={this._handleInputBlur}
|
53 | onChange={this._handleInput}
|
54 | onFocus={this._handleInputFocus}
|
55 | value={this.props.value}
|
56 | />
|
57 | </div>
|
58 | );
|
59 | },
|
60 |
|
61 | blur: function() {
|
62 | if (this.isMounted()) React.findDOMNode('input').blur();
|
63 | },
|
64 |
|
65 | focus: function() {
|
66 | if (this.isMounted()) React.findDOMNode('input').focus();
|
67 | },
|
68 |
|
69 | _handleInput: function(e) {
|
70 | var input = e.target.value;
|
71 | this.setState({ hasValue: input });
|
72 | this.props.onChange(input);
|
73 | },
|
74 |
|
75 | _handleInputBlur: function(e) {
|
76 | this.setState({ isFocused: false });
|
77 | if (this.props.onBlur) this.props.onFocus(e);
|
78 | },
|
79 |
|
80 | _handleInputFocus: function(e) {
|
81 | this.setState({ isFocused: true });
|
82 | if (this.props.onFocus) this.props.onFocus(e);
|
83 | }
|
84 |
|
85 | });
|
86 |
|
87 | module.exports = TextInput;
|