forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPassword.jsx
108 lines (106 loc) · 2.69 KB
/
Password.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import classNames from 'classnames';
import { getComponentFromProp, getOptionProps, getListeners } from '../_util/props-util';
import Input from './Input';
import Icon from '../icon';
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',
mixins: [BaseMixin],
inheritAttrs: false,
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: {
focus() {
this.$refs.input.focus();
},
blur() {
this.$refs.input.blur();
},
onVisibleChange() {
if (this.disabled) {
return;
}
this.setState({
visible: !this.visible,
});
},
getIcon() {
const { prefixCls, action } = this.$props;
const iconTrigger = ActionMap[action] || '';
const iconProps = {
props: {
type: this.visible ? 'eye' : 'eye-invisible',
},
on: {
[iconTrigger]: this.onVisibleChange,
mousedown: e => {
// Prevent focused state lost
// https://github.com/ant-design/ant-design/issues/15173
e.preventDefault();
},
mouseup: e => {
// Prevent focused state lost
// https://github.com/ant-design/ant-design/pull/23633/files
e.preventDefault();
},
},
class: `${prefixCls}-icon`,
key: 'passwordIcon',
};
return <Icon {...iconProps} />;
},
},
render() {
const {
prefixCls,
inputPrefixCls,
size,
suffix,
visibilityToggle,
...restProps
} = getOptionProps(this);
const suffixIcon = visibilityToggle && this.getIcon();
const inputClassName = classNames(prefixCls, {
[`${prefixCls}-${size}`]: !!size,
});
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,
ref: 'input',
on: getListeners(this),
};
return <Input {...inputProps} />;
},
};