2018-11-29 10:03:16 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
2019-08-05 18:38:10 +08:00
|
|
|
import omit from 'omit.js';
|
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-03-07 15:11:44 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2018-11-29 10:03:16 +08:00
|
|
|
import Input, { InputProps } from './Input';
|
|
|
|
|
|
|
|
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;
|
2018-11-29 10:03:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface PasswordState {
|
2018-12-07 20:02:01 +08:00
|
|
|
visible: boolean;
|
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
|
|
|
|
2018-12-02 18:11:19 +08:00
|
|
|
export default class Password extends React.Component<PasswordProps, PasswordState> {
|
2019-08-24 00:25:45 +08:00
|
|
|
input: HTMLInputElement;
|
|
|
|
|
2018-11-29 10:03:16 +08:00
|
|
|
static defaultProps = {
|
|
|
|
action: 'click',
|
2018-12-05 18:13:47 +08:00
|
|
|
visibilityToggle: true,
|
2018-11-29 10:03:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
state: PasswordState = {
|
|
|
|
visible: false,
|
|
|
|
};
|
|
|
|
|
2019-12-31 14:41:09 +08:00
|
|
|
onVisibleChange = () => {
|
2019-09-19 17:19:22 +08:00
|
|
|
const { disabled } = this.props;
|
|
|
|
if (disabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
this.setState(({ visible }) => ({ visible: !visible }));
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-11-29 10:03:16 +08:00
|
|
|
|
2020-03-07 15:11:44 +08:00
|
|
|
getIcon = (prefixCls: string) => {
|
|
|
|
const { action } = this.props;
|
2019-03-24 16:21:48 +08:00
|
|
|
const iconTrigger = ActionMap[action!] || '';
|
2019-11-28 12:34:33 +08:00
|
|
|
const icon = this.state.visible ? EyeOutlined : EyeInvisibleOutlined;
|
2019-03-04 21:54:10 +08:00
|
|
|
const iconProps = {
|
2019-12-31 14:41:09 +08:00
|
|
|
[iconTrigger]: this.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-10 18:47:08 +08:00
|
|
|
return React.createElement(icon as React.ComponentType, iconProps);
|
2020-03-07 15:11:44 +08:00
|
|
|
};
|
2018-11-29 10:03:16 +08:00
|
|
|
|
2019-08-24 00:25:45 +08:00
|
|
|
saveInput = (instance: Input) => {
|
2019-08-26 15:33:42 +08:00
|
|
|
if (instance && instance.input) {
|
|
|
|
this.input = instance.input;
|
|
|
|
}
|
2019-08-24 00:25:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
focus() {
|
|
|
|
this.input.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
this.input.blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
select() {
|
|
|
|
this.input.select();
|
|
|
|
}
|
|
|
|
|
2020-03-07 15:11:44 +08:00
|
|
|
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
|
|
|
|
} = this.props;
|
2020-03-07 15:11:44 +08:00
|
|
|
|
|
|
|
const inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
|
|
|
|
const prefixCls = getPrefixCls('input-password', customizePrefixCls);
|
|
|
|
|
|
|
|
const suffixIcon = visibilityToggle && this.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-02-09 11:57:42 +08:00
|
|
|
const props = {
|
|
|
|
...omit(restProps, ['suffix']),
|
|
|
|
type: this.state.visible ? 'text' : 'password',
|
|
|
|
className: inputClassName,
|
|
|
|
prefixCls: inputPrefixCls,
|
|
|
|
suffix: suffixIcon,
|
|
|
|
ref: this.saveInput,
|
|
|
|
};
|
2020-03-07 15:11:44 +08:00
|
|
|
|
2020-02-09 11:57:42 +08:00
|
|
|
if (size) {
|
|
|
|
props.size = size;
|
|
|
|
}
|
2020-03-07 15:11:44 +08:00
|
|
|
|
2020-02-09 11:57:42 +08:00
|
|
|
return <Input {...props} />;
|
2020-03-07 15:11:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <ConfigConsumer>{this.renderPassword}</ConfigConsumer>;
|
2018-11-29 10:03:16 +08:00
|
|
|
}
|
|
|
|
}
|