Skip to content

Commit 4a7fcf9

Browse files
committed
refactor: switch #3885
1 parent 67b9da7 commit 4a7fcf9

File tree

7 files changed

+96
-266
lines changed

7 files changed

+96
-266
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Switch should has click wave effect 1`] = `
4-
<button class="ant-switch ant-switch-checked" type="button" role="switch" aria-checked="true">
4+
<button type="button" role="switch" aria-checked="true" class="ant-switch ant-switch-checked">
55
<!----><span class="ant-switch-inner"><!----></span>
66
</button>
77
`;

components/switch/index.tsx

+94-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
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';
211
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
312
import PropTypes from '../_util/vue-types';
4-
import VcSwitch from '../vc-switch';
13+
import KeyCode from '../_util/KeyCode';
514
import Wave from '../_util/wave';
615
import { defaultConfigProvider } from '../config-provider';
716
import 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
},

components/vc-switch/assets/index.less

-117
This file was deleted.

components/vc-switch/index.ts

-4
This file was deleted.

components/vc-switch/src/PropTypes.ts

-16
This file was deleted.

0 commit comments

Comments
 (0)