-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathFormInput.js
More file actions
80 lines (72 loc) · 1.7 KB
/
Copy pathFormInput.js
File metadata and controls
80 lines (72 loc) · 1.7 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
var React = require('react');
var classNames = require('classnames');
export default React.createClass({
displayName: 'FormInput',
propTypes: {
autoFocus: React.PropTypes.bool,
className: React.PropTypes.string,
disabled: React.PropTypes.bool,
href: React.PropTypes.string,
id: React.PropTypes.string,
innerRef: React.PropTypes.func,
multiline: React.PropTypes.bool,
name: React.PropTypes.string,
noedit: React.PropTypes.bool,
onChange: React.PropTypes.func,
size: React.PropTypes.oneOf(['lg', 'sm', 'xs']),
type: React.PropTypes.string,
value: React.PropTypes.oneOfType([
React.PropTypes.number,
React.PropTypes.string,
]),
},
getDefaultProps () {
return {
type: 'text',
};
},
componentDidMount () {
if (this.props.autoFocus) {
this.focus();
}
},
focus () {
this.input.focus();
},
select () {
this.input.select();
},
getRef (ref) {
this.input = ref;
if (this.props.innerRef) {
this.props.innerRef(ref);
}
},
render () {
const { noedit, multiline, size, className, ...rest } = this.props;
// classes
let newClassName = classNames(
{
'FormInput-noedit': noedit,
'FormInput-noedit--multiline': noedit && multiline,
'FormInput': !noedit,
},
(size ? ('FormInput--' + size) : null),
className
);
let props = { ...rest, className: newClassName };
let Element = 'input';
if (noedit && this.props.href) {
Element = 'a';
props.type = null;
props.children = props.children || props.value;
} else if (noedit) {
Element = 'div';
props.type = null;
props.children = props.children || props.value;
} else if (multiline) {
Element = 'textarea';
}
return <Element ref={this.getRef} {...props} />;
},
});