2024-01-08 11:05:03 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { useRef, useState } from 'react';
|
2022-06-21 10:24:52 +08:00
|
|
|
import EyeInvisibleOutlined from '@ant-design/icons/EyeInvisibleOutlined';
|
|
|
|
import EyeOutlined from '@ant-design/icons/EyeOutlined';
|
2018-11-29 10:03:16 +08:00
|
|
|
import classNames from 'classnames';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2022-10-13 15:07:43 +08:00
|
|
|
import { composeRef } from 'rc-util/lib/ref';
|
2024-01-08 11:05:03 +08:00
|
|
|
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
2022-12-25 18:34:42 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2023-05-06 15:49:37 +08:00
|
|
|
import useRemovePasswordTimeout from './hooks/useRemovePasswordTimeout';
|
2022-06-21 10:24:52 +08:00
|
|
|
import type { InputProps, InputRef } from './Input';
|
|
|
|
import Input from './Input';
|
2024-09-03 23:51:01 +08:00
|
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
2018-11-29 10:03:16 +08:00
|
|
|
|
2022-11-05 21:34:20 +08:00
|
|
|
const defaultIconRender = (visible: boolean): React.ReactNode =>
|
2022-09-30 15:58:34 +08:00
|
|
|
visible ? <EyeOutlined /> : <EyeInvisibleOutlined />;
|
|
|
|
|
2024-01-08 11:05:03 +08:00
|
|
|
interface VisibilityToggle {
|
2022-10-31 21:15:59 +08:00
|
|
|
visible?: boolean;
|
|
|
|
onVisibleChange?: (visible: boolean) => void;
|
2024-01-08 11:05:03 +08:00
|
|
|
}
|
2022-10-31 21:15:59 +08:00
|
|
|
|
2018-11-29 10:03:16 +08:00
|
|
|
export interface PasswordProps extends InputProps {
|
|
|
|
readonly inputPrefixCls?: string;
|
2024-01-08 11:05:03 +08:00
|
|
|
readonly action?: 'click' | 'hover';
|
2022-10-31 21:15:59 +08:00
|
|
|
visibilityToggle?: boolean | VisibilityToggle;
|
2020-04-30 21:19:16 +08:00
|
|
|
iconRender?: (visible: boolean) => React.ReactNode;
|
2018-11-29 10:03:16 +08:00
|
|
|
}
|
|
|
|
|
2024-01-08 11:05:03 +08:00
|
|
|
const actionMap: Record<PropertyKey, keyof React.DOMAttributes<HTMLSpanElement>> = {
|
2018-11-29 10:03:16 +08:00
|
|
|
click: 'onClick',
|
|
|
|
hover: 'onMouseOver',
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-11-29 10:03:16 +08:00
|
|
|
|
2024-01-08 11:05:03 +08:00
|
|
|
type IconPropsType = React.HTMLAttributes<HTMLSpanElement> & React.Attributes;
|
|
|
|
|
2022-03-01 14:17:48 +08:00
|
|
|
const Password = React.forwardRef<InputRef, PasswordProps>((props, ref) => {
|
2024-04-09 14:47:58 +08:00
|
|
|
const {
|
2024-09-03 23:51:01 +08:00
|
|
|
disabled: customDisabled,
|
2024-04-09 14:47:58 +08:00
|
|
|
action = 'click',
|
|
|
|
visibilityToggle = true,
|
|
|
|
iconRender = defaultIconRender,
|
|
|
|
} = props;
|
|
|
|
|
2024-09-03 23:51:01 +08:00
|
|
|
// ===================== Disabled =====================
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
|
|
|
const mergedDisabled = customDisabled ?? disabled;
|
|
|
|
|
2022-10-31 21:15:59 +08:00
|
|
|
const visibilityControlled =
|
|
|
|
typeof visibilityToggle === 'object' && visibilityToggle.visible !== undefined;
|
|
|
|
const [visible, setVisible] = useState(() =>
|
|
|
|
visibilityControlled ? visibilityToggle.visible! : false,
|
|
|
|
);
|
2022-10-13 15:07:43 +08:00
|
|
|
const inputRef = useRef<InputRef>(null);
|
|
|
|
|
2022-10-31 21:15:59 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (visibilityControlled) {
|
|
|
|
setVisible(visibilityToggle.visible!);
|
|
|
|
}
|
|
|
|
}, [visibilityControlled, visibilityToggle]);
|
|
|
|
|
2022-10-13 15:07:43 +08:00
|
|
|
// Remove Password value
|
|
|
|
const removePasswordTimeout = useRemovePasswordTimeout(inputRef);
|
2019-08-24 00:25:45 +08:00
|
|
|
|
2020-06-16 19:49:08 +08:00
|
|
|
const onVisibleChange = () => {
|
2024-09-03 23:51:01 +08:00
|
|
|
if (mergedDisabled) {
|
2019-09-19 17:19:22 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-10-13 15:07:43 +08:00
|
|
|
if (visible) {
|
|
|
|
removePasswordTimeout();
|
|
|
|
}
|
2022-11-19 13:47:33 +08:00
|
|
|
setVisible((prevState) => {
|
2022-10-31 21:15:59 +08:00
|
|
|
const newState = !prevState;
|
|
|
|
if (typeof visibilityToggle === 'object') {
|
|
|
|
visibilityToggle.onVisibleChange?.(newState);
|
|
|
|
}
|
|
|
|
return newState;
|
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-11-29 10:03:16 +08:00
|
|
|
|
2020-06-16 19:49:08 +08:00
|
|
|
const getIcon = (prefixCls: string) => {
|
2024-01-08 11:05:03 +08:00
|
|
|
const iconTrigger = actionMap[action] || '';
|
2020-04-30 21:19:16 +08:00
|
|
|
const icon = iconRender(visible);
|
2024-01-08 11:05:03 +08:00
|
|
|
const iconProps: IconPropsType = {
|
2020-06-16 19:49:08 +08:00
|
|
|
[iconTrigger]: onVisibleChange,
|
2019-03-04 21:54:10 +08:00
|
|
|
className: `${prefixCls}-icon`,
|
|
|
|
key: 'passwordIcon',
|
2024-01-08 11:05:03 +08:00
|
|
|
onMouseDown: (e: React.MouseEvent<HTMLSpanElement, MouseEvent>) => {
|
2019-03-04 21:54:51 +08:00
|
|
|
// Prevent focused state lost
|
|
|
|
// https://github.com/ant-design/ant-design/issues/15173
|
|
|
|
e.preventDefault();
|
|
|
|
},
|
2024-01-08 11:05:03 +08:00
|
|
|
onMouseUp: (e: React.MouseEvent<HTMLSpanElement, MouseEvent>) => {
|
2020-04-27 18:02:51 +08:00
|
|
|
// Prevent caret position change
|
|
|
|
// https://github.com/ant-design/ant-design/issues/23524
|
|
|
|
e.preventDefault();
|
|
|
|
},
|
2019-03-04 21:54:10 +08:00
|
|
|
};
|
2024-01-08 11:05:03 +08:00
|
|
|
return React.cloneElement<IconPropsType>(
|
|
|
|
React.isValidElement<IconPropsType>(icon) ? icon : <span>{icon}</span>,
|
|
|
|
iconProps,
|
|
|
|
);
|
2020-03-07 15:11:44 +08:00
|
|
|
};
|
2018-11-29 10:03:16 +08:00
|
|
|
|
2022-12-25 18:34:42 +08:00
|
|
|
const {
|
|
|
|
className,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
inputPrefixCls: customizeInputPrefixCls,
|
|
|
|
size,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const { getPrefixCls } = React.useContext<ConfigConsumerProps>(ConfigContext);
|
|
|
|
const inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
|
|
|
|
const prefixCls = getPrefixCls('input-password', customizePrefixCls);
|
|
|
|
|
|
|
|
const suffixIcon = visibilityToggle && getIcon(prefixCls);
|
|
|
|
|
|
|
|
const inputClassName = classNames(prefixCls, className, {
|
|
|
|
[`${prefixCls}-${size}`]: !!size,
|
|
|
|
});
|
|
|
|
|
|
|
|
const omittedProps: InputProps = {
|
|
|
|
...omit(restProps, ['suffix', 'iconRender', 'visibilityToggle']),
|
|
|
|
type: visible ? 'text' : 'password',
|
|
|
|
className: inputClassName,
|
|
|
|
prefixCls: inputPrefixCls,
|
|
|
|
suffix: suffixIcon,
|
2020-03-07 15:11:44 +08:00
|
|
|
};
|
|
|
|
|
2022-12-25 18:34:42 +08:00
|
|
|
if (size) {
|
|
|
|
omittedProps.size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <Input ref={composeRef(ref, inputRef)} {...omittedProps} />;
|
2020-06-16 19:49:08 +08:00
|
|
|
});
|
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2024-01-08 11:05:03 +08:00
|
|
|
Password.displayName = 'Input.Password';
|
2022-06-21 10:24:52 +08:00
|
|
|
}
|
2020-06-16 19:49:08 +08:00
|
|
|
|
|
|
|
export default Password;
|