forked from reactstrap/reactstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.js
More file actions
154 lines (135 loc) · 3.48 KB
/
Input.js
File metadata and controls
154 lines (135 loc) · 3.48 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* eslint react/prefer-stateless-function: 0 */
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { mapToCssModules, deprecated, warnOnce, tagPropType } from './utils';
const propTypes = {
children: PropTypes.node,
type: PropTypes.string,
size: PropTypes.string,
bsSize: PropTypes.string,
state: deprecated(
PropTypes.string,
'Please use the props "valid" and "invalid" to indicate the state.'
),
valid: PropTypes.bool,
invalid: PropTypes.bool,
tag: tagPropType,
innerRef: PropTypes.oneOfType([
PropTypes.object,
PropTypes.func,
PropTypes.string
]),
static: deprecated(PropTypes.bool, 'Please use the prop "plaintext"'),
plaintext: PropTypes.bool,
addon: PropTypes.bool,
className: PropTypes.string,
cssModule: PropTypes.object
};
const defaultProps = {
type: 'text'
};
class Input extends React.Component {
constructor(props) {
super(props);
this.getRef = this.getRef.bind(this);
this.focus = this.focus.bind(this);
}
getRef(ref) {
if (this.props.innerRef) {
this.props.innerRef(ref);
}
this.ref = ref;
}
focus() {
if (this.ref) {
this.ref.focus();
}
}
render() {
let {
className,
cssModule,
type,
bsSize,
state,
valid,
invalid,
tag,
addon,
static: staticInput,
plaintext,
innerRef,
...attributes
} = this.props;
const checkInput = ['radio', 'checkbox'].indexOf(type) > -1;
const isNotaNumber = new RegExp('\\D', 'g');
const fileInput = type === 'file';
const textareaInput = type === 'textarea';
const selectInput = type === 'select';
let Tag = tag || (selectInput || textareaInput ? type : 'input');
let formControlClass = 'form-control';
if (plaintext || staticInput) {
formControlClass = `${formControlClass}-plaintext`;
Tag = tag || 'input';
} else if (fileInput) {
formControlClass = `${formControlClass}-file`;
} else if (checkInput) {
if (addon) {
formControlClass = null;
} else {
formControlClass = 'form-check-input';
}
}
if (
state &&
typeof valid === 'undefined' &&
typeof invalid === 'undefined'
) {
if (state === 'danger') {
invalid = true;
} else if (state === 'success') {
valid = true;
}
}
if (attributes.size && isNotaNumber.test(attributes.size)) {
warnOnce(
'Please use the prop "bsSize" instead of the "size" to bootstrap\'s input sizing.'
);
bsSize = attributes.size;
delete attributes.size;
}
const classes = mapToCssModules(
classNames(
className,
invalid && 'is-invalid',
valid && 'is-valid',
bsSize ? `form-control-${bsSize}` : false,
formControlClass
),
cssModule
);
if (Tag === 'input' || (tag && typeof tag === 'function')) {
attributes.type = type;
}
if (
attributes.children &&
!(
plaintext ||
staticInput ||
type === 'select' ||
typeof Tag !== 'string' ||
Tag === 'select'
)
) {
warnOnce(
`Input with a type of "${type}" cannot have children. Please use "value"/"defaultValue" instead.`
);
delete attributes.children;
}
return <Tag {...attributes} ref={innerRef} className={classes} />;
}
}
Input.propTypes = propTypes;
Input.defaultProps = defaultProps;
export default Input;