forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
201 lines (187 loc) · 6.45 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import type { ExtractPropTypes, PropType } from 'vue';
import { defineComponent, onBeforeMount, ref, computed, onMounted, nextTick, watch } from 'vue';
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
import PropTypes from '../_util/vue-types';
import KeyCode from '../_util/KeyCode';
import Wave from '../_util/wave';
import warning from '../_util/warning';
import { tuple, withInstall } from '../_util/type';
import { getPropsSlot } from '../_util/props-util';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import { useInjectFormItemContext } from '../form/FormItemContext';
import omit from '../_util/omit';
import type { FocusEventHandler } from '../_util/EventInterface';
import useStyle from './style';
export const SwitchSizes = tuple('small', 'default');
type CheckedType = boolean | string | number;
export const switchProps = () => ({
id: String,
prefixCls: String,
size: PropTypes.oneOf(SwitchSizes),
disabled: { type: Boolean, default: undefined },
checkedChildren: PropTypes.any,
unCheckedChildren: PropTypes.any,
tabindex: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
autofocus: { type: Boolean, default: undefined },
loading: { type: Boolean, default: undefined },
checked: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.looseBool]),
checkedValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.looseBool]).def(
true,
),
unCheckedValue: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.looseBool,
]).def(false),
onChange: {
type: Function as PropType<(checked: CheckedType, e: Event) => void>,
},
onClick: {
type: Function as PropType<(checked: CheckedType, e: Event) => void>,
},
onKeydown: {
type: Function as PropType<(e: Event) => void>,
},
onMouseup: {
type: Function as PropType<(e: Event) => void>,
},
'onUpdate:checked': {
type: Function as PropType<(checked: CheckedType) => void>,
},
onBlur: Function as PropType<FocusEventHandler>,
onFocus: Function as PropType<FocusEventHandler>,
});
export type SwitchProps = Partial<ExtractPropTypes<ReturnType<typeof switchProps>>>;
const Switch = defineComponent({
compatConfig: { MODE: 3 },
name: 'ASwitch',
__ANT_SWITCH: true,
inheritAttrs: false,
props: switchProps(),
slots: ['checkedChildren', 'unCheckedChildren'],
// emits: ['update:checked', 'mouseup', 'change', 'click', 'keydown', 'blur'],
setup(props, { attrs, slots, expose, emit }) {
const formItemContext = useInjectFormItemContext();
onBeforeMount(() => {
warning(
!('defaultChecked' in attrs),
'Switch',
`'defaultChecked' is deprecated, please use 'v-model:checked'`,
);
warning(
!('value' in attrs),
'Switch',
'`value` is not validate prop, do you mean `checked`?',
);
});
const checked = ref<string | number | boolean>(
props.checked !== undefined ? props.checked : (attrs.defaultChecked as boolean),
);
const checkedStatus = computed(() => checked.value === props.checkedValue);
watch(
() => props.checked,
() => {
checked.value = props.checked;
},
);
const { prefixCls, direction, size } = useConfigInject('switch', props);
const [wrapSSR, hashId] = useStyle(prefixCls);
const refSwitchNode = ref();
const focus = () => {
refSwitchNode.value?.focus();
};
const blur = () => {
refSwitchNode.value?.blur();
};
expose({ focus, blur });
onMounted(() => {
nextTick(() => {
if (props.autofocus && !props.disabled) {
refSwitchNode.value.focus();
}
});
});
const setChecked = (check: CheckedType, e: MouseEvent | KeyboardEvent) => {
if (props.disabled) {
return;
}
emit('update:checked', check);
emit('change', check, e);
formItemContext.onFieldChange();
};
const handleBlur = (e: FocusEvent) => {
emit('blur', e);
};
const handleClick = (e: MouseEvent) => {
focus();
const newChecked = checkedStatus.value ? props.unCheckedValue : props.checkedValue;
setChecked(newChecked, e);
emit('click', newChecked, e);
};
const handleKeyDown = (e: KeyboardEvent) => {
if (e.keyCode === KeyCode.LEFT) {
setChecked(props.unCheckedValue, e);
} else if (e.keyCode === KeyCode.RIGHT) {
setChecked(props.checkedValue, e);
}
emit('keydown', e);
};
const handleMouseUp = (e: MouseEvent) => {
refSwitchNode.value?.blur();
emit('mouseup', e);
};
const classNames = computed(() => ({
[`${prefixCls.value}-small`]: size.value === 'small',
[`${prefixCls.value}-loading`]: props.loading,
[`${prefixCls.value}-checked`]: checkedStatus.value,
[`${prefixCls.value}-disabled`]: props.disabled,
[prefixCls.value]: true,
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
[hashId.value]: true,
}));
return () =>
wrapSSR(
<Wave insertExtraNode>
<button
{...omit(props, [
'prefixCls',
'checkedChildren',
'unCheckedChildren',
'checked',
'autofocus',
'checkedValue',
'unCheckedValue',
'id',
'onChange',
'onUpdate:checked',
])}
{...attrs}
id={props.id ?? formItemContext.id.value}
onKeydown={handleKeyDown}
onClick={handleClick}
onBlur={handleBlur}
onMouseup={handleMouseUp}
type="button"
role="switch"
aria-checked={checked.value as any}
disabled={props.disabled || props.loading}
class={[attrs.class, classNames.value]}
ref={refSwitchNode}
>
<div class={`${prefixCls.value}-handle`}>
{props.loading ? <LoadingOutlined class={`${prefixCls.value}-loading-icon`} /> : null}
</div>
<span class={`${prefixCls.value}-inner`}>
<span class={`${prefixCls.value}-inner-checked`}>
{getPropsSlot(slots, props, 'checkedChildren')}
</span>
<span class={`${prefixCls.value}-inner-unchecked`}>
{getPropsSlot(slots, props, 'unCheckedChildren')}
</span>
</span>
</button>
</Wave>,
);
},
});
export default withInstall(Switch);