UNPKG

3.42 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6import classNames from 'classnames';
7import React from 'react';
8import PropTypes from 'prop-types';
9import elementType from 'prop-types-extra/lib/elementType';
10import warning from 'warning';
11
12import FormControlFeedback from './FormControlFeedback';
13import FormControlStatic from './FormControlStatic';
14import { prefix, bsClass, getClassSet, splitBsProps, bsSizes } from './utils/bootstrapUtils';
15import { SIZE_MAP, Size } from './utils/StyleConfig';
16
17var propTypes = {
18 componentClass: elementType,
19 /**
20 * Only relevant if `componentClass` is `'input'`.
21 */
22 type: PropTypes.string,
23 /**
24 * Uses `controlId` from `<FormGroup>` if not explicitly specified.
25 */
26 id: PropTypes.string,
27 /**
28 * Attaches a ref to the `<input>` element. Only functions can be used here.
29 *
30 * ```js
31 * <FormControl inputRef={ref => { this.input = ref; }} />
32 * ```
33 */
34 inputRef: PropTypes.func
35};
36
37var defaultProps = {
38 componentClass: 'input'
39};
40
41var contextTypes = {
42 $bs_formGroup: PropTypes.object
43};
44
45var FormControl = function (_React$Component) {
46 _inherits(FormControl, _React$Component);
47
48 function FormControl() {
49 _classCallCheck(this, FormControl);
50
51 return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
52 }
53
54 FormControl.prototype.render = function render() {
55 var formGroup = this.context.$bs_formGroup;
56 var controlId = formGroup && formGroup.controlId;
57
58 var _props = this.props,
59 Component = _props.componentClass,
60 type = _props.type,
61 _props$id = _props.id,
62 id = _props$id === undefined ? controlId : _props$id,
63 inputRef = _props.inputRef,
64 className = _props.className,
65 bsSize = _props.bsSize,
66 props = _objectWithoutProperties(_props, ['componentClass', 'type', 'id', 'inputRef', 'className', 'bsSize']);
67
68 var _splitBsProps = splitBsProps(props),
69 bsProps = _splitBsProps[0],
70 elementProps = _splitBsProps[1];
71
72 process.env.NODE_ENV !== 'production' ? warning(controlId == null || id === controlId, '`controlId` is ignored on `<FormControl>` when `id` is specified.') : void 0;
73
74 // input[type="file"] should not have .form-control.
75 var classes = void 0;
76 if (type !== 'file') {
77 classes = getClassSet(bsProps);
78 }
79
80 // If user provides a size, make sure to append it to classes as input-
81 // e.g. if bsSize is small, it will append input-sm
82 if (bsSize) {
83 var size = SIZE_MAP[bsSize] || bsSize;
84 classes[prefix({ bsClass: 'input' }, size)] = true;
85 }
86
87 return React.createElement(Component, _extends({}, elementProps, {
88 type: type,
89 id: id,
90 ref: inputRef,
91 className: classNames(className, classes)
92 }));
93 };
94
95 return FormControl;
96}(React.Component);
97
98FormControl.propTypes = propTypes;
99FormControl.defaultProps = defaultProps;
100FormControl.contextTypes = contextTypes;
101
102FormControl.Feedback = FormControlFeedback;
103FormControl.Static = FormControlStatic;
104
105export default bsClass('form-control', bsSizes([Size.SMALL, Size.LARGE], FormControl));
\No newline at end of file