|
1 |
| -import { defineComponent, inject } from 'vue'; |
| 1 | +import { defineComponent, inject, onBeforeMount, ref, ExtractPropTypes, computed } from 'vue'; |
2 | 2 | import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
|
3 | 3 | import PropTypes from '../_util/vue-types';
|
4 |
| -import hasProp, { getOptionProps, getComponent } from '../_util/props-util'; |
5 | 4 | import VcSwitch from '../vc-switch';
|
6 | 5 | import Wave from '../_util/wave';
|
7 | 6 | import { defaultConfigProvider } from '../config-provider';
|
8 | 7 | import warning from '../_util/warning';
|
9 | 8 | import { tuple, withInstall } from '../_util/type';
|
| 9 | +import { getPropsSlot } from '../_util/props-util'; |
| 10 | +import Omit from 'omit.js'; |
| 11 | + |
| 12 | +export const SwitchSizes = tuple('small', 'default', 'large'); |
| 13 | + |
| 14 | +const switchProps = { |
| 15 | + prefixCls: PropTypes.string, |
| 16 | + size: PropTypes.oneOf(SwitchSizes), |
| 17 | + disabled: PropTypes.looseBool, |
| 18 | + checkedChildren: PropTypes.any, |
| 19 | + unCheckedChildren: PropTypes.any, |
| 20 | + tabindex: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), |
| 21 | + // defaultChecked: PropTypes.looseBool, |
| 22 | + autofocus: PropTypes.looseBool, |
| 23 | + loading: PropTypes.looseBool, |
| 24 | + checked: PropTypes.looseBool, |
| 25 | +}; |
| 26 | + |
| 27 | +export type SwitchProps = Partial<ExtractPropTypes<typeof switchProps>>; |
10 | 28 |
|
11 | 29 | const Switch = defineComponent({
|
12 | 30 | name: 'ASwitch',
|
13 | 31 | __ANT_SWITCH: true,
|
14 | 32 | inheritAttrs: false,
|
15 |
| - props: { |
16 |
| - prefixCls: PropTypes.string, |
17 |
| - // size=default and size=large are the same |
18 |
| - size: PropTypes.oneOf(tuple('small', 'default', 'large')), |
19 |
| - disabled: PropTypes.looseBool, |
20 |
| - checkedChildren: PropTypes.any, |
21 |
| - unCheckedChildren: PropTypes.any, |
22 |
| - tabindex: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), |
23 |
| - checked: PropTypes.looseBool, |
24 |
| - defaultChecked: PropTypes.looseBool, |
25 |
| - autofocus: PropTypes.looseBool, |
26 |
| - loading: PropTypes.looseBool, |
27 |
| - onChange: PropTypes.func, |
28 |
| - onClick: PropTypes.func, |
29 |
| - 'onUpdate:checked': PropTypes.func, |
30 |
| - }, |
31 |
| - // emits: ['change', 'click', 'update:checked'], |
32 |
| - setup() { |
33 |
| - return { |
34 |
| - refSwitchNode: undefined, |
35 |
| - configProvider: inject('configProvider', defaultConfigProvider), |
36 |
| - }; |
37 |
| - }, |
38 |
| - created() { |
39 |
| - warning( |
40 |
| - hasProp(this, 'checked') || !('value' in this.$attrs), |
41 |
| - 'Switch', |
42 |
| - '`value` is not validate prop, do you mean `checked`?', |
43 |
| - ); |
44 |
| - }, |
45 |
| - methods: { |
46 |
| - focus() { |
47 |
| - this.refSwitchNode?.focus(); |
48 |
| - }, |
49 |
| - blur() { |
50 |
| - this.refSwitchNode?.blur(); |
51 |
| - }, |
52 |
| - saveRef(c) { |
53 |
| - this.refSwitchNode = c; |
54 |
| - }, |
55 |
| - }, |
| 33 | + props: switchProps, |
| 34 | + setup(props: SwitchProps, { attrs, slots, expose }) { |
| 35 | + const configProvider = inject('configProvider', defaultConfigProvider); |
| 36 | + const refSwitchNode = ref(); |
56 | 37 |
|
57 |
| - render() { |
58 |
| - const { prefixCls: customizePrefixCls, size, loading, disabled, ...restProps } = getOptionProps( |
59 |
| - this, |
60 |
| - ); |
61 |
| - const { getPrefixCls } = this.configProvider; |
62 |
| - const prefixCls = getPrefixCls('switch', customizePrefixCls); |
63 |
| - const { $attrs } = this; |
64 |
| - const classes = { |
65 |
| - [$attrs.class as string]: $attrs.class, |
66 |
| - [`${prefixCls}-small`]: size === 'small', |
67 |
| - [`${prefixCls}-loading`]: loading, |
| 38 | + const focus = () => { |
| 39 | + refSwitchNode.value?.focus(); |
68 | 40 | };
|
69 |
| - const loadingIcon = loading ? <LoadingOutlined class={`${prefixCls}-loading-icon`} /> : null; |
70 |
| - const switchProps = { |
71 |
| - ...restProps, |
72 |
| - ...$attrs, |
73 |
| - prefixCls, |
74 |
| - loadingIcon, |
75 |
| - checkedChildren: getComponent(this, 'checkedChildren'), |
76 |
| - unCheckedChildren: getComponent(this, 'unCheckedChildren'), |
77 |
| - disabled: disabled || loading, |
78 |
| - class: classes, |
79 |
| - ref: this.saveRef, |
| 41 | + const blur = () => { |
| 42 | + refSwitchNode.value?.blur(); |
80 | 43 | };
|
81 |
| - return ( |
| 44 | + |
| 45 | + expose({ focus, blur }); |
| 46 | + |
| 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; |
| 60 | + const prefixCls = computed(() => { |
| 61 | + return getPrefixCls('switch', props.prefixCls); |
| 62 | + }); |
| 63 | + return () => ( |
82 | 64 | <Wave insertExtraNode>
|
83 |
| - <VcSwitch {...switchProps} /> |
| 65 | + <VcSwitch |
| 66 | + {...Omit(props, ['prefixCls', 'size', 'loading', 'disabled'])} |
| 67 | + {...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')} |
| 75 | + disabled={props.disabled || props.loading} |
| 76 | + class={{ |
| 77 | + [attrs.class as string]: attrs.class, |
| 78 | + [`${prefixCls.value}-small`]: props.size === 'small', |
| 79 | + [`${prefixCls.value}-loading`]: props.loading, |
| 80 | + }} |
| 81 | + ref={refSwitchNode} |
| 82 | + /> |
84 | 83 | </Wave>
|
85 | 84 | );
|
86 | 85 | },
|
|
0 commit comments