2021-07-07 13:35:41 +00:00
|
|
|
import type { ExtractPropTypes, PropType } from 'vue';
|
2021-07-07 13:50:50 +00:00
|
|
|
import { defineComponent, onBeforeMount, ref, computed, onMounted, nextTick, watch } 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-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';
|
2021-07-07 13:50:50 +00:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
2021-09-25 08:51:32 +00:00
|
|
|
import { useInjectFormItemContext } from '../form/FormItemContext';
|
|
|
|
import omit from '../_util/omit';
|
2021-04-11 01:57:20 +00:00
|
|
|
|
2021-07-07 13:35:41 +00:00
|
|
|
export const SwitchSizes = tuple('small', 'default');
|
2021-07-07 14:09:09 +00:00
|
|
|
type CheckedType = boolean | string | number;
|
2021-04-11 01:57:20 +00:00
|
|
|
const switchProps = {
|
2021-09-25 08:51:32 +00:00
|
|
|
id: PropTypes.string,
|
2021-04-11 01:57:20 +00:00
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
size: PropTypes.oneOf(SwitchSizes),
|
|
|
|
disabled: PropTypes.looseBool,
|
2021-07-07 13:35:41 +00:00
|
|
|
checkedChildren: PropTypes.VNodeChild,
|
|
|
|
unCheckedChildren: PropTypes.VNodeChild,
|
2021-04-11 01:57:20 +00:00
|
|
|
tabindex: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
|
|
autofocus: PropTypes.looseBool,
|
|
|
|
loading: PropTypes.looseBool,
|
2021-07-07 14:09:09 +00:00
|
|
|
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),
|
2021-07-07 13:35:41 +00:00
|
|
|
onChange: {
|
2021-07-07 14:09:09 +00:00
|
|
|
type: Function as PropType<(checked: CheckedType, e: Event) => void>,
|
2021-07-07 13:35:41 +00:00
|
|
|
},
|
|
|
|
onClick: {
|
2021-07-07 14:09:09 +00:00
|
|
|
type: Function as PropType<(checked: CheckedType, e: Event) => void>,
|
2021-07-07 13:35:41 +00:00
|
|
|
},
|
|
|
|
onKeydown: {
|
|
|
|
type: Function as PropType<(e: Event) => void>,
|
|
|
|
},
|
|
|
|
onMouseup: {
|
|
|
|
type: Function as PropType<(e: Event) => void>,
|
|
|
|
},
|
|
|
|
'onUpdate:checked': {
|
2021-07-07 14:09:09 +00:00
|
|
|
type: Function as PropType<(checked: CheckedType) => void>,
|
2021-07-07 13:35:41 +00:00
|
|
|
},
|
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-07-07 13:50:50 +00:00
|
|
|
slots: ['checkedChildren', 'unCheckedChildren'],
|
2021-09-25 08:51:32 +00:00
|
|
|
emits: ['update:checked', 'mouseup', 'change', 'click', 'keydown', 'blur'],
|
2021-07-07 13:35:41 +00:00
|
|
|
setup(props, { attrs, slots, expose, emit }) {
|
2021-09-25 08:51:32 +00:00
|
|
|
const formItemContext = useInjectFormItemContext();
|
2021-04-11 07:12:26 +00:00
|
|
|
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-09-25 08:51:32 +00:00
|
|
|
const checked = ref<string | number | boolean>(
|
|
|
|
props.checked !== undefined ? props.checked : (attrs.defaultChecked as boolean),
|
|
|
|
);
|
2021-07-07 13:35:41 +00:00
|
|
|
const checkedStatus = computed(() => checked.value === props.checkedValue);
|
2021-06-08 06:18:54 +00:00
|
|
|
|
|
|
|
watch(
|
|
|
|
() => props.checked,
|
|
|
|
() => {
|
2021-07-07 13:35:41 +00:00
|
|
|
checked.value = props.checked;
|
2021-06-08 06:18:54 +00:00
|
|
|
},
|
|
|
|
);
|
2021-04-11 07:12:26 +00:00
|
|
|
|
2021-07-07 13:50:50 +00:00
|
|
|
const { prefixCls } = useConfigInject('switch', props);
|
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 });
|
|
|
|
|
2021-04-11 07:12:26 +00:00
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
if (props.autofocus && !props.disabled) {
|
|
|
|
refSwitchNode.value.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-07 14:09:09 +00:00
|
|
|
const setChecked = (check: CheckedType, e: MouseEvent | KeyboardEvent) => {
|
2021-04-11 07:12:26 +00:00
|
|
|
if (props.disabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emit('update:checked', check);
|
|
|
|
emit('change', check, e);
|
2021-09-25 08:51:32 +00:00
|
|
|
formItemContext.onFieldChange();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleBlur = () => {
|
|
|
|
emit('blur');
|
2021-04-11 07:12:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleClick = (e: MouseEvent) => {
|
|
|
|
focus();
|
2021-07-07 13:50:50 +00:00
|
|
|
const newChecked = checkedStatus.value ? props.unCheckedValue : props.checkedValue;
|
2021-04-11 07:12:26 +00:00
|
|
|
setChecked(newChecked, e);
|
|
|
|
emit('click', newChecked, e);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
|
|
if (e.keyCode === KeyCode.LEFT) {
|
2021-07-07 13:50:50 +00:00
|
|
|
setChecked(props.unCheckedValue, e);
|
2021-04-11 07:12:26 +00:00
|
|
|
} else if (e.keyCode === KeyCode.RIGHT) {
|
2021-07-07 13:35:41 +00:00
|
|
|
setChecked(props.checkedValue, e);
|
2021-04-11 07:12:26 +00:00
|
|
|
}
|
|
|
|
emit('keydown', e);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleMouseUp = (e: MouseEvent) => {
|
|
|
|
refSwitchNode.value?.blur();
|
|
|
|
emit('mouseup', e);
|
|
|
|
};
|
2021-07-07 13:35:41 +00:00
|
|
|
|
|
|
|
const classNames = computed(() => ({
|
|
|
|
[`${prefixCls.value}-small`]: props.size === 'small',
|
|
|
|
[`${prefixCls.value}-loading`]: props.loading,
|
|
|
|
[`${prefixCls.value}-checked`]: checkedStatus.value,
|
|
|
|
[`${prefixCls.value}-disabled`]: props.disabled,
|
2021-07-07 13:50:50 +00:00
|
|
|
[prefixCls.value]: true,
|
2021-07-07 13:35:41 +00:00
|
|
|
}));
|
2021-08-18 07:41:21 +00:00
|
|
|
|
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
|
2021-09-25 08:51:32 +00:00
|
|
|
{...omit(props, [
|
2021-04-11 07:12:26 +00:00
|
|
|
'prefixCls',
|
|
|
|
'checkedChildren',
|
|
|
|
'unCheckedChildren',
|
|
|
|
'checked',
|
|
|
|
'autofocus',
|
2021-07-07 13:35:41 +00:00
|
|
|
'checkedValue',
|
2021-07-07 13:50:50 +00:00
|
|
|
'unCheckedValue',
|
2021-09-25 08:51:32 +00:00
|
|
|
'id',
|
2021-04-11 07:12:26 +00:00
|
|
|
])}
|
2021-04-11 01:57:20 +00:00
|
|
|
{...attrs}
|
2021-09-25 08:51:32 +00:00
|
|
|
id={props.id ?? formItemContext.id.value}
|
2021-04-11 07:12:26 +00:00
|
|
|
onKeydown={handleKeyDown}
|
|
|
|
onClick={handleClick}
|
2021-09-25 08:51:32 +00:00
|
|
|
onBlur={handleBlur}
|
2021-04-11 07:12:26 +00:00
|
|
|
onMouseup={handleMouseUp}
|
|
|
|
type="button"
|
|
|
|
role="switch"
|
2021-09-25 08:51:32 +00:00
|
|
|
aria-checked={checked.value as any}
|
2021-04-11 01:57:20 +00:00
|
|
|
disabled={props.disabled || props.loading}
|
2021-07-07 13:50:50 +00:00
|
|
|
class={[attrs.class, classNames.value]}
|
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`}>
|
2021-08-18 07:41:21 +00:00
|
|
|
{checkedStatus.value
|
2021-04-11 07:12:26 +00:00
|
|
|
? 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);
|