UNPKG

4.72 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _extends2 = require('babel-runtime/helpers/extends');
6
7var _extends3 = _interopRequireDefault(_extends2);
8
9var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
10
11var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
12
13var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
14
15var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
16
17var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
18
19var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
20
21var _inherits2 = require('babel-runtime/helpers/inherits');
22
23var _inherits3 = _interopRequireDefault(_inherits2);
24
25var _classnames = require('classnames');
26
27var _classnames2 = _interopRequireDefault(_classnames);
28
29var _react = require('react');
30
31var _react2 = _interopRequireDefault(_react);
32
33var _propTypes = require('prop-types');
34
35var _propTypes2 = _interopRequireDefault(_propTypes);
36
37var _elementType = require('prop-types-extra/lib/elementType');
38
39var _elementType2 = _interopRequireDefault(_elementType);
40
41var _warning = require('warning');
42
43var _warning2 = _interopRequireDefault(_warning);
44
45var _FormControlFeedback = require('./FormControlFeedback');
46
47var _FormControlFeedback2 = _interopRequireDefault(_FormControlFeedback);
48
49var _FormControlStatic = require('./FormControlStatic');
50
51var _FormControlStatic2 = _interopRequireDefault(_FormControlStatic);
52
53var _bootstrapUtils = require('./utils/bootstrapUtils');
54
55var _StyleConfig = require('./utils/StyleConfig');
56
57function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
58
59var propTypes = {
60 componentClass: _elementType2.default,
61 /**
62 * Only relevant if `componentClass` is `'input'`.
63 */
64 type: _propTypes2.default.string,
65 /**
66 * Uses `controlId` from `<FormGroup>` if not explicitly specified.
67 */
68 id: _propTypes2.default.string,
69 /**
70 * Attaches a ref to the `<input>` element. Only functions can be used here.
71 *
72 * ```js
73 * <FormControl inputRef={ref => { this.input = ref; }} />
74 * ```
75 */
76 inputRef: _propTypes2.default.func
77};
78
79var defaultProps = {
80 componentClass: 'input'
81};
82
83var contextTypes = {
84 $bs_formGroup: _propTypes2.default.object
85};
86
87var FormControl = function (_React$Component) {
88 (0, _inherits3.default)(FormControl, _React$Component);
89
90 function FormControl() {
91 (0, _classCallCheck3.default)(this, FormControl);
92 return (0, _possibleConstructorReturn3.default)(this, _React$Component.apply(this, arguments));
93 }
94
95 FormControl.prototype.render = function render() {
96 var formGroup = this.context.$bs_formGroup;
97 var controlId = formGroup && formGroup.controlId;
98
99 var _props = this.props,
100 Component = _props.componentClass,
101 type = _props.type,
102 _props$id = _props.id,
103 id = _props$id === undefined ? controlId : _props$id,
104 inputRef = _props.inputRef,
105 className = _props.className,
106 bsSize = _props.bsSize,
107 props = (0, _objectWithoutProperties3.default)(_props, ['componentClass', 'type', 'id', 'inputRef', 'className', 'bsSize']);
108
109 var _splitBsProps = (0, _bootstrapUtils.splitBsProps)(props),
110 bsProps = _splitBsProps[0],
111 elementProps = _splitBsProps[1];
112
113 process.env.NODE_ENV !== 'production' ? (0, _warning2.default)(controlId == null || id === controlId, '`controlId` is ignored on `<FormControl>` when `id` is specified.') : void 0;
114
115 // input[type="file"] should not have .form-control.
116 var classes = void 0;
117 if (type !== 'file') {
118 classes = (0, _bootstrapUtils.getClassSet)(bsProps);
119 }
120
121 // If user provides a size, make sure to append it to classes as input-
122 // e.g. if bsSize is small, it will append input-sm
123 if (bsSize) {
124 var size = _StyleConfig.SIZE_MAP[bsSize] || bsSize;
125 classes[(0, _bootstrapUtils.prefix)({ bsClass: 'input' }, size)] = true;
126 }
127
128 return _react2.default.createElement(Component, (0, _extends3.default)({}, elementProps, {
129 type: type,
130 id: id,
131 ref: inputRef,
132 className: (0, _classnames2.default)(className, classes)
133 }));
134 };
135
136 return FormControl;
137}(_react2.default.Component);
138
139FormControl.propTypes = propTypes;
140FormControl.defaultProps = defaultProps;
141FormControl.contextTypes = contextTypes;
142
143FormControl.Feedback = _FormControlFeedback2.default;
144FormControl.Static = _FormControlStatic2.default;
145
146exports.default = (0, _bootstrapUtils.bsClass)('form-control', (0, _bootstrapUtils.bsSizes)([_StyleConfig.Size.SMALL, _StyleConfig.Size.LARGE], FormControl));
147module.exports = exports['default'];
\No newline at end of file