2019-04-07 09:19:18 +00:00
|
|
|
import classNames from 'classnames';
|
2020-01-19 08:58:38 +00:00
|
|
|
import { getComponentFromProp, getOptionProps, getListeners } from '../_util/props-util';
|
2019-04-07 09:19:18 +00:00
|
|
|
import Input from './Input';
|
2020-03-26 03:53:51 +00:00
|
|
|
import EyeOutlined from '@ant-design/icons-vue/EyeOutlined';
|
|
|
|
import EyeInvisibleOutlined from '@ant-design/icons-vue/EyeInvisibleOutlined';
|
2019-04-07 09:19:18 +00:00
|
|
|
import inputProps from './inputProps';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
|
|
|
|
const ActionMap = {
|
|
|
|
click: 'click',
|
|
|
|
hover: 'mouseover',
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'AInputPassword',
|
2019-10-17 10:49:33 +00:00
|
|
|
mixins: [BaseMixin],
|
2020-01-18 09:06:09 +00:00
|
|
|
inheritAttrs: false,
|
2019-04-07 09:19:18 +00:00
|
|
|
model: {
|
|
|
|
prop: 'value',
|
|
|
|
event: 'change.value',
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
...inputProps,
|
|
|
|
prefixCls: PropTypes.string.def('ant-input-password'),
|
|
|
|
inputPrefixCls: PropTypes.string.def('ant-input'),
|
|
|
|
action: PropTypes.string.def('click'),
|
|
|
|
visibilityToggle: PropTypes.bool.def(true),
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
visible: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
methods: {
|
2019-12-09 14:44:52 +00:00
|
|
|
focus() {
|
|
|
|
this.$refs.input.focus();
|
|
|
|
},
|
|
|
|
blur() {
|
|
|
|
this.$refs.input.blur();
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
onVisibleChange() {
|
|
|
|
if (this.disabled) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-07 09:19:18 +00:00
|
|
|
this.setState({
|
|
|
|
visible: !this.visible,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getIcon() {
|
|
|
|
const { prefixCls, action } = this.$props;
|
|
|
|
const iconTrigger = ActionMap[action] || '';
|
|
|
|
const iconProps = {
|
|
|
|
on: {
|
2020-03-07 11:45:13 +00:00
|
|
|
[iconTrigger]: this.onVisibleChange,
|
2019-09-11 10:11:48 +00:00
|
|
|
mousedown: e => {
|
2019-04-07 09:19:18 +00:00
|
|
|
// Prevent focused state lost
|
|
|
|
// https://github.com/ant-design/ant-design/issues/15173
|
|
|
|
e.preventDefault();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
class: `${prefixCls}-icon`,
|
|
|
|
key: 'passwordIcon',
|
|
|
|
};
|
2020-03-26 03:53:51 +00:00
|
|
|
return this.visible ? (
|
|
|
|
<EyeOutlined {...iconProps} />
|
|
|
|
) : (
|
|
|
|
<EyeInvisibleOutlined {...iconProps} />
|
|
|
|
);
|
2019-04-07 09:19:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
render() {
|
2019-10-17 10:49:33 +00:00
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
inputPrefixCls,
|
|
|
|
size,
|
|
|
|
suffix,
|
|
|
|
visibilityToggle,
|
|
|
|
...restProps
|
|
|
|
} = getOptionProps(this);
|
2019-04-07 09:19:18 +00:00
|
|
|
const suffixIcon = visibilityToggle && this.getIcon();
|
|
|
|
const inputClassName = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-${size}`]: !!size,
|
|
|
|
});
|
2019-10-17 09:59:23 +00:00
|
|
|
const inputProps = {
|
|
|
|
props: {
|
|
|
|
...restProps,
|
|
|
|
prefixCls: inputPrefixCls,
|
|
|
|
size,
|
|
|
|
suffix: suffixIcon,
|
|
|
|
prefix: getComponentFromProp(this, 'prefix'),
|
|
|
|
addonAfter: getComponentFromProp(this, 'addonAfter'),
|
|
|
|
addonBefore: getComponentFromProp(this, 'addonBefore'),
|
|
|
|
},
|
|
|
|
attrs: {
|
|
|
|
...this.$attrs,
|
|
|
|
type: this.visible ? 'text' : 'password',
|
|
|
|
},
|
|
|
|
class: inputClassName,
|
2019-12-09 14:44:52 +00:00
|
|
|
ref: 'input',
|
2020-01-19 08:58:38 +00:00
|
|
|
on: getListeners(this),
|
2019-10-17 09:59:23 +00:00
|
|
|
};
|
2019-10-17 10:49:33 +00:00
|
|
|
return <Input {...inputProps} />;
|
2019-04-07 09:19:18 +00:00
|
|
|
},
|
|
|
|
};
|