@@ -6,16 +6,26 @@ import EyeOutlined from '@ant-design/icons-vue/EyeOutlined';
6
6
import EyeInvisibleOutlined from '@ant-design/icons-vue/EyeInvisibleOutlined' ;
7
7
import type { InputProps } from './inputProps' ;
8
8
import inputProps from './inputProps' ;
9
- import { computed , defineComponent , shallowRef } from 'vue' ;
9
+ import type { PropType , Ref , ShallowRef } from 'vue' ;
10
+ import { computed , defineComponent , shallowRef , watch } from 'vue' ;
10
11
import useConfigInject from '../config-provider/hooks/useConfigInject' ;
11
12
import omit from '../_util/omit' ;
12
13
14
+ type VisibilityToggleProps =
15
+ | boolean
16
+ | {
17
+ visible : Ref < boolean > ;
18
+ onVisibleChange : ( visible : boolean ) => void ;
19
+ } ;
20
+
13
21
const ActionMap = {
14
22
click : 'onClick' ,
15
23
hover : 'onMouseover' ,
16
24
} ;
25
+
17
26
const defaultIconRender = ( visible : boolean ) =>
18
27
visible ? < EyeOutlined /> : < EyeInvisibleOutlined /> ;
28
+
19
29
export default defineComponent ( {
20
30
compatConfig : { MODE : 3 } ,
21
31
name : 'AInputPassword' ,
@@ -25,18 +35,38 @@ export default defineComponent({
25
35
prefixCls : String ,
26
36
inputPrefixCls : String ,
27
37
action : { type : String , default : 'click' } ,
28
- visibilityToggle : { type : Boolean , default : true } ,
38
+ visibilityToggle : {
39
+ type : [ Boolean , Object ] as PropType < VisibilityToggleProps > ,
40
+ default : true ,
41
+ } ,
29
42
iconRender : Function ,
30
43
} ,
31
44
setup ( props , { slots, attrs, expose } ) {
32
- const visible = shallowRef ( false ) ;
45
+ let visible : ShallowRef < boolean > = undefined ;
46
+
47
+ if ( typeof props . visibilityToggle === 'boolean' ) {
48
+ visible = shallowRef ( false ) ;
49
+ } else {
50
+ visible = shallowRef ( props . visibilityToggle . visible ) ;
51
+ // eslint-disable-next-line vue/no-setup-props-destructure
52
+ const { visibilityToggle } = props ;
53
+ function _onVisibleChange ( visible : boolean ) {
54
+ visibilityToggle . onVisibleChange ( visible ) ;
55
+ }
56
+
57
+ watch ( visible , ( ) => {
58
+ _onVisibleChange ( visible . value ) ;
59
+ } ) ;
60
+ }
61
+
33
62
const onVisibleChange = ( ) => {
34
63
const { disabled } = props ;
35
64
if ( disabled ) {
36
65
return ;
37
66
}
38
67
visible . value = ! visible . value ;
39
68
} ;
69
+
40
70
const inputRef = shallowRef ( ) ;
41
71
const focus = ( ) => {
42
72
inputRef . value ?. focus ( ) ;
@@ -74,7 +104,9 @@ export default defineComponent({
74
104
const renderPassword = ( ) => {
75
105
const { size, visibilityToggle, ...restProps } = props ;
76
106
77
- const suffixIcon = visibilityToggle && getIcon ( prefixCls . value ) ;
107
+ const suffixIcon =
108
+ ( typeof visibilityToggle === 'boolean' ? visibilityToggle : true ) &&
109
+ getIcon ( prefixCls . value ) ;
78
110
const inputClassName = classNames ( prefixCls . value , attrs . class , {
79
111
[ `${ prefixCls . value } -${ size } ` ] : ! ! size ,
80
112
} ) ;
0 commit comments