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)

273 lines (226 loc) 9.51 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"); var _input = _interopRequireDefault(require("../form-control/input")); 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 _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; } /** * 验证码,需要先通过 setUkelliConfig 设置获取验证码的方式 * * @export * @class Captcha * @extends {Component} */ var Captcha = /*#__PURE__*/ function (_Component) { _inherits(Captcha, _Component); 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; return _this; } _createClass(Captcha, [{ key: "componentDidMount", value: function componentDidMount() { this.getCaptcha(); } }, { key: "select", value: function select() { this.captchaInput.select(); } }, { key: "getCaptcha", value: function getCaptcha(props) { var _this2 = this; props = props || this.props; this.refreshTime = Date.now(); var _props = props, onError = _props.onError; this.setState({ loading: true }); if (window.$UKE.queryCAPTCHAData) { window.$UKE.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 () { _this2.refreshCaptcha(); }, 1000); } else { _this2.setState({ loading: false, captchaImg: captchaImage }); _this2.captchaKey = captchaKey; _this2.props.onCaptchaLoad && _this2.props.onCaptchaLoad(); } }); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.__unmount = true; this.clearTimeout(); } }, { 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 clickTime = Date.now(); if (should || clickTime - this.refreshTime > 1 * 60 * 1000) { this.getCaptcha(); } this.select(); } }, { key: "refreshCaptcha", value: function refreshCaptcha() { this.shouldRefreshCaptcha(true); } }, { 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 = window.$UKE.getUkeKeyMap; var _this$state = this.state, captchaImg = _this$state.captchaImg, captchaValue = _this$state.captchaValue, loading = _this$state.loading; var _this$props = this.props, value = _this$props.value, icon = _this$props.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; }(_react.Component); exports.default = Captcha; _defineProperty(Captcha, "propTypes", { onError: _propTypes.default.func, onChange: _propTypes.default.func, onCaptchaLoad: _propTypes.default.func, onBlur: _propTypes.default.func, /** 与 react 受控控件行为一致 */ value: _propTypes.default.string, icon: _propTypes.default.string, limit: _propTypes.default.number }); _defineProperty(Captcha, "defaultProps", { limit: 4, icon: 'security' });