11import type { ExtractPropTypes , PropType } from 'vue' ;
2- import {
3- defineComponent ,
4- inject ,
5- onBeforeMount ,
6- ref ,
7- computed ,
8- onMounted ,
9- nextTick ,
10- watch ,
11- } from 'vue' ;
2+ import { defineComponent , onBeforeMount , ref , computed , onMounted , nextTick , watch } from 'vue' ;
123import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined' ;
134import PropTypes from '../_util/vue-types' ;
145import KeyCode from '../_util/KeyCode' ;
156import Wave from '../_util/wave' ;
16- import { defaultConfigProvider } from '../config-provider' ;
177import warning from '../_util/warning' ;
188import { tuple , withInstall } from '../_util/type' ;
199import { getPropsSlot } from '../_util/props-util' ;
2010import Omit from 'omit.js' ;
11+ import useConfigInject from '../_util/hooks/useConfigInject' ;
2112
2213export const SwitchSizes = tuple ( 'small' , 'default' ) ;
2314
@@ -32,7 +23,7 @@ const switchProps = {
3223 loading : PropTypes . looseBool ,
3324 checked : PropTypes . any ,
3425 checkedValue : PropTypes . any . def ( true ) ,
35- uncheckedValue : PropTypes . any . def ( false ) ,
26+ unCheckedValue : PropTypes . any . def ( false ) ,
3627 onChange : {
3728 type : Function as PropType < ( checked : any , e : Event ) => void > ,
3829 } ,
@@ -57,6 +48,7 @@ const Switch = defineComponent({
5748 __ANT_SWITCH : true ,
5849 inheritAttrs : false ,
5950 props : switchProps ,
51+ slots : [ 'checkedChildren' , 'unCheckedChildren' ] ,
6052 emits : [ 'update:checked' , 'mouseup' , 'change' , 'click' , 'keydown' ] ,
6153 setup ( props , { attrs, slots, expose, emit } ) {
6254 onBeforeMount ( ( ) => {
@@ -81,8 +73,7 @@ const Switch = defineComponent({
8173 } ,
8274 ) ;
8375
84- const configProvider = inject ( 'configProvider' , defaultConfigProvider ) ;
85- const { getPrefixCls } = configProvider ;
76+ const { prefixCls } = useConfigInject ( 'switch' , props ) ;
8677 const refSwitchNode = ref ( ) ;
8778 const focus = ( ) => {
8879 refSwitchNode . value ?. focus ( ) ;
@@ -93,10 +84,6 @@ const Switch = defineComponent({
9384
9485 expose ( { focus, blur } ) ;
9586
96- const prefixCls = computed ( ( ) => {
97- return getPrefixCls ( 'switch' , props . prefixCls ) ;
98- } ) ;
99-
10087 onMounted ( ( ) => {
10188 nextTick ( ( ) => {
10289 if ( props . autofocus && ! props . disabled ) {
@@ -115,14 +102,14 @@ const Switch = defineComponent({
115102
116103 const handleClick = ( e : MouseEvent ) => {
117104 focus ( ) ;
118- const newChecked = checkedStatus . value ? props . uncheckedValue : props . checkedValue ;
105+ const newChecked = checkedStatus . value ? props . unCheckedValue : props . checkedValue ;
119106 setChecked ( newChecked , e ) ;
120107 emit ( 'click' , newChecked , e ) ;
121108 } ;
122109
123110 const handleKeyDown = ( e : KeyboardEvent ) => {
124111 if ( e . keyCode === KeyCode . LEFT ) {
125- setChecked ( props . uncheckedValue , e ) ;
112+ setChecked ( props . unCheckedValue , e ) ;
126113 } else if ( e . keyCode === KeyCode . RIGHT ) {
127114 setChecked ( props . checkedValue , e ) ;
128115 }
@@ -139,6 +126,7 @@ const Switch = defineComponent({
139126 [ `${ prefixCls . value } -loading` ] : props . loading ,
140127 [ `${ prefixCls . value } -checked` ] : checkedStatus . value ,
141128 [ `${ prefixCls . value } -disabled` ] : props . disabled ,
129+ [ prefixCls . value ] : true ,
142130 } ) ) ;
143131 return ( ) => (
144132 < Wave insertExtraNode >
@@ -151,7 +139,7 @@ const Switch = defineComponent({
151139 'autofocus' ,
152140 'defaultChecked' ,
153141 'checkedValue' ,
154- 'uncheckedValue ' ,
142+ 'unCheckedValue ' ,
155143 ] ) }
156144 { ...attrs }
157145 onKeydown = { handleKeyDown }
@@ -161,13 +149,7 @@ const Switch = defineComponent({
161149 role = "switch"
162150 aria-checked = { checked . value }
163151 disabled = { props . disabled || props . loading }
164- class = { [
165- {
166- [ attrs . class as string ] : ! ! attrs . class ,
167- [ prefixCls . value ] : true ,
168- } ,
169- classNames . value ,
170- ] }
152+ class = { [ attrs . class , classNames . value ] }
171153 ref = { refSwitchNode }
172154 >
173155 { props . loading ? < LoadingOutlined class = { `${ prefixCls . value } -loading-icon` } /> : null }
0 commit comments