Password.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import * as React from 'react';
  11. import { useRef, useState } from 'react';
  12. import EyeInvisibleOutlined from "@ant-design/icons/es/icons/EyeInvisibleOutlined";
  13. import EyeOutlined from "@ant-design/icons/es/icons/EyeOutlined";
  14. import classNames from 'classnames';
  15. import omit from "rc-util/es/omit";
  16. import { composeRef } from "rc-util/es/ref";
  17. import { ConfigContext } from '../config-provider';
  18. import DisabledContext from '../config-provider/DisabledContext';
  19. import useRemovePasswordTimeout from './hooks/useRemovePasswordTimeout';
  20. import Input from './Input';
  21. const defaultIconRender = visible => visible ? /*#__PURE__*/React.createElement(EyeOutlined, null) : /*#__PURE__*/React.createElement(EyeInvisibleOutlined, null);
  22. const actionMap = {
  23. click: 'onClick',
  24. hover: 'onMouseOver'
  25. };
  26. const Password = /*#__PURE__*/React.forwardRef((props, ref) => {
  27. const {
  28. disabled: customDisabled,
  29. action = 'click',
  30. visibilityToggle = true,
  31. iconRender = defaultIconRender,
  32. suffix
  33. } = props;
  34. // ===================== Disabled =====================
  35. const disabled = React.useContext(DisabledContext);
  36. const mergedDisabled = customDisabled !== null && customDisabled !== void 0 ? customDisabled : disabled;
  37. const visibilityControlled = typeof visibilityToggle === 'object' && visibilityToggle.visible !== undefined;
  38. const [visible, setVisible] = useState(() => visibilityControlled ? visibilityToggle.visible : false);
  39. const inputRef = useRef(null);
  40. React.useEffect(() => {
  41. if (visibilityControlled) {
  42. setVisible(visibilityToggle.visible);
  43. }
  44. }, [visibilityControlled, visibilityToggle]);
  45. // Remove Password value
  46. const removePasswordTimeout = useRemovePasswordTimeout(inputRef);
  47. const onVisibleChange = () => {
  48. var _a;
  49. if (mergedDisabled) {
  50. return;
  51. }
  52. if (visible) {
  53. removePasswordTimeout();
  54. }
  55. const nextVisible = !visible;
  56. setVisible(nextVisible);
  57. if (typeof visibilityToggle === 'object') {
  58. (_a = visibilityToggle.onVisibleChange) === null || _a === void 0 ? void 0 : _a.call(visibilityToggle, nextVisible);
  59. }
  60. };
  61. const getIcon = prefixCls => {
  62. const iconTrigger = actionMap[action] || '';
  63. const icon = iconRender(visible);
  64. const iconProps = {
  65. [iconTrigger]: onVisibleChange,
  66. className: `${prefixCls}-icon`,
  67. key: 'passwordIcon',
  68. onMouseDown: e => {
  69. // Prevent focused state lost
  70. // https://github.com/ant-design/ant-design/issues/15173
  71. e.preventDefault();
  72. },
  73. onMouseUp: e => {
  74. // Prevent caret position change
  75. // https://github.com/ant-design/ant-design/issues/23524
  76. e.preventDefault();
  77. }
  78. };
  79. return /*#__PURE__*/React.cloneElement(/*#__PURE__*/React.isValidElement(icon) ? icon : /*#__PURE__*/React.createElement("span", null, icon), iconProps);
  80. };
  81. const {
  82. className,
  83. prefixCls: customizePrefixCls,
  84. inputPrefixCls: customizeInputPrefixCls,
  85. size
  86. } = props,
  87. restProps = __rest(props, ["className", "prefixCls", "inputPrefixCls", "size"]);
  88. const {
  89. getPrefixCls
  90. } = React.useContext(ConfigContext);
  91. const inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
  92. const prefixCls = getPrefixCls('input-password', customizePrefixCls);
  93. const suffixIcon = visibilityToggle && getIcon(prefixCls);
  94. const inputClassName = classNames(prefixCls, className, {
  95. [`${prefixCls}-${size}`]: !!size
  96. });
  97. const omittedProps = Object.assign(Object.assign({}, omit(restProps, ['suffix', 'iconRender', 'visibilityToggle'])), {
  98. type: visible ? 'text' : 'password',
  99. className: inputClassName,
  100. prefixCls: inputPrefixCls,
  101. suffix: (/*#__PURE__*/React.createElement(React.Fragment, null, suffixIcon, suffix))
  102. });
  103. if (size) {
  104. omittedProps.size = size;
  105. }
  106. return /*#__PURE__*/React.createElement(Input, Object.assign({
  107. ref: composeRef(ref, inputRef)
  108. }, omittedProps));
  109. });
  110. if (process.env.NODE_ENV !== 'production') {
  111. Password.displayName = 'Input.Password';
  112. }
  113. export default Password;