1
- import type { ExtractPropTypes } from 'vue' ;
1
+ import type { ExtractPropTypes , PropType } from 'vue' ;
2
2
import {
3
3
defineComponent ,
4
4
inject ,
@@ -19,23 +19,35 @@ import { tuple, withInstall } from '../_util/type';
19
19
import { getPropsSlot } from '../_util/props-util' ;
20
20
import Omit from 'omit.js' ;
21
21
22
- export const SwitchSizes = tuple ( 'small' , 'default' , 'large' ) ;
22
+ export const SwitchSizes = tuple ( 'small' , 'default' ) ;
23
23
24
24
const switchProps = {
25
25
prefixCls : PropTypes . string ,
26
26
size : PropTypes . oneOf ( SwitchSizes ) ,
27
27
disabled : PropTypes . looseBool ,
28
- checkedChildren : PropTypes . any ,
29
- unCheckedChildren : PropTypes . any ,
28
+ checkedChildren : PropTypes . VNodeChild ,
29
+ unCheckedChildren : PropTypes . VNodeChild ,
30
30
tabindex : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
31
31
autofocus : PropTypes . looseBool ,
32
32
loading : PropTypes . looseBool ,
33
- checked : PropTypes . looseBool ,
34
- onChange : PropTypes . func ,
35
- onClick : PropTypes . func ,
36
- onKeydown : PropTypes . func ,
37
- onMouseup : PropTypes . func ,
38
- 'onUpdate:checked' : PropTypes . func ,
33
+ checked : PropTypes . any ,
34
+ checkedValue : PropTypes . any . def ( true ) ,
35
+ uncheckedValue : PropTypes . any . def ( false ) ,
36
+ onChange : {
37
+ type : Function as PropType < ( checked : any , e : Event ) => void > ,
38
+ } ,
39
+ onClick : {
40
+ type : Function as PropType < ( checked : any , e : Event ) => void > ,
41
+ } ,
42
+ onKeydown : {
43
+ type : Function as PropType < ( e : Event ) => void > ,
44
+ } ,
45
+ onMouseup : {
46
+ type : Function as PropType < ( e : Event ) => void > ,
47
+ } ,
48
+ 'onUpdate:checked' : {
49
+ type : Function as PropType < ( checked : any ) => void > ,
50
+ } ,
39
51
} ;
40
52
41
53
export type SwitchProps = Partial < ExtractPropTypes < typeof switchProps > > ;
@@ -46,7 +58,7 @@ const Switch = defineComponent({
46
58
inheritAttrs : false ,
47
59
props : switchProps ,
48
60
emits : [ 'update:checked' , 'mouseup' , 'change' , 'click' , 'keydown' ] ,
49
- setup ( props : SwitchProps , { attrs, slots, expose, emit } ) {
61
+ setup ( props , { attrs, slots, expose, emit } ) {
50
62
onBeforeMount ( ( ) => {
51
63
warning (
52
64
! ( 'defaultChecked' in attrs ) ,
@@ -59,12 +71,13 @@ const Switch = defineComponent({
59
71
'`value` is not validate prop, do you mean `checked`?' ,
60
72
) ;
61
73
} ) ;
62
- const checked = ref ( props . checked !== undefined ? ! ! props . checked : ! ! attrs . defaultChecked ) ;
74
+ const checked = ref ( props . checked !== undefined ? props . checked : attrs . defaultChecked ) ;
75
+ const checkedStatus = computed ( ( ) => checked . value === props . checkedValue ) ;
63
76
64
77
watch (
65
78
( ) => props . checked ,
66
79
( ) => {
67
- checked . value = ! ! props . checked ;
80
+ checked . value = props . checked ;
68
81
} ,
69
82
) ;
70
83
@@ -92,29 +105,26 @@ const Switch = defineComponent({
92
105
} ) ;
93
106
} ) ;
94
107
95
- const setChecked = ( check : boolean , e : MouseEvent | KeyboardEvent ) => {
108
+ const setChecked = ( check : any , e : MouseEvent | KeyboardEvent ) => {
96
109
if ( props . disabled ) {
97
110
return ;
98
111
}
99
- if ( props . checked === undefined ) {
100
- checked . value = check ;
101
- }
102
112
emit ( 'update:checked' , check ) ;
103
113
emit ( 'change' , check , e ) ;
104
114
} ;
105
115
106
116
const handleClick = ( e : MouseEvent ) => {
107
117
focus ( ) ;
108
- const newChecked = ! checked . value ;
118
+ const newChecked = checkedStatus . value ? props . uncheckedValue : props . checkedValue ;
109
119
setChecked ( newChecked , e ) ;
110
120
emit ( 'click' , newChecked , e ) ;
111
121
} ;
112
122
113
123
const handleKeyDown = ( e : KeyboardEvent ) => {
114
124
if ( e . keyCode === KeyCode . LEFT ) {
115
- setChecked ( false , e ) ;
125
+ setChecked ( props . uncheckedValue , e ) ;
116
126
} else if ( e . keyCode === KeyCode . RIGHT ) {
117
- setChecked ( true , e ) ;
127
+ setChecked ( props . checkedValue , e ) ;
118
128
}
119
129
emit ( 'keydown' , e ) ;
120
130
} ;
@@ -123,6 +133,13 @@ const Switch = defineComponent({
123
133
refSwitchNode . value ?. blur ( ) ;
124
134
emit ( 'mouseup' , e ) ;
125
135
} ;
136
+
137
+ const classNames = computed ( ( ) => ( {
138
+ [ `${ prefixCls . value } -small` ] : props . size === 'small' ,
139
+ [ `${ prefixCls . value } -loading` ] : props . loading ,
140
+ [ `${ prefixCls . value } -checked` ] : checkedStatus . value ,
141
+ [ `${ prefixCls . value } -disabled` ] : props . disabled ,
142
+ } ) ) ;
126
143
return ( ) => (
127
144
< Wave insertExtraNode >
128
145
< button
@@ -133,6 +150,8 @@ const Switch = defineComponent({
133
150
'checked' ,
134
151
'autofocus' ,
135
152
'defaultChecked' ,
153
+ 'checkedValue' ,
154
+ 'uncheckedValue' ,
136
155
] ) }
137
156
{ ...attrs }
138
157
onKeydown = { handleKeyDown }
@@ -142,14 +161,13 @@ const Switch = defineComponent({
142
161
role = "switch"
143
162
aria-checked = { checked . value }
144
163
disabled = { props . disabled || props . loading }
145
- class = { {
146
- [ attrs . class as string ] : attrs . class ,
147
- [ prefixCls . value ] : true ,
148
- [ `${ prefixCls . value } -small` ] : props . size === 'small' ,
149
- [ `${ prefixCls . value } -loading` ] : props . loading ,
150
- [ `${ prefixCls . value } -checked` ] : checked . value ,
151
- [ `${ prefixCls . value } -disabled` ] : props . disabled ,
152
- } }
164
+ class = { [
165
+ {
166
+ [ attrs . class as string ] : ! ! attrs . class ,
167
+ [ prefixCls . value ] : true ,
168
+ } ,
169
+ classNames . value ,
170
+ ] }
153
171
ref = { refSwitchNode }
154
172
>
155
173
{ props . loading ? < LoadingOutlined class = { `${ prefixCls . value } -loading-icon` } /> : null }
0 commit comments