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-06-21 10:24:52 +08:00
|
|
|
import * as React from 'react';
|
2020-06-16 19:49:08 +08:00
|
|
|
import { useState } from 'react';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
|
|
|
import { ConfigConsumer } from '../config-provider';
|
2022-06-21 10:24:52 +08:00
|
|
|
import type { InputProps, InputRef } from './Input';
|
|
|
|
import Input from './Input';
|
2018-11-29 10:03:16 +08:00
|
|
|
|
2022-09-30 15:58:34 +08:00
|
|
|
const defaultIconRender = (visible: boolean) =>
|
|
|
|
visible ? <EyeOutlined /> : <EyeInvisibleOutlined />;
|
|
|
|
|
2018-11-29 10:03:16 +08:00
|
|
|
export interface PasswordProps extends InputProps {
|
|
|
|
readonly inputPrefixCls?: string;
|
2019-03-24 16:21:48 +08:00
|
|
|
readonly action?: string;
|
2018-12-07 20:02:01 +08:00
|
|
|
visibilityToggle?: boolean;
|
2020-04-30 21:19:16 +08:00
|
|
|
iconRender?: (visible: boolean) => React.ReactNode;
|
2018-11-29 10:03:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ActionMap: Record<string, string> = {
|
|
|
|
click: 'onClick',
|
|
|
|
hover: 'onMouseOver',
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-11-29 10:03:16 +08:00
|
|
|
|
2022-03-01 14:17:48 +08:00
|
|
|
const Password = React.forwardRef<InputRef, PasswordProps>((props, ref) => {
|
2020-06-16 19:49:08 +08:00
|
|
|
const [visible, setVisible] = useState(false);
|
2019-08-24 00:25:45 +08:00
|
|
|
|
2020-06-16 19:49:08 +08:00
|
|
|
const onVisibleChange = () => {
|
|
|
|
const { disabled } = props;
|
2019-09-19 17:19:22 +08:00
|
|
|
if (disabled) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-30 15:58:34 +08:00
|
|
|
setVisible(prevState => !prevState);
|
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) => {
|
2022-09-30 15:58:34 +08:00
|
|
|
const { action = 'click', iconRender = defaultIconRender } = props;
|
|
|
|
const iconTrigger = ActionMap[action] || '';
|
2020-04-30 21:19:16 +08:00
|
|
|
const icon = iconRender(visible);
|
2019-03-04 21:54:10 +08:00
|
|
|
const iconProps = {
|
2020-06-16 19:49:08 +08:00
|
|
|
[iconTrigger]: onVisibleChange,
|
2019-03-04 21:54:10 +08:00
|
|
|
className: `${prefixCls}-icon`,
|
|
|
|
key: 'passwordIcon',
|
2019-03-04 21:54:51 +08:00
|
|
|
onMouseDown: (e: MouseEvent) => {
|
|
|
|
// Prevent focused state lost
|
|
|
|
// https://github.com/ant-design/ant-design/issues/15173
|
|
|
|
e.preventDefault();
|
|
|
|
},
|
2020-04-27 18:02:51 +08:00
|
|
|
onMouseUp: (e: MouseEvent) => {
|
|
|
|
// Prevent caret position change
|
|
|
|
// https://github.com/ant-design/ant-design/issues/23524
|
|
|
|
e.preventDefault();
|
|
|
|
},
|
2019-03-04 21:54:10 +08:00
|
|
|
};
|
2020-04-30 21:19:16 +08:00
|
|
|
return React.cloneElement(React.isValidElement(icon) ? icon : <span>{icon}</span>, iconProps);
|
2020-03-07 15:11:44 +08:00
|
|
|
};
|
2018-11-29 10:03:16 +08:00
|
|
|
|
2020-06-16 19:49:08 +08:00
|
|
|
const renderPassword = ({ getPrefixCls }: ConfigConsumerProps) => {
|
2018-12-05 18:13:47 +08:00
|
|
|
const {
|
|
|
|
className,
|
2020-03-07 15:11:44 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
inputPrefixCls: customizeInputPrefixCls,
|
2018-12-05 18:13:47 +08:00
|
|
|
size,
|
2022-09-30 15:58:34 +08:00
|
|
|
visibilityToggle = true,
|
2018-12-05 18:13:47 +08:00
|
|
|
...restProps
|
2020-06-16 19:49:08 +08:00
|
|
|
} = props;
|
2020-03-07 15:11:44 +08:00
|
|
|
|
|
|
|
const inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
|
|
|
|
const prefixCls = getPrefixCls('input-password', customizePrefixCls);
|
|
|
|
|
2020-06-16 19:49:08 +08:00
|
|
|
const suffixIcon = visibilityToggle && getIcon(prefixCls);
|
2018-11-29 10:03:16 +08:00
|
|
|
const inputClassName = classNames(prefixCls, className, {
|
|
|
|
[`${prefixCls}-${size}`]: !!size,
|
|
|
|
});
|
2020-03-07 15:11:44 +08:00
|
|
|
|
2022-07-26 11:25:45 +08:00
|
|
|
const omittedProps: InputProps = {
|
2020-04-30 21:19:16 +08:00
|
|
|
...omit(restProps, ['suffix', 'iconRender']),
|
2020-06-16 19:49:08 +08:00
|
|
|
type: visible ? 'text' : 'password',
|
2020-02-09 11:57:42 +08:00
|
|
|
className: inputClassName,
|
|
|
|
prefixCls: inputPrefixCls,
|
|
|
|
suffix: suffixIcon,
|
2022-07-26 11:25:45 +08:00
|
|
|
};
|
2020-03-07 15:11:44 +08:00
|
|
|
|
2020-02-09 11:57:42 +08:00
|
|
|
if (size) {
|
2020-06-16 19:49:08 +08:00
|
|
|
omittedProps.size = size;
|
2020-02-09 11:57:42 +08:00
|
|
|
}
|
2020-03-07 15:11:44 +08:00
|
|
|
|
2020-06-16 19:49:08 +08:00
|
|
|
return <Input ref={ref} {...omittedProps} />;
|
2020-03-07 15:11:44 +08:00
|
|
|
};
|
|
|
|
|
2020-06-16 19:49:08 +08:00
|
|
|
return <ConfigConsumer>{renderPassword}</ConfigConsumer>;
|
|
|
|
});
|
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Password.displayName = 'Password';
|
|
|
|
}
|
2020-06-16 19:49:08 +08:00
|
|
|
|
|
|
|
export default Password;
|