UNPKG

ukelli-ui

Version:

ukelli-ui 是基于 React 的 UI 库,提供简约和功能齐全的组件,可高度定制的组件接口,灵活的配置,提供给开发者另一种开发思路,也致力于尝试不同的组件使用和开发方向。

306 lines (248 loc) 10.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _basicHelper = require("basic-helper"); var _ukeUtils = require("../uke-utils"); var _input = _interopRequireDefault(require("../form-control/input")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } 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 _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; } var queryCAPTCHAData = null; /** * 验证码,需要先通过 setUkelliConfig 设置获取验证码的方式 * * @export * @class Captcha * @extends {Component} */ var Captcha = /*#__PURE__*/ function (_UkeComponent) { _inherits(Captcha, _UkeComponent); function Captcha(props) { var _this; _classCallCheck(this, Captcha); _this = _possibleConstructorReturn(this, _getPrototypeOf(Captcha).call(this, props)); _this.state = { captchaImg: '', captchaValue: '', loading: false }; _this.isControl = props.hasOwnProperty('value'); _this.captchaLength = props.limit || 4; _this.isPass = false; _this.value = props.value; _this.captchaKey = ''; _this.refreshTime = 0; // 自动尝试刷新验证码 _this.retryTime = 0; return _this; } _createClass(Captcha, [{ key: "componentDidMount", value: function componentDidMount() { this.getCaptcha(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.__unmount = true; this.clearTimeout(); } }, { key: "select", value: function select() { this.captchaInput.select(); } }, { key: "getCaptcha", value: function getCaptcha(props) { var _this2 = this; this.refreshTime = Date.now(); var _this$props = this.props, autoRetryTime = _this$props.autoRetryTime, onError = _this$props.onError, onCaptchaLoad = _this$props.onCaptchaLoad; var _queryCAPTCHAData = queryCAPTCHAData || window.$UKE.queryCAPTCHAData; this.setState({ loading: true }); if (_queryCAPTCHAData) { _queryCAPTCHAData(function (options) { if (_this2.__unmount) return; var hasErr = options.hasErr, captchaImage = options.captchaImage, captchaKey = options.captchaKey; if (hasErr) { _this2.clearTimeout(); _this2.getCaptchaTimer = setTimeout(function () { // 如果自动刷新次数少于设置项,则自动刷新 if (_this2.retryTime < autoRetryTime) _this2.refreshCaptcha(false); _this2.retryTime++; }, 1000); } else { _this2.setState({ loading: false, captchaImg: captchaImage }); _this2.captchaKey = captchaKey; (0, _basicHelper.Call)(onCaptchaLoad, captchaKey); } }); } else { console.log("\u8BF7\u5148\u8BBE\u7F6E\u83B7\u53D6\u9A8C\u8BC1\u7801\u7684\u63A5\u53E3, \u8BE6\u60C5 https://ui.ukelli.com/#/Captcha"); } } }, { key: "clearTimeout", value: function (_clearTimeout) { function clearTimeout() { return _clearTimeout.apply(this, arguments); } clearTimeout.toString = function () { return _clearTimeout.toString(); }; return clearTimeout; }(function () { this.getCaptchaTimer && clearTimeout(this.getCaptchaTimer); }) }, { key: "shouldRefreshCaptcha", value: function shouldRefreshCaptcha() { var should = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; var needFocus = arguments.length > 1 ? arguments[1] : undefined; var clickTime = Date.now(); if (should || clickTime - this.refreshTime > 1 * 60 * 1000) { this.getCaptcha(); } if (needFocus) this.select(); } }, { key: "refreshCaptcha", value: function refreshCaptcha() { var needFocus = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; this.shouldRefreshCaptcha(true, needFocus); } }, { key: "changeCaptcha", value: function changeCaptcha(val) { var onChange = this.props.onChange; var _val = val.length > this.captchaLength ? val.slice(0, this.captchaLength) : val; if (val.length === this.captchaLength) this.isPass = true; if (!this.isControl) { this.setState({ captchaValue: _val }); } this.value = _val; (0, _basicHelper.Call)(onChange, { isPass: this.isPass, value: _val, key: this.captchaKey // forUsername: this.forUsername }); } }, { key: "focus", value: function focus() { this.captchaInput.focus(); } }, { key: "render", value: function render() { var _this3 = this; var gm = this.gm; var _this$state = this.state, captchaImg = _this$state.captchaImg, captchaValue = _this$state.captchaValue, loading = _this$state.loading; var _this$props2 = this.props, value = _this$props2.value, icon = _this$props2.icon; var _captchaValue = this.isControl ? value : captchaValue; var hasCap = !!captchaImg; var loadingTip = null; var captchaImgElem = hasCap && !loading ? _react["default"].createElement("img", { src: captchaImg, alt: "", className: "cover-image" }) : null; if (!hasCap) { loadingTip = gm('验证码'); } if (loading) { loadingTip = gm('刷新中'); } return _react["default"].createElement("div", { className: "captcha-group" }, _react["default"].createElement(_input["default"], { ref: function ref(e) { return _this3.captchaInput = e; }, icon: icon, type: "number", className: "form-control captcha-input", value: _captchaValue, onFocus: function onFocus(e) { return _this3.shouldRefreshCaptcha(); }, onChange: function onChange(val) { return _this3.changeCaptcha(val); }, placeholder: gm("验证码") }, _react["default"].createElement("div", { className: "captcha", onClick: function onClick(e) { _this3.getCaptcha(); } }, _react["default"].createElement("div", { className: "text-center captcha-tip" + (!loading && hasCap ? ' hide' : '') }, loadingTip), captchaImgElem))); // <input // type="text" // className={"form-control captcha-input input-" + scale} // ref="captchaInput" // value={_captchaValue} // onFocus={e => this.shouldRefreshCaptcha()} // onChange={e => this.changeCaptcha(e.target.value)} // placeholder="验证码"/> } }]); return Captcha; }(_ukeUtils.UkeComponent); exports["default"] = Captcha; _defineProperty(Captcha, "propTypes", { /** 获取错误时的回调 */ onError: _propTypes["default"].func, /** 值改变时的回调 */ onChange: _propTypes["default"].func, /** 验证码 Mount 的回调 */ onCaptchaLoad: _propTypes["default"].func, /** 失去焦点的回调 */ onBlur: _propTypes["default"].func, /** 与 react 受控控件行为一致 */ value: _propTypes["default"].string, /** icon */ icon: _propTypes["default"].string, /** 限制输入长度 */ limit: _propTypes["default"].number, /** 尝试自动刷新验证码的次数 */ autoRetryTime: _propTypes["default"].number }); _defineProperty(Captcha, "defaultProps", { limit: 4, autoRetryTime: 10, icon: 'security' }); _defineProperty(Captcha, "setQueryCAPTCHAData", function (func) { queryCAPTCHAData = func; });