@@ -31,6 +31,8 @@ const switchProps = {
31
31
autofocus : PropTypes . looseBool ,
32
32
loading : PropTypes . looseBool ,
33
33
checked : PropTypes . looseBool ,
34
+ trueValue : PropTypes . any . def ( true ) ,
35
+ falseValue : PropTypes . any . def ( false ) ,
34
36
onChange : PropTypes . func ,
35
37
onClick : PropTypes . func ,
36
38
onKeydown : PropTypes . func ,
@@ -59,12 +61,15 @@ const Switch = defineComponent({
59
61
'`value` is not validate prop, do you mean `checked`?' ,
60
62
) ;
61
63
} ) ;
62
- const checked = ref ( props . checked !== undefined ? ! ! props . checked : ! ! attrs . defaultChecked ) ;
64
+ const checked = ref ( props . checked !== undefined ? props . checked : attrs . defaultChecked ) ;
65
+ const checkedStatus = computed ( ( ) => {
66
+ return checked . value === props . trueValue ;
67
+ } ) ;
63
68
64
69
watch (
65
70
( ) => props . checked ,
66
71
( ) => {
67
- checked . value = ! ! props . checked ;
72
+ checked . value = props . checked ;
68
73
} ,
69
74
) ;
70
75
@@ -92,29 +97,26 @@ const Switch = defineComponent({
92
97
} ) ;
93
98
} ) ;
94
99
95
- const setChecked = ( check : boolean , e : MouseEvent | KeyboardEvent ) => {
100
+ const setChecked = ( check : any , e : MouseEvent | KeyboardEvent ) => {
96
101
if ( props . disabled ) {
97
102
return ;
98
103
}
99
- if ( props . checked === undefined ) {
100
- checked . value = check ;
101
- }
102
104
emit ( 'update:checked' , check ) ;
103
105
emit ( 'change' , check , e ) ;
104
106
} ;
105
107
106
108
const handleClick = ( e : MouseEvent ) => {
107
109
focus ( ) ;
108
- const newChecked = ! checked . value ;
110
+ const newChecked = checkedStatus . value ? props . falseValue : props . trueValue ;
109
111
setChecked ( newChecked , e ) ;
110
112
emit ( 'click' , newChecked , e ) ;
111
113
} ;
112
114
113
115
const handleKeyDown = ( e : KeyboardEvent ) => {
114
116
if ( e . keyCode === KeyCode . LEFT ) {
115
- setChecked ( false , e ) ;
117
+ setChecked ( props . falseValue , e ) ;
116
118
} else if ( e . keyCode === KeyCode . RIGHT ) {
117
- setChecked ( true , e ) ;
119
+ setChecked ( props . trueValue , e ) ;
118
120
}
119
121
emit ( 'keydown' , e ) ;
120
122
} ;
@@ -133,6 +135,8 @@ const Switch = defineComponent({
133
135
'checked' ,
134
136
'autofocus' ,
135
137
'defaultChecked' ,
138
+ 'trueValue' ,
139
+ 'falseValue' ,
136
140
] ) }
137
141
{ ...attrs }
138
142
onKeydown = { handleKeyDown }
@@ -147,7 +151,7 @@ const Switch = defineComponent({
147
151
[ prefixCls . value ] : true ,
148
152
[ `${ prefixCls . value } -small` ] : props . size === 'small' ,
149
153
[ `${ prefixCls . value } -loading` ] : props . loading ,
150
- [ `${ prefixCls . value } -checked` ] : checked . value ,
154
+ [ `${ prefixCls . value } -checked` ] : checkedStatus . value ,
151
155
[ `${ prefixCls . value } -disabled` ] : props . disabled ,
152
156
} }
153
157
ref = { refSwitchNode }
0 commit comments