1
This commit is contained in:
+108
@@ -0,0 +1,108 @@
|
||||
"use client";
|
||||
|
||||
import * as React from 'react';
|
||||
import { useRef, useState } from 'react';
|
||||
import EyeInvisibleOutlined from "@ant-design/icons/es/icons/EyeInvisibleOutlined";
|
||||
import EyeOutlined from "@ant-design/icons/es/icons/EyeOutlined";
|
||||
import { omit } from '@rc-component/util';
|
||||
import { composeRef } from "@rc-component/util/es/ref";
|
||||
import { clsx } from 'clsx';
|
||||
import { isPlainObject } from '../_util/is';
|
||||
import { ConfigContext } from '../config-provider';
|
||||
import DisabledContext from '../config-provider/DisabledContext';
|
||||
import useRemovePasswordTimeout from './hooks/useRemovePasswordTimeout';
|
||||
import Input from './Input';
|
||||
const defaultIconRender = visible => visible ? /*#__PURE__*/React.createElement(EyeOutlined, null) : /*#__PURE__*/React.createElement(EyeInvisibleOutlined, null);
|
||||
const actionMap = {
|
||||
click: 'onClick',
|
||||
hover: 'onMouseOver'
|
||||
};
|
||||
const Password = /*#__PURE__*/React.forwardRef((props, ref) => {
|
||||
const {
|
||||
disabled: customDisabled,
|
||||
action = 'click',
|
||||
visibilityToggle = true,
|
||||
iconRender = defaultIconRender,
|
||||
suffix
|
||||
} = props;
|
||||
// ===================== Disabled =====================
|
||||
const disabled = React.useContext(DisabledContext);
|
||||
const mergedDisabled = customDisabled ?? disabled;
|
||||
const visibilityControlled = isPlainObject(visibilityToggle) && visibilityToggle.visible !== undefined;
|
||||
const [visible, setVisible] = useState(() => visibilityControlled ? visibilityToggle.visible : false);
|
||||
const inputRef = useRef(null);
|
||||
React.useEffect(() => {
|
||||
if (visibilityControlled) {
|
||||
setVisible(visibilityToggle.visible);
|
||||
}
|
||||
}, [visibilityControlled, visibilityToggle]);
|
||||
// Remove Password value
|
||||
const removePasswordTimeout = useRemovePasswordTimeout(inputRef);
|
||||
const onVisibleChange = () => {
|
||||
if (mergedDisabled) {
|
||||
return;
|
||||
}
|
||||
if (visible) {
|
||||
removePasswordTimeout();
|
||||
}
|
||||
const nextVisible = !visible;
|
||||
setVisible(nextVisible);
|
||||
if (isPlainObject(visibilityToggle)) {
|
||||
visibilityToggle.onVisibleChange?.(nextVisible);
|
||||
}
|
||||
};
|
||||
const getIcon = prefixCls => {
|
||||
const iconTrigger = actionMap[action] || '';
|
||||
const icon = iconRender(visible);
|
||||
const iconProps = {
|
||||
[iconTrigger]: onVisibleChange,
|
||||
className: `${prefixCls}-icon`,
|
||||
key: 'passwordIcon',
|
||||
onMouseDown: e => {
|
||||
// Prevent focused state lost
|
||||
// https://github.com/ant-design/ant-design/issues/15173
|
||||
e.preventDefault();
|
||||
},
|
||||
onMouseUp: e => {
|
||||
// Prevent caret position change
|
||||
// https://github.com/ant-design/ant-design/issues/23524
|
||||
e.preventDefault();
|
||||
}
|
||||
};
|
||||
return /*#__PURE__*/React.cloneElement(/*#__PURE__*/React.isValidElement(icon) ? icon : /*#__PURE__*/React.createElement("span", null, icon), iconProps);
|
||||
};
|
||||
const {
|
||||
className,
|
||||
prefixCls: customizePrefixCls,
|
||||
inputPrefixCls: customizeInputPrefixCls,
|
||||
size,
|
||||
...restProps
|
||||
} = props;
|
||||
const {
|
||||
getPrefixCls
|
||||
} = React.useContext(ConfigContext);
|
||||
const inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
|
||||
const prefixCls = getPrefixCls('input-password', customizePrefixCls);
|
||||
const suffixIcon = visibilityToggle && getIcon(prefixCls);
|
||||
const inputClassName = clsx(prefixCls, className, {
|
||||
[`${prefixCls}-${size}`]: !!size
|
||||
});
|
||||
const omittedProps = {
|
||||
...omit(restProps, ['suffix', 'iconRender', 'visibilityToggle']),
|
||||
type: visible ? 'text' : 'password',
|
||||
className: inputClassName,
|
||||
prefixCls: inputPrefixCls,
|
||||
suffix: (/*#__PURE__*/React.createElement(React.Fragment, null, suffixIcon, suffix))
|
||||
};
|
||||
if (size) {
|
||||
omittedProps.size = size;
|
||||
}
|
||||
return /*#__PURE__*/React.createElement(Input, {
|
||||
ref: composeRef(ref, inputRef),
|
||||
...omittedProps
|
||||
});
|
||||
});
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
Password.displayName = 'Input.Password';
|
||||
}
|
||||
export default Password;
|
||||
Reference in New Issue
Block a user