UNPKG

8.39 kBJavaScriptView Raw
1function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2var _excluded = ["className", "cssModule", "type", "bsSize", "valid", "invalid", "tag", "addon", "plaintext", "innerRef"];
3function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
4function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
5function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
6function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
7function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
8function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
9function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
10function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
11function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
12function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
13function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
14function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
15function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
16/* eslint react/prefer-stateless-function: 0 */
17
18import React from 'react';
19import PropTypes from 'prop-types';
20import classNames from 'classnames';
21import { mapToCssModules, warnOnce, tagPropType } from './utils';
22var propTypes = {
23 children: PropTypes.node,
24 type: PropTypes.string,
25 size: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
26 bsSize: PropTypes.string,
27 valid: PropTypes.bool,
28 invalid: PropTypes.bool,
29 tag: tagPropType,
30 innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
31 plaintext: PropTypes.bool,
32 addon: PropTypes.bool,
33 className: PropTypes.string,
34 cssModule: PropTypes.object
35};
36var Input = /*#__PURE__*/function (_React$Component) {
37 _inherits(Input, _React$Component);
38 var _super = _createSuper(Input);
39 function Input(props) {
40 var _this;
41 _classCallCheck(this, Input);
42 _this = _super.call(this, props);
43 _this.getRef = _this.getRef.bind(_assertThisInitialized(_this));
44 _this.focus = _this.focus.bind(_assertThisInitialized(_this));
45 return _this;
46 }
47 _createClass(Input, [{
48 key: "getRef",
49 value: function getRef(ref) {
50 if (this.props.innerRef) {
51 this.props.innerRef(ref);
52 }
53 this.ref = ref;
54 }
55 }, {
56 key: "focus",
57 value: function focus() {
58 if (this.ref) {
59 this.ref.focus();
60 }
61 }
62 }, {
63 key: "render",
64 value: function render() {
65 var _this$props = this.props,
66 className = _this$props.className,
67 cssModule = _this$props.cssModule,
68 _this$props$type = _this$props.type,
69 type = _this$props$type === void 0 ? 'text' : _this$props$type,
70 bsSize = _this$props.bsSize,
71 valid = _this$props.valid,
72 invalid = _this$props.invalid,
73 tag = _this$props.tag,
74 addon = _this$props.addon,
75 plaintext = _this$props.plaintext,
76 innerRef = _this$props.innerRef,
77 attributes = _objectWithoutProperties(_this$props, _excluded);
78 var checkInput = ['switch', 'radio', 'checkbox'].indexOf(type) > -1;
79 var isNotaNumber = /\D/g;
80 var textareaInput = type === 'textarea';
81 var selectInput = type === 'select';
82 var rangeInput = type === 'range';
83 var Tag = tag || (selectInput || textareaInput ? type : 'input');
84 var formControlClass = 'form-control';
85 if (plaintext) {
86 formControlClass = "".concat(formControlClass, "-plaintext");
87 Tag = tag || 'input';
88 } else if (rangeInput) {
89 formControlClass = 'form-range';
90 } else if (selectInput) {
91 formControlClass = 'form-select';
92 } else if (checkInput) {
93 if (addon) {
94 formControlClass = null;
95 } else {
96 formControlClass = 'form-check-input';
97 }
98 }
99 if (attributes.size && isNotaNumber.test(attributes.size)) {
100 warnOnce('Please use the prop "bsSize" instead of the "size" to bootstrap\'s input sizing.');
101 bsSize = attributes.size;
102 delete attributes.size;
103 }
104 var classes = mapToCssModules(classNames(className, invalid && 'is-invalid', valid && 'is-valid', bsSize ? selectInput ? "form-select-".concat(bsSize) : "form-control-".concat(bsSize) : false, formControlClass), cssModule);
105 if (Tag === 'input' || tag && typeof tag === 'function') {
106 attributes.type = type === 'switch' ? 'checkbox' : type;
107 }
108 if (attributes.children && !(plaintext || type === 'select' || typeof Tag !== 'string' || Tag === 'select')) {
109 warnOnce("Input with a type of \"".concat(type, "\" cannot have children. Please use \"value\"/\"defaultValue\" instead."));
110 delete attributes.children;
111 }
112 return /*#__PURE__*/React.createElement(Tag, _extends({}, attributes, {
113 ref: innerRef,
114 className: classes,
115 "aria-invalid": invalid
116 }));
117 }
118 }]);
119 return Input;
120}(React.Component);
121Input.propTypes = propTypes;
122export default Input;
\No newline at end of file