2018-11-29 10:03:16 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2020-03-02 12:09:38 +08:00
|
|
|
import EyeOutlined from '@ant-design/icons/EyeOutlined';
|
|
|
|
import EyeInvisibleOutlined from '@ant-design/icons/EyeInvisibleOutlined';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2020-06-16 19:49:08 +08:00
|
|
|
import { useState } from 'react';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { InputRef, InputProps } from './Input';
|
|
|
|
import Input from './Input';
|
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
|
|
|
import { ConfigConsumer } from '../config-provider';
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-16 19:49:08 +08:00
|
|
|
setVisible(!visible);
|
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) => {
|
|
|
|
const { action, iconRender = () => null } = props;
|
2019-03-24 16:21:48 +08:00
|
|
|
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,
|
|
|
|
visibilityToggle,
|
|
|
|
...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
|
|
|
|
2020-06-16 19:49:08 +08:00
|
|
|
const omittedProps = {
|
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,
|
2021-01-13 21:00:30 +08:00
|
|
|
} as InputProps;
|
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>;
|
|
|
|
});
|
|
|
|
|
|
|
|
Password.defaultProps = {
|
|
|
|
action: 'click',
|
|
|
|
visibilityToggle: true,
|
|
|
|
iconRender: (visible: boolean) => (visible ? <EyeOutlined /> : <EyeInvisibleOutlined />),
|
|
|
|
};
|
|
|
|
|
|
|
|
Password.displayName = 'Password';
|
|
|
|
|
|
|
|
export default Password;
|