UNPKG

antd

Version:

An enterprise-class UI design language and React components implementation

121 lines (98 loc) 5.99 kB
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } 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 _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(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; 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; } var __rest = this && this.__rest || function (s, e) { var t = {}; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; } if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import * as React from 'react'; import classNames from 'classnames'; import omit from 'omit.js'; import EyeOutlined from '@ant-design/icons/EyeOutlined'; import EyeInvisibleOutlined from '@ant-design/icons/EyeInvisibleOutlined'; import { useState } from 'react'; import { ConfigConsumer } from '../config-provider'; import Input from './Input'; var ActionMap = { click: 'onClick', hover: 'onMouseOver' }; var Password = /*#__PURE__*/React.forwardRef(function (props, ref) { var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), visible = _useState2[0], setVisible = _useState2[1]; var onVisibleChange = function onVisibleChange() { var disabled = props.disabled; if (disabled) { return; } setVisible(!visible); }; var getIcon = function getIcon(prefixCls) { var _iconProps; var action = props.action, _props$iconRender = props.iconRender, iconRender = _props$iconRender === void 0 ? function () { return null; } : _props$iconRender; var iconTrigger = ActionMap[action] || ''; var icon = iconRender(visible); var iconProps = (_iconProps = {}, _defineProperty(_iconProps, iconTrigger, onVisibleChange), _defineProperty(_iconProps, "className", "".concat(prefixCls, "-icon")), _defineProperty(_iconProps, "key", 'passwordIcon'), _defineProperty(_iconProps, "onMouseDown", function onMouseDown(e) { // Prevent focused state lost // https://github.com/ant-design/ant-design/issues/15173 e.preventDefault(); }), _defineProperty(_iconProps, "onMouseUp", function onMouseUp(e) { // Prevent caret position change // https://github.com/ant-design/ant-design/issues/23524 e.preventDefault(); }), _iconProps); return /*#__PURE__*/React.cloneElement( /*#__PURE__*/React.isValidElement(icon) ? icon : /*#__PURE__*/React.createElement("span", null, icon), iconProps); }; var renderPassword = function renderPassword(_ref) { var getPrefixCls = _ref.getPrefixCls; var className = props.className, customizePrefixCls = props.prefixCls, customizeInputPrefixCls = props.inputPrefixCls, size = props.size, visibilityToggle = props.visibilityToggle, restProps = __rest(props, ["className", "prefixCls", "inputPrefixCls", "size", "visibilityToggle"]); var inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls); var prefixCls = getPrefixCls('input-password', customizePrefixCls); var suffixIcon = visibilityToggle && getIcon(prefixCls); var inputClassName = classNames(prefixCls, className, _defineProperty({}, "".concat(prefixCls, "-").concat(size), !!size)); var omittedProps = _extends(_extends({}, omit(restProps, ['suffix', 'iconRender'])), { type: visible ? 'text' : 'password', className: inputClassName, prefixCls: inputPrefixCls, suffix: suffixIcon }); if (size) { omittedProps.size = size; } return /*#__PURE__*/React.createElement(Input, _extends({ ref: ref }, omittedProps)); }; return /*#__PURE__*/React.createElement(ConfigConsumer, null, renderPassword); }); Password.defaultProps = { action: 'click', visibilityToggle: true, iconRender: function iconRender(visible) { return visible ? /*#__PURE__*/React.createElement(EyeOutlined, null) : /*#__PURE__*/React.createElement(EyeInvisibleOutlined, null); } }; Password.displayName = 'Password'; export default Password;