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' ;
211import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined' ;
312import PropTypes from '../_util/vue-types' ;
4- import VcSwitch from '../vc-switch ' ;
13+ import KeyCode from '../_util/KeyCode ' ;
514import Wave from '../_util/wave' ;
615import { defaultConfigProvider } from '../config-provider' ;
716import warning from '../_util/warning' ;
@@ -18,7 +27,7 @@ const switchProps = {
1827 checkedChildren : PropTypes . any ,
1928 unCheckedChildren : PropTypes . any ,
2029 tabindex : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
21- // defaultChecked: PropTypes.looseBool,
30+ defaultChecked : PropTypes . looseBool ,
2231 autofocus : PropTypes . looseBool ,
2332 loading : PropTypes . looseBool ,
2433 checked : PropTypes . looseBool ,
@@ -31,10 +40,24 @@ const Switch = defineComponent({
3140 __ANT_SWITCH : true ,
3241 inheritAttrs : false ,
3342 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+
3558 const configProvider = inject ( 'configProvider' , defaultConfigProvider ) ;
59+ const { getPrefixCls } = configProvider ;
3660 const refSwitchNode = ref ( ) ;
37-
3861 const focus = ( ) => {
3962 refSwitchNode . value ?. focus ( ) ;
4063 } ;
@@ -44,42 +67,85 @@ const Switch = defineComponent({
4467
4568 expose ( { focus, blur } ) ;
4669
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 ;
6070 const prefixCls = computed ( ( ) => {
6171 return getPrefixCls ( 'switch' , props . prefixCls ) ;
6272 } ) ;
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+ } ;
63113 return ( ) => (
64114 < 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+ ] ) }
67124 { ...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 }
75131 disabled = { props . disabled || props . loading }
76132 class = { {
77133 [ attrs . class as string ] : attrs . class ,
134+ [ prefixCls . value ] : true ,
78135 [ `${ prefixCls . value } -small` ] : props . size === 'small' ,
79136 [ `${ prefixCls . value } -loading` ] : props . loading ,
137+ [ `${ prefixCls . value } -checked` ] : checked . value ,
138+ [ `${ prefixCls . value } -disabled` ] : props . disabled ,
80139 } }
81140 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 >
83149 </ Wave >
84150 ) ;
85151 } ,
0 commit comments