-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathindex.tsx
87 lines (79 loc) · 2.83 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { defineComponent, inject, onBeforeMount, ref, ExtractPropTypes } from 'vue';
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
import PropTypes from '../_util/vue-types';
import VcSwitch from '../vc-switch';
import Wave from '../_util/wave';
import { defaultConfigProvider } from '../config-provider';
import warning from '../_util/warning';
import { tuple, withInstall } from '../_util/type';
import { getPropsSlot } from '../_util/props-util';
import Omit from 'omit.js';
export const SwitchSizes = tuple('small', 'default', 'large');
const switchProps = {
prefixCls: PropTypes.string,
size: PropTypes.oneOf(SwitchSizes),
disabled: PropTypes.looseBool,
checkedChildren: PropTypes.any,
unCheckedChildren: PropTypes.any,
tabindex: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
// defaultChecked: PropTypes.looseBool,
autofocus: PropTypes.looseBool,
loading: PropTypes.looseBool,
checked: PropTypes.looseBool,
};
export type SwitchProps = Partial<ExtractPropTypes<typeof switchProps>>;
const Switch = defineComponent({
name: 'ASwitch',
__ANT_SWITCH: true,
inheritAttrs: false,
props: switchProps,
setup(props: SwitchProps, { attrs, slots, expose }) {
const configProvider = inject('configProvider', defaultConfigProvider);
const refSwitchNode = ref();
const focus = () => {
refSwitchNode.value?.focus();
};
const blur = () => {
refSwitchNode.value?.blur();
};
expose({ focus, blur });
onBeforeMount(() => {
if ('defaultChecked' in attrs) {
console.warn(
`[antdv: Switch]: 'defaultChecked' will be obsolete, please use 'v-model:checked'`,
);
}
warning(
!('value' in attrs),
'Switch',
'`value` is not validate prop, do you mean `checked`?',
);
});
const { getPrefixCls } = configProvider;
return () => (
<Wave insertExtraNode>
<VcSwitch
{...Omit(props, ['prefixCls', 'size', 'loading', 'disabled'])}
{...attrs}
checked={props.checked}
prefixCls={getPrefixCls('switch', props.prefixCls)}
loadingIcon={
props.loading ? (
<LoadingOutlined class={`${getPrefixCls('switch', props.prefixCls)}-loading-icon`} />
) : null
}
checkedChildren={getPropsSlot(slots, props, 'checkedChildren')}
unCheckedChildren={getPropsSlot(slots, props, 'unCheckedChildren')}
disabled={props.disabled || props.loading}
class={{
[attrs.class as string]: attrs.class,
[`${getPrefixCls('switch', props.prefixCls)}-small`]: props.size === 'small',
[`${getPrefixCls('switch', props.prefixCls)}-loading`]: props.loading,
}}
ref={refSwitchNode}
/>
</Wave>
);
},
});
export default withInstall(Switch);