UNPKG

@livelybone/react-input

Version:

A react input component, realized input validator, formatter. `textarea` available

309 lines (256 loc) 8.55 kB
/** * Bundle of @livelybone/react-input * Generated: 2020-05-26 * Version: 2.5.0 * License: MIT * Author: 2631541504@qq.com */ import React from 'react'; function _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; } function _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; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread2(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _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 } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } var ReactInput = /*#__PURE__*/function (_React$Component) { _inherits(ReactInput, _React$Component); var _super = _createSuper(ReactInput); function ReactInput() { var _this; _classCallCheck(this, ReactInput); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _this.isCompositionUpdate = false; _this.inputEl = void 0; _this.onComposition = function (eventName, ev) { ev.stopPropagation(); var eventHandler = _this.props[eventName]; if (eventHandler) eventHandler(ev); _this.isCompositionUpdate = eventName !== 'onCompositionEnd'; if (!_this.isCompositionUpdate) { var $ev = _objectSpread2({}, ev); setTimeout(function () { _this.onChange($ev); }); } }; _this.onChange = function (ev) { if (_this.props.onChange && _this.shouldCallChange) { _this.props.onChange(ev); } }; return _this; } _createClass(ReactInput, [{ key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { if (this.shouldCallChange && this.inputEl && this.props.value !== undefined) { this.inputEl.value = this.props.value; } } }, { key: "componentDidMount", value: function componentDidMount() { if (this.shouldCallChange && this.inputEl && this.props.value) { this.inputEl.value = this.props.value; } } }, { key: "render", value: function render() { var props = this.$props; var type = this.props.type || 'text'; return type !== 'textarea' ? /*#__PURE__*/React.createElement("input", Object.assign({}, props, { type: type })) : /*#__PURE__*/React.createElement("textarea", props); } }, { key: "$props", get: function get() { var _this2 = this; var _this$props = this.props, value = _this$props.value, type = _this$props.type, autoComplete = _this$props.autoComplete, shouldCompositionEventTriggerChangeEvent = _this$props.shouldCompositionEventTriggerChangeEvent, rest = _objectWithoutProperties(_this$props, ["value", "type", "autoComplete", "shouldCompositionEventTriggerChangeEvent"]); return _objectSpread2({}, rest, { value: undefined, autoComplete: type === 'password' && autoComplete === 'off' ? 'new-password' : autoComplete, defaultValue: value, ref: function ref(_ref) { _this2.inputEl = _ref; }, onChange: this.onChange, onCompositionEnd: this.onComposition.bind(this, 'onCompositionEnd'), onCompositionStart: this.onComposition.bind(this, 'onCompositionStart'), onCompositionUpdate: this.onComposition.bind(this, 'onCompositionUpdate') }); } }, { key: "shouldCallChange", get: function get() { return this.props.shouldCompositionEventTriggerChangeEvent || !this.isCompositionUpdate; } }]); return ReactInput; }(React.Component); export default ReactInput;