UNPKG

@cocoy/kit-components

Version:

一些前端组件

170 lines 8.8 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } import { useState } from 'react'; import { Form, Input, Progress, Popover } from 'antd'; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; import { Fragment as _Fragment } from "react/jsx-runtime"; var isPassword = function isPassword(string) { return /^(?![a-zA-Z]+$)(?!\d+$)(?![^\da-zA-Z\s]+$).{8,20}$/.test(string); }; var isPasswordStrongly = function isPasswordStrongly(string) { if (/^(?=.*\d)(?=.*[a-zA-Z])(?=.*[^\da-zA-Z\s]).{8,20}$/.test(string)) { return 'success'; } else if (/^(?![a-zA-Z]+$)(?!\d+$)(?![^\da-zA-Z\s]+$).{8,20}$/.test(string)) { return 'normal'; } else { return 'exception'; } }; var passwordStrengthMap = { success: { text: '强', percent: 100 }, normal: { text: '中', percent: 70 }, exception: { text: '弱', percent: 30 } }; export var MangoFormPassword = function MangoFormPassword(_ref) { var _ref$widthLabel = _ref.widthLabel, widthLabel = _ref$widthLabel === void 0 ? false : _ref$widthLabel, _ref$label = _ref.label, label = _ref$label === void 0 ? [] : _ref$label, _ref$widthConfirm = _ref.widthConfirm, widthConfirm = _ref$widthConfirm === void 0 ? true : _ref$widthConfirm, popoverProps = _ref.popoverProps; var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), popoverVisible = _useState2[0], setPopoverVisible = _useState2[1]; return /*#__PURE__*/_jsxs(_Fragment, { children: [/*#__PURE__*/_jsx(Popover, _objectSpread(_objectSpread({ content: /*#__PURE__*/_jsx(Form.Item, { shouldUpdate: function shouldUpdate(prevValues, curValues) { return (prevValues === null || prevValues === void 0 ? void 0 : prevValues['password']) !== (curValues === null || curValues === void 0 ? void 0 : curValues['password']); }, noStyle: true, children: function children(form) { var _form$getFieldsValue = form.getFieldsValue(['password']), password = _form$getFieldsValue.password; var passwordStrengthStatus = isPasswordStrongly(password); return popoverVisible && /*#__PURE__*/_jsxs("div", { style: { padding: '5px 10px' }, children: [/*#__PURE__*/_jsxs("div", { children: [/*#__PURE__*/_jsx("span", { children: "\u5F3A\u5EA6\uFF1A" }), /*#__PURE__*/_jsx("span", { children: passwordStrengthMap[passwordStrengthStatus]['text'] })] }), /*#__PURE__*/_jsx(Progress, { percent: passwordStrengthMap[passwordStrengthStatus]['percent'], showInfo: false, status: passwordStrengthStatus }), /*#__PURE__*/_jsx("div", { style: { marginTop: 10 }, children: /*#__PURE__*/_jsx("span", { children: "\u8BF7\u8F93\u5165 8-20 \u4F4D\u5BC6\u7801\uFF0C\u5FC5\u987B\u5305\u542B\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u7B26\u53F7\u4E2D\u81F3\u5C112\u79CD" }) })] }); } }), open: popoverVisible, placement: "top" }, popoverProps), {}, { children: /*#__PURE__*/_jsx(Form.Item, { label: widthLabel ? label[0] || '密码' : null, name: "password", normalize: function normalize(value) { return value.trim(); }, rules: [{ required: true, message: '请输入密码' }, { min: 8, message: '密码最小长度为8' }, { max: 20, message: '密码最大长度为20' }, function () { return { validator: function validator(_, value) { if (!value) { return Promise.resolve(); } else { if (!isPassword(value)) { return Promise.reject(new Error('请输入 8-20 位密码,必须包含字母、数字、符号中至少2种')); } else { return Promise.resolve(); } } } }; }], children: /*#__PURE__*/_jsx(Input.Password, { maxLength: 20, placeholder: "\u8BF7\u8F93\u5165\u5BC6\u7801", allowClear: true, onBlur: function onBlur() { setPopoverVisible(false); }, onFocus: function onFocus() { setPopoverVisible(true); } }) }) })), widthConfirm && /*#__PURE__*/_jsx(Form.Item, { dependencies: ['password'], label: widthLabel ? label[1] || '确认密码' : null, name: "confirmPassword", normalize: function normalize(value) { return value.trim(); }, rules: [{ required: true, message: '请再次输入密码' }, { min: 8, message: '密码最小长度为8' }, { max: 20, message: '密码最大长度为20' }, function (_form) { return { validator: function validator(_, value) { if (!value || _form.getFieldValue('password') === value) { return Promise.resolve(); } return Promise.reject(new Error('两次输入密码不匹配')); } }; }], children: /*#__PURE__*/_jsx(Input.Password, { maxLength: 20, placeholder: "\u8BF7\u518D\u6B21\u8F93\u5165\u5BC6\u7801", allowClear: true }) })] }); };