1
1
import 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' ;
12
3
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined' ;
13
4
import PropTypes from '../_util/vue-types' ;
14
5
import KeyCode from '../_util/KeyCode' ;
15
6
import Wave from '../_util/wave' ;
16
- import { defaultConfigProvider } from '../config-provider' ;
17
7
import warning from '../_util/warning' ;
18
8
import { tuple , withInstall } from '../_util/type' ;
19
9
import { getPropsSlot } from '../_util/props-util' ;
20
10
import Omit from 'omit.js' ;
11
+ import useConfigInject from '../_util/hooks/useConfigInject' ;
21
12
22
13
export const SwitchSizes = tuple ( 'small' , 'default' ) ;
23
14
@@ -32,7 +23,7 @@ const switchProps = {
32
23
loading : PropTypes . looseBool ,
33
24
checked : PropTypes . any ,
34
25
checkedValue : PropTypes . any . def ( true ) ,
35
- uncheckedValue : PropTypes . any . def ( false ) ,
26
+ unCheckedValue : PropTypes . any . def ( false ) ,
36
27
onChange : {
37
28
type : Function as PropType < ( checked : any , e : Event ) => void > ,
38
29
} ,
@@ -57,6 +48,7 @@ const Switch = defineComponent({
57
48
__ANT_SWITCH : true ,
58
49
inheritAttrs : false ,
59
50
props : switchProps ,
51
+ slots : [ 'checkedChildren' , 'unCheckedChildren' ] ,
60
52
emits : [ 'update:checked' , 'mouseup' , 'change' , 'click' , 'keydown' ] ,
61
53
setup ( props , { attrs, slots, expose, emit } ) {
62
54
onBeforeMount ( ( ) => {
@@ -81,8 +73,7 @@ const Switch = defineComponent({
81
73
} ,
82
74
) ;
83
75
84
- const configProvider = inject ( 'configProvider' , defaultConfigProvider ) ;
85
- const { getPrefixCls } = configProvider ;
76
+ const { prefixCls } = useConfigInject ( 'switch' , props ) ;
86
77
const refSwitchNode = ref ( ) ;
87
78
const focus = ( ) => {
88
79
refSwitchNode . value ?. focus ( ) ;
@@ -93,10 +84,6 @@ const Switch = defineComponent({
93
84
94
85
expose ( { focus, blur } ) ;
95
86
96
- const prefixCls = computed ( ( ) => {
97
- return getPrefixCls ( 'switch' , props . prefixCls ) ;
98
- } ) ;
99
-
100
87
onMounted ( ( ) => {
101
88
nextTick ( ( ) => {
102
89
if ( props . autofocus && ! props . disabled ) {
@@ -115,14 +102,14 @@ const Switch = defineComponent({
115
102
116
103
const handleClick = ( e : MouseEvent ) => {
117
104
focus ( ) ;
118
- const newChecked = checkedStatus . value ? props . uncheckedValue : props . checkedValue ;
105
+ const newChecked = checkedStatus . value ? props . unCheckedValue : props . checkedValue ;
119
106
setChecked ( newChecked , e ) ;
120
107
emit ( 'click' , newChecked , e ) ;
121
108
} ;
122
109
123
110
const handleKeyDown = ( e : KeyboardEvent ) => {
124
111
if ( e . keyCode === KeyCode . LEFT ) {
125
- setChecked ( props . uncheckedValue , e ) ;
112
+ setChecked ( props . unCheckedValue , e ) ;
126
113
} else if ( e . keyCode === KeyCode . RIGHT ) {
127
114
setChecked ( props . checkedValue , e ) ;
128
115
}
@@ -139,6 +126,7 @@ const Switch = defineComponent({
139
126
[ `${ prefixCls . value } -loading` ] : props . loading ,
140
127
[ `${ prefixCls . value } -checked` ] : checkedStatus . value ,
141
128
[ `${ prefixCls . value } -disabled` ] : props . disabled ,
129
+ [ prefixCls . value ] : true ,
142
130
} ) ) ;
143
131
return ( ) => (
144
132
< Wave insertExtraNode >
@@ -151,7 +139,7 @@ const Switch = defineComponent({
151
139
'autofocus' ,
152
140
'defaultChecked' ,
153
141
'checkedValue' ,
154
- 'uncheckedValue ' ,
142
+ 'unCheckedValue ' ,
155
143
] ) }
156
144
{ ...attrs }
157
145
onKeydown = { handleKeyDown }
@@ -161,13 +149,7 @@ const Switch = defineComponent({
161
149
role = "switch"
162
150
aria-checked = { checked . value }
163
151
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 ] }
171
153
ref = { refSwitchNode }
172
154
>
173
155
{ props . loading ? < LoadingOutlined class = { `${ prefixCls . value } -loading-icon` } /> : null }
0 commit comments