1
- import { defineComponent , inject , onBeforeMount , ref , ExtractPropTypes , computed } from 'vue' ;
1
+ import {
2
+ defineComponent ,
3
+ inject ,
4
+ onBeforeMount ,
5
+ ref ,
6
+ ExtractPropTypes ,
7
+ computed ,
8
+ onMounted ,
9
+ nextTick ,
10
+ } from 'vue' ;
2
11
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined' ;
3
12
import PropTypes from '../_util/vue-types' ;
4
- import VcSwitch from '../vc-switch ' ;
13
+ import KeyCode from '../_util/KeyCode ' ;
5
14
import Wave from '../_util/wave' ;
6
15
import { defaultConfigProvider } from '../config-provider' ;
7
16
import warning from '../_util/warning' ;
@@ -18,7 +27,7 @@ const switchProps = {
18
27
checkedChildren : PropTypes . any ,
19
28
unCheckedChildren : PropTypes . any ,
20
29
tabindex : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
21
- // defaultChecked: PropTypes.looseBool,
30
+ defaultChecked : PropTypes . looseBool ,
22
31
autofocus : PropTypes . looseBool ,
23
32
loading : PropTypes . looseBool ,
24
33
checked : PropTypes . looseBool ,
@@ -31,10 +40,24 @@ const Switch = defineComponent({
31
40
__ANT_SWITCH : true ,
32
41
inheritAttrs : false ,
33
42
props : switchProps ,
34
- setup ( props : SwitchProps , { attrs, slots, expose } ) {
43
+ emits : [ 'update:checked' , 'mouseup' , 'change' , 'click' , 'keydown' ] ,
44
+ setup ( props : SwitchProps , { attrs, slots, expose, emit } ) {
45
+ onBeforeMount ( ( ) => {
46
+ warning (
47
+ ! ( 'defaultChecked' in attrs ) ,
48
+ 'Switch' ,
49
+ `'defaultChecked' is deprecated, please use 'v-model:checked'` ,
50
+ ) ;
51
+ warning (
52
+ ! ( 'value' in attrs ) ,
53
+ 'Switch' ,
54
+ '`value` is not validate prop, do you mean `checked`?' ,
55
+ ) ;
56
+ } ) ;
57
+
35
58
const configProvider = inject ( 'configProvider' , defaultConfigProvider ) ;
59
+ const { getPrefixCls } = configProvider ;
36
60
const refSwitchNode = ref ( ) ;
37
-
38
61
const focus = ( ) => {
39
62
refSwitchNode . value ?. focus ( ) ;
40
63
} ;
@@ -44,42 +67,85 @@ const Switch = defineComponent({
44
67
45
68
expose ( { focus, blur } ) ;
46
69
47
- onBeforeMount ( ( ) => {
48
- if ( 'defaultChecked' in attrs ) {
49
- console . warn (
50
- `[antdv: Switch]: 'defaultChecked' will be obsolete, please use 'v-model:checked'` ,
51
- ) ;
52
- }
53
- warning (
54
- ! ( 'value' in attrs ) ,
55
- 'Switch' ,
56
- '`value` is not validate prop, do you mean `checked`?' ,
57
- ) ;
58
- } ) ;
59
- const { getPrefixCls } = configProvider ;
60
70
const prefixCls = computed ( ( ) => {
61
71
return getPrefixCls ( 'switch' , props . prefixCls ) ;
62
72
} ) ;
73
+ const checked = computed ( ( ) => {
74
+ return 'checked' in props ? ! ! props . checked : ! ! props . defaultChecked ;
75
+ } ) ;
76
+
77
+ onMounted ( ( ) => {
78
+ nextTick ( ( ) => {
79
+ if ( props . autofocus && ! props . disabled ) {
80
+ refSwitchNode . value . focus ( ) ;
81
+ }
82
+ } ) ;
83
+ } ) ;
84
+
85
+ const setChecked = ( check : boolean , e : MouseEvent | KeyboardEvent ) => {
86
+ if ( props . disabled ) {
87
+ return ;
88
+ }
89
+ emit ( 'update:checked' , check ) ;
90
+ emit ( 'change' , check , e ) ;
91
+ } ;
92
+
93
+ const handleClick = ( e : MouseEvent ) => {
94
+ focus ( ) ;
95
+ const newChecked = ! checked . value ;
96
+ setChecked ( newChecked , e ) ;
97
+ emit ( 'click' , newChecked , e ) ;
98
+ } ;
99
+
100
+ const handleKeyDown = ( e : KeyboardEvent ) => {
101
+ if ( e . keyCode === KeyCode . LEFT ) {
102
+ setChecked ( false , e ) ;
103
+ } else if ( e . keyCode === KeyCode . RIGHT ) {
104
+ setChecked ( true , e ) ;
105
+ }
106
+ emit ( 'keydown' , e ) ;
107
+ } ;
108
+
109
+ const handleMouseUp = ( e : MouseEvent ) => {
110
+ refSwitchNode . value ?. blur ( ) ;
111
+ emit ( 'mouseup' , e ) ;
112
+ } ;
63
113
return ( ) => (
64
114
< Wave insertExtraNode >
65
- < VcSwitch
66
- { ...Omit ( props , [ 'prefixCls' , 'size' , 'loading' , 'disabled' ] ) }
115
+ < button
116
+ { ...Omit ( props , [
117
+ 'prefixCls' ,
118
+ 'checkedChildren' ,
119
+ 'unCheckedChildren' ,
120
+ 'checked' ,
121
+ 'autofocus' ,
122
+ 'defaultChecked' ,
123
+ ] ) }
67
124
{ ...attrs }
68
- checked = { props . checked }
69
- prefixCls = { prefixCls . value }
70
- loadingIcon = {
71
- props . loading ? < LoadingOutlined class = { `${ prefixCls . value } -loading-icon` } /> : null
72
- }
73
- checkedChildren = { getPropsSlot ( slots , props , 'checkedChildren' ) }
74
- unCheckedChildren = { getPropsSlot ( slots , props , 'unCheckedChildren' ) }
125
+ onKeydown = { handleKeyDown }
126
+ onClick = { handleClick }
127
+ onMouseup = { handleMouseUp }
128
+ type = "button"
129
+ role = "switch"
130
+ aria-checked = { checked . value }
75
131
disabled = { props . disabled || props . loading }
76
132
class = { {
77
133
[ attrs . class as string ] : attrs . class ,
134
+ [ prefixCls . value ] : true ,
78
135
[ `${ prefixCls . value } -small` ] : props . size === 'small' ,
79
136
[ `${ prefixCls . value } -loading` ] : props . loading ,
137
+ [ `${ prefixCls . value } -checked` ] : checked . value ,
138
+ [ `${ prefixCls . value } -disabled` ] : props . disabled ,
80
139
} }
81
140
ref = { refSwitchNode }
82
- />
141
+ >
142
+ { props . loading ? < LoadingOutlined class = { `${ prefixCls . value } -loading-icon` } /> : null }
143
+ < span class = { `${ prefixCls . value } -inner` } >
144
+ { checked . value
145
+ ? getPropsSlot ( slots , props , 'checkedChildren' )
146
+ : getPropsSlot ( slots , props , 'unCheckedChildren' ) }
147
+ </ span >
148
+ </ button >
83
149
</ Wave >
84
150
) ;
85
151
} ,
0 commit comments