2021-04-11 07:12:26 +00:00
|
|
|
import {
|
|
|
|
defineComponent,
|
|
|
|
inject,
|
|
|
|
onBeforeMount,
|
|
|
|
ref,
|
|
|
|
ExtractPropTypes,
|
|
|
|
computed,
|
|
|
|
onMounted,
|
|
|
|
nextTick,
|
|
|
|
} from 'vue';
|
2020-03-30 16:02:28 +00:00
|
|
|
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2021-04-11 07:12:26 +00:00
|
|
|
import KeyCode from '../_util/KeyCode';
|
2019-01-12 03:33:27 +00:00
|
|
|
import Wave from '../_util/wave';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-03-07 11:45:13 +00:00
|
|
|
import warning from '../_util/warning';
|
2020-11-01 07:03:33 +00:00
|
|
|
import { tuple, withInstall } from '../_util/type';
|
2021-04-11 01:57:20 +00:00
|
|
|
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]),
|
2021-04-11 07:12:26 +00:00
|
|
|
defaultChecked: PropTypes.looseBool,
|
2021-04-11 01:57:20 +00:00
|
|
|
autofocus: PropTypes.looseBool,
|
|
|
|
loading: PropTypes.looseBool,
|
|
|
|
checked: PropTypes.looseBool,
|
2021-04-27 14:10:37 +00:00
|
|
|
onChange: PropTypes.func,
|
|
|
|
onClick: PropTypes.func,
|
|
|
|
onKeydown: PropTypes.func,
|
|
|
|
onMouseup: PropTypes.func,
|
2021-05-24 06:59:20 +00:00
|
|
|
'onUpdate:checked': PropTypes.func,
|
2021-04-11 01:57:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type SwitchProps = Partial<ExtractPropTypes<typeof switchProps>>;
|
2018-02-27 04:14:29 +00:00
|
|
|
|
2020-10-20 04:46:16 +00:00
|
|
|
const Switch = defineComponent({
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ASwitch',
|
2020-03-07 11:45:13 +00:00
|
|
|
__ANT_SWITCH: true,
|
2020-06-23 07:45:30 +00:00
|
|
|
inheritAttrs: false,
|
2021-04-11 01:57:20 +00:00
|
|
|
props: switchProps,
|
2021-04-11 07:12:26 +00:00
|
|
|
emits: ['update:checked', 'mouseup', 'change', 'click', 'keydown'],
|
|
|
|
setup(props: SwitchProps, { attrs, slots, expose, emit }) {
|
|
|
|
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`?',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-11 01:57:20 +00:00
|
|
|
const configProvider = inject('configProvider', defaultConfigProvider);
|
2021-04-11 07:12:26 +00:00
|
|
|
const { getPrefixCls } = configProvider;
|
2021-04-11 01:57:20 +00:00
|
|
|
const refSwitchNode = ref();
|
|
|
|
const focus = () => {
|
|
|
|
refSwitchNode.value?.focus();
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2021-04-11 01:57:20 +00:00
|
|
|
const blur = () => {
|
|
|
|
refSwitchNode.value?.blur();
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2021-04-11 01:57:20 +00:00
|
|
|
|
|
|
|
expose({ focus, blur });
|
|
|
|
|
|
|
|
const prefixCls = computed(() => {
|
|
|
|
return getPrefixCls('switch', props.prefixCls);
|
|
|
|
});
|
2021-04-11 07:12:26 +00:00
|
|
|
const checked = computed(() => {
|
|
|
|
return 'checked' in props ? !!props.checked : !!props.defaultChecked;
|
|
|
|
});
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
if (props.autofocus && !props.disabled) {
|
|
|
|
refSwitchNode.value.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const setChecked = (check: boolean, e: MouseEvent | KeyboardEvent) => {
|
|
|
|
if (props.disabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emit('update:checked', check);
|
|
|
|
emit('change', check, e);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleClick = (e: MouseEvent) => {
|
|
|
|
focus();
|
|
|
|
const newChecked = !checked.value;
|
|
|
|
setChecked(newChecked, e);
|
|
|
|
emit('click', newChecked, e);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
|
|
if (e.keyCode === KeyCode.LEFT) {
|
|
|
|
setChecked(false, e);
|
|
|
|
} else if (e.keyCode === KeyCode.RIGHT) {
|
|
|
|
setChecked(true, e);
|
|
|
|
}
|
|
|
|
emit('keydown', e);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleMouseUp = (e: MouseEvent) => {
|
|
|
|
refSwitchNode.value?.blur();
|
|
|
|
emit('mouseup', e);
|
|
|
|
};
|
2021-04-11 01:57:20 +00:00
|
|
|
return () => (
|
2020-10-20 04:46:16 +00:00
|
|
|
<Wave insertExtraNode>
|
2021-04-11 07:12:26 +00:00
|
|
|
<button
|
|
|
|
{...Omit(props, [
|
|
|
|
'prefixCls',
|
|
|
|
'checkedChildren',
|
|
|
|
'unCheckedChildren',
|
|
|
|
'checked',
|
|
|
|
'autofocus',
|
|
|
|
'defaultChecked',
|
|
|
|
])}
|
2021-04-11 01:57:20 +00:00
|
|
|
{...attrs}
|
2021-04-11 07:12:26 +00:00
|
|
|
onKeydown={handleKeyDown}
|
|
|
|
onClick={handleClick}
|
|
|
|
onMouseup={handleMouseUp}
|
|
|
|
type="button"
|
|
|
|
role="switch"
|
|
|
|
aria-checked={checked.value}
|
2021-04-11 01:57:20 +00:00
|
|
|
disabled={props.disabled || props.loading}
|
|
|
|
class={{
|
|
|
|
[attrs.class as string]: attrs.class,
|
2021-04-11 07:12:26 +00:00
|
|
|
[prefixCls.value]: true,
|
2021-04-11 01:57:20 +00:00
|
|
|
[`${prefixCls.value}-small`]: props.size === 'small',
|
|
|
|
[`${prefixCls.value}-loading`]: props.loading,
|
2021-04-11 07:12:26 +00:00
|
|
|
[`${prefixCls.value}-checked`]: checked.value,
|
|
|
|
[`${prefixCls.value}-disabled`]: props.disabled,
|
2021-04-11 01:57:20 +00:00
|
|
|
}}
|
|
|
|
ref={refSwitchNode}
|
2021-04-11 07:12:26 +00:00
|
|
|
>
|
|
|
|
{props.loading ? <LoadingOutlined class={`${prefixCls.value}-loading-icon`} /> : null}
|
|
|
|
<span class={`${prefixCls.value}-inner`}>
|
|
|
|
{checked.value
|
|
|
|
? getPropsSlot(slots, props, 'checkedChildren')
|
|
|
|
: getPropsSlot(slots, props, 'unCheckedChildren')}
|
|
|
|
</span>
|
|
|
|
</button>
|
2020-10-20 04:46:16 +00:00
|
|
|
</Wave>
|
|
|
|
);
|
2018-02-27 04:14:29 +00:00
|
|
|
},
|
2020-10-20 04:46:16 +00:00
|
|
|
});
|
2018-03-19 02:16:27 +00:00
|
|
|
|
2020-11-01 07:03:33 +00:00
|
|
|
export default withInstall(Switch);
|