2018-11-29 10:03:16 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import Input, { InputProps } from './Input';
|
|
|
|
import Icon from '../icon';
|
|
|
|
|
|
|
|
export interface PasswordProps extends InputProps {
|
|
|
|
readonly inputPrefixCls?: string;
|
|
|
|
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> {
|
2018-11-29 10:03:16 +08:00
|
|
|
static defaultProps = {
|
|
|
|
inputPrefixCls: 'ant-input',
|
|
|
|
prefixCls: 'ant-input-password',
|
|
|
|
action: 'click',
|
2018-12-05 18:13:47 +08:00
|
|
|
visibilityToggle: true,
|
2018-11-29 10:03:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
state: PasswordState = {
|
|
|
|
visible: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
onChange = () => {
|
|
|
|
this.setState({
|
|
|
|
visible: !this.state.visible,
|
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-11-29 10:03:16 +08:00
|
|
|
|
|
|
|
getIcon() {
|
|
|
|
const { prefixCls, action } = this.props;
|
|
|
|
const iconTrigger = ActionMap[action] || '';
|
2018-12-05 16:14:04 +08:00
|
|
|
const iconProps = { [iconTrigger]: this.onChange };
|
2018-11-29 10:03:16 +08:00
|
|
|
return React.cloneElement(
|
|
|
|
<Icon
|
|
|
|
{...iconProps}
|
|
|
|
className={`${prefixCls}-icon`}
|
2018-12-03 17:03:31 +08:00
|
|
|
type={this.state.visible ? 'eye-invisible' : 'eye'}
|
2018-11-29 10:03:16 +08:00
|
|
|
key="passwordIcon"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-12-05 18:13:47 +08:00
|
|
|
const {
|
|
|
|
className,
|
|
|
|
prefixCls,
|
|
|
|
inputPrefixCls,
|
|
|
|
size,
|
|
|
|
suffix,
|
|
|
|
visibilityToggle,
|
|
|
|
...restProps
|
|
|
|
} = this.props;
|
|
|
|
const suffixIcon = visibilityToggle && this.getIcon();
|
2018-11-29 10:03:16 +08:00
|
|
|
const inputClassName = classNames(prefixCls, className, {
|
|
|
|
[`${prefixCls}-${size}`]: !!size,
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<Input
|
|
|
|
{...restProps}
|
|
|
|
type={this.state.visible ? 'text' : 'password'}
|
|
|
|
size={size}
|
|
|
|
className={inputClassName}
|
|
|
|
prefixCls={inputPrefixCls}
|
|
|
|
suffix={suffixIcon}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|