UNPKG

4.86 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7var _react = _interopRequireDefault(require("react"));
8var _propTypes = _interopRequireDefault(require("prop-types"));
9var _classnames = _interopRequireDefault(require("classnames"));
10var _utils = require("./utils");
11const _excluded = ["className", "cssModule", "type", "bsSize", "valid", "invalid", "tag", "addon", "plaintext", "innerRef"];
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13function _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); }
14function _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; }
15function _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; }
16const propTypes = {
17 children: _propTypes.default.node,
18 type: _propTypes.default.string,
19 size: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
20 bsSize: _propTypes.default.string,
21 valid: _propTypes.default.bool,
22 invalid: _propTypes.default.bool,
23 tag: _utils.tagPropType,
24 innerRef: _propTypes.default.oneOfType([_propTypes.default.object, _propTypes.default.func, _propTypes.default.string]),
25 plaintext: _propTypes.default.bool,
26 addon: _propTypes.default.bool,
27 className: _propTypes.default.string,
28 cssModule: _propTypes.default.object
29};
30class Input extends _react.default.Component {
31 constructor(props) {
32 super(props);
33 this.getRef = this.getRef.bind(this);
34 this.focus = this.focus.bind(this);
35 }
36 getRef(ref) {
37 if (this.props.innerRef) {
38 this.props.innerRef(ref);
39 }
40 this.ref = ref;
41 }
42 focus() {
43 if (this.ref) {
44 this.ref.focus();
45 }
46 }
47 render() {
48 let _this$props = this.props,
49 {
50 className,
51 cssModule,
52 type = 'text',
53 bsSize,
54 valid,
55 invalid,
56 tag,
57 addon,
58 plaintext,
59 innerRef
60 } = _this$props,
61 attributes = _objectWithoutProperties(_this$props, _excluded);
62 const checkInput = ['switch', 'radio', 'checkbox'].indexOf(type) > -1;
63 const isNotaNumber = /\D/g;
64 const textareaInput = type === 'textarea';
65 const selectInput = type === 'select';
66 const rangeInput = type === 'range';
67 let Tag = tag || (selectInput || textareaInput ? type : 'input');
68 let formControlClass = 'form-control';
69 if (plaintext) {
70 formControlClass = `${formControlClass}-plaintext`;
71 Tag = tag || 'input';
72 } else if (rangeInput) {
73 formControlClass = 'form-range';
74 } else if (selectInput) {
75 formControlClass = 'form-select';
76 } else if (checkInput) {
77 if (addon) {
78 formControlClass = null;
79 } else {
80 formControlClass = 'form-check-input';
81 }
82 }
83 if (attributes.size && isNotaNumber.test(attributes.size)) {
84 (0, _utils.warnOnce)('Please use the prop "bsSize" instead of the "size" to bootstrap\'s input sizing.');
85 bsSize = attributes.size;
86 delete attributes.size;
87 }
88 const classes = (0, _utils.mapToCssModules)((0, _classnames.default)(className, invalid && 'is-invalid', valid && 'is-valid', bsSize ? selectInput ? `form-select-${bsSize}` : `form-control-${bsSize}` : false, formControlClass), cssModule);
89 if (Tag === 'input' || tag && typeof tag === 'function') {
90 attributes.type = type === 'switch' ? 'checkbox' : type;
91 }
92 if (attributes.children && !(plaintext || type === 'select' || typeof Tag !== 'string' || Tag === 'select')) {
93 (0, _utils.warnOnce)(`Input with a type of "${type}" cannot have children. Please use "value"/"defaultValue" instead.`);
94 delete attributes.children;
95 }
96 return /*#__PURE__*/_react.default.createElement(Tag, _extends({}, attributes, {
97 ref: innerRef,
98 className: classes,
99 "aria-invalid": invalid
100 }));
101 }
102}
103Input.propTypes = propTypes;
104var _default = Input;
105exports.default = _default;
\No newline at end of file