| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
- import { createVNode as _createVNode } from "vue";
- import { defineComponent, onBeforeMount, ref, computed, onMounted, nextTick, watch } from 'vue';
- import LoadingOutlined from "@ant-design/icons-vue/es/icons/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 useStyle from './style';
- import { useInjectDisabled } from '../config-provider/DisabledContext';
- export const SwitchSizes = tuple('small', 'default');
- 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
- },
- onClick: {
- type: Function
- },
- onKeydown: {
- type: Function
- },
- onMouseup: {
- type: Function
- },
- 'onUpdate:checked': {
- type: Function
- },
- onBlur: Function,
- onFocus: Function
- });
- const Switch = defineComponent({
- compatConfig: {
- MODE: 3
- },
- name: 'ASwitch',
- __ANT_SWITCH: true,
- inheritAttrs: false,
- props: switchProps(),
- slots: Object,
- // emits: ['update:checked', 'mouseup', 'change', 'click', 'keydown', 'blur'],
- setup(props, _ref) {
- let {
- attrs,
- slots,
- expose,
- emit
- } = _ref;
- const formItemContext = useInjectFormItemContext();
- const disabledContext = useInjectDisabled();
- const mergedDisabled = computed(() => {
- var _a;
- return (_a = props.disabled) !== null && _a !== void 0 ? _a : disabledContext.value;
- });
- 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(props.checked !== undefined ? props.checked : attrs.defaultChecked);
- 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 = () => {
- var _a;
- (_a = refSwitchNode.value) === null || _a === void 0 ? void 0 : _a.focus();
- };
- const blur = () => {
- var _a;
- (_a = refSwitchNode.value) === null || _a === void 0 ? void 0 : _a.blur();
- };
- expose({
- focus,
- blur
- });
- onMounted(() => {
- nextTick(() => {
- if (props.autofocus && !mergedDisabled.value) {
- refSwitchNode.value.focus();
- }
- });
- });
- const setChecked = (check, e) => {
- if (mergedDisabled.value) {
- return;
- }
- emit('update:checked', check);
- emit('change', check, e);
- formItemContext.onFieldChange();
- };
- const handleBlur = e => {
- emit('blur', e);
- };
- const handleClick = e => {
- focus();
- const newChecked = checkedStatus.value ? props.unCheckedValue : props.checkedValue;
- setChecked(newChecked, e);
- emit('click', newChecked, e);
- };
- const handleKeyDown = e => {
- 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 => {
- var _a;
- (_a = refSwitchNode.value) === null || _a === void 0 ? void 0 : _a.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`]: mergedDisabled.value,
- [prefixCls.value]: true,
- [`${prefixCls.value}-rtl`]: direction.value === 'rtl',
- [hashId.value]: true
- }));
- return () => {
- var _a;
- return wrapSSR(_createVNode(Wave, null, {
- default: () => [_createVNode("button", _objectSpread(_objectSpread(_objectSpread({}, omit(props, ['prefixCls', 'checkedChildren', 'unCheckedChildren', 'checked', 'autofocus', 'checkedValue', 'unCheckedValue', 'id', 'onChange', 'onUpdate:checked'])), attrs), {}, {
- "id": (_a = props.id) !== null && _a !== void 0 ? _a : formItemContext.id.value,
- "onKeydown": handleKeyDown,
- "onClick": handleClick,
- "onBlur": handleBlur,
- "onMouseup": handleMouseUp,
- "type": "button",
- "role": "switch",
- "aria-checked": checked.value,
- "disabled": mergedDisabled.value || props.loading,
- "class": [attrs.class, classNames.value],
- "ref": refSwitchNode
- }), [_createVNode("div", {
- "class": `${prefixCls.value}-handle`
- }, [props.loading ? _createVNode(LoadingOutlined, {
- "class": `${prefixCls.value}-loading-icon`
- }, null) : null]), _createVNode("span", {
- "class": `${prefixCls.value}-inner`
- }, [_createVNode("span", {
- "class": `${prefixCls.value}-inner-checked`
- }, [getPropsSlot(slots, props, 'checkedChildren')]), _createVNode("span", {
- "class": `${prefixCls.value}-inner-unchecked`
- }, [getPropsSlot(slots, props, 'unCheckedChildren')])])])]
- }));
- };
- }
- });
- export default withInstall(Switch);
|