UNPKG

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