UNPKG

ukelli-ui

Version:

[![Build Status](https://travis-ci.org/ukelli/ukelli-ui.svg?branch=master)](https://travis-ci.org/ukelli/ukelli-ui) [![install size](https://packagephobia.now.sh/badge?p=ukelli-ui)](https://packagephobia.now.sh/result?p=ukelli-ui)

209 lines (163 loc) 8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _basicHelper = require("basic-helper"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } function _typeof(obj) { 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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } 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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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 _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } 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; } /** * 基础的输入框验证 class * * @export * @class InputVerifyClass * @extends {Component} */ var InputVerifyClass = /*#__PURE__*/ function (_Component) { _inherits(InputVerifyClass, _Component); function InputVerifyClass(props) { var _this; _classCallCheck(this, InputVerifyClass); _this = _possibleConstructorReturn(this, _getPrototypeOf(InputVerifyClass).call(this, props)); var defaultValue = props.defaultValue, value = props.value; _this.isControl = _this.checkProps('value'); _this.isMatchLen = _this.checkProps('lenRange'); _this.isMatchNumbRangeMode = _this.checkProps('numRange'); _this.isPass = _this.checkProps('lenRange'); _this.value = !_this.isControl ? defaultValue : value; _this.state = { value: defaultValue, matchLen: true, matchRange: true }; return _this; } _createClass(InputVerifyClass, [{ key: "checkProps", value: function checkProps(field) { return !!this.props.hasOwnProperty(field); } }, { key: "getValue", value: function getValue() { return this.isControl ? this.props.value : this.state.value; } }, { key: "_onChange", value: function _onChange(val, props) { var _ref = props || this.props, onChange = _ref.onChange, lenRange = _ref.lenRange, numRange = _ref.numRange, precent = _ref.precent; var _val = val; _val = this.checkLen(_val, lenRange); _val = this.checkNum(_val, numRange); this.setState({ value: _val }); this.isPass = this.isMatchLen && this.isMatchNumbRangeMode; this.value = _val; (0, _basicHelper.Call)(onChange, _val); } }, { key: "_onFocus", value: function _onFocus() { var onFocus = this.props.onFocus; (0, _basicHelper.Call)(onFocus); } }, { key: "_onBlur", value: function _onBlur() { (0, _basicHelper.Call)(this.props.onBlur); } }, { key: "onClear", value: function onClear() { this._onChange(''); } }, { key: "checkLen", value: function checkLen(val, lenRange) { var _val = val; if (lenRange) { var _lenRange = _slicedToArray(lenRange, 3), s = _lenRange[0], e = _lenRange[1], isSlice = _lenRange[2]; if (_val.length > e) { if (isSlice) { _val = _val.slice(0, e); } } } return _val; } }, { key: "checkNum", value: function checkNum(val, numRange) { var _val = +val; var matchRange = this.state.matchRange; if (this.isMatchNumbRangeMode) { var isMatch = false; var _numRange = _slicedToArray(numRange, 2), s = _numRange[0], e = _numRange[1]; if (_val > e) _val = e; if (_val < s) { isMatch = false; } else { isMatch = true; } matchRange !== isMatch && this.setState({ matchRange: isMatch }); } return _val; } }]); return InputVerifyClass; }(_react.Component); exports.default = InputVerifyClass; _defineProperty(InputVerifyClass, "propTypes", { onChange: _propTypes.default.func, onFocus: _propTypes.default.func, onBlur: _propTypes.default.func, onClear: _propTypes.default.func, className: _propTypes.default.string, defaultValue: _propTypes.default.any, value: _propTypes.default.any, /** 控件类型 */ type: _propTypes.default.oneOf(['text', 'password']), /** 是否开启数字转中文 */ needCN: _propTypes.default.bool, /** 是否开启百分比模式 */ precent: _propTypes.default.bool, /** 是否可输入 */ inputable: _propTypes.default.bool, /** 限制输入数字的范围 */ numRange: _propTypes.default.arrayOf(_propTypes.default.number), /** 辅助按钮每一次的增减单位 */ unit: _propTypes.default.number, /** 限制输入的字符串长度范围 */ lenRange: _propTypes.default.arrayOf(_propTypes.default.number) });