forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormControl.js
More file actions
96 lines (82 loc) · 2.41 KB
/
Copy pathFormControl.js
File metadata and controls
96 lines (82 loc) · 2.41 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
import classNames from 'classnames';
import React from 'react';
import elementType from 'react-prop-types/lib/elementType';
import warning from 'warning';
import FormControlFeedback from './FormControlFeedback';
import FormControlStatic from './FormControlStatic';
import { bsClass, getClassSet, splitBsProps, bsSizes } from './utils/bootstrapUtils';
import { SIZE_MAP, Size } from './utils/StyleConfig';
import { prefix } from './utils/bootstrapUtils';
const propTypes = {
componentClass: elementType,
/**
* Only relevant if `componentClass` is `'input'`.
*/
type: React.PropTypes.string,
/**
* Uses `controlId` from `<FormGroup>` if not explicitly specified.
*/
id: React.PropTypes.string,
/**
* Attaches a ref to the `<input>` element. Only functions can be used here.
*
* ```js
* <FormControl inputRef={ref => { this.input = ref; }} />
* ```
*/
inputRef: React.PropTypes.func,
};
const defaultProps = {
componentClass: 'input',
};
const contextTypes = {
$bs_formGroup: React.PropTypes.object,
};
class FormControl extends React.Component {
render() {
const formGroup = this.context.$bs_formGroup;
const controlId = formGroup && formGroup.controlId;
const {
componentClass: Component,
type,
id = controlId,
inputRef,
className,
bsSize,
...props
} = this.props;
const [bsProps, elementProps] = splitBsProps(props);
warning(
controlId == null || id === controlId,
'`controlId` is ignored on `<FormControl>` when `id` is specified.'
);
// input[type="file"] should not have .form-control.
let classes;
if (type !== 'file') {
classes = getClassSet(bsProps);
}
// If user provides a size, make sure to append it to classes as input-
// e.g. if bsSize is small, it will append input-sm
if (bsSize) {
const size = SIZE_MAP[bsSize] || bsSize;
classes[prefix({ bsClass: 'input' }, size)] = true;
}
return (
<Component
{...elementProps}
type={type}
id={id}
ref={inputRef}
className={classNames(className, classes)}
/>
);
}
}
FormControl.propTypes = propTypes;
FormControl.defaultProps = defaultProps;
FormControl.contextTypes = contextTypes;
FormControl.Feedback = FormControlFeedback;
FormControl.Static = FormControlStatic;
export default bsClass('form-control',
bsSizes([Size.SMALL, Size.LARGE], FormControl)
);