UNPKG

antd

Version:

An enterprise-class UI design language and React components implementation

109 lines (93 loc) 4.17 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; 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 "rc-util/es/omit"; import EyeOutlined from "@ant-design/icons/es/icons/EyeOutlined"; import EyeInvisibleOutlined from "@ant-design/icons/es/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;