You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0 lines
19 KiB

1 month ago
  1. {"ast":null,"code":"import { defineComponent, computed, ref, watch, onMounted, openBlock, createElementBlock, normalizeClass, unref, withModifiers, createElementVNode, withKeys, createBlock, withCtx, resolveDynamicComponent, createCommentVNode, toDisplayString, normalizeStyle, createVNode, renderSlot, nextTick } from 'vue';\nimport { ElIcon } from '../../icon/index.mjs';\nimport { Loading } from '@element-plus/icons-vue';\nimport { switchProps, switchEmits } from './switch.mjs';\nimport _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';\nimport { useFormItem, useFormItemInputId } from '../../form/src/hooks/use-form-item.mjs';\nimport { useFormSize, useFormDisabled } from '../../form/src/hooks/use-form-common-props.mjs';\nimport { useNamespace } from '../../../hooks/use-namespace/index.mjs';\nimport { addUnit } from '../../../utils/dom/style.mjs';\nimport { UPDATE_MODEL_EVENT, CHANGE_EVENT, INPUT_EVENT } from '../../../constants/event.mjs';\nimport { debugWarn, throwError } from '../../../utils/error.mjs';\nimport { isPromise } from '@vue/shared';\nimport { isBoolean } from '../../../utils/types.mjs';\nconst COMPONENT_NAME = \"ElSwitch\";\nconst __default__ = defineComponent({\n name: COMPONENT_NAME\n});\nconst _sfc_main = /* @__PURE__ */defineComponent({\n ...__default__,\n props: switchProps,\n emits: switchEmits,\n setup(__props, {\n expose,\n emit\n }) {\n const props = __props;\n const {\n formItem\n } = useFormItem();\n const switchSize = useFormSize();\n const ns = useNamespace(\"switch\");\n const {\n inputId\n } = useFormItemInputId(props, {\n formItemContext: formItem\n });\n const switchDisabled = useFormDisabled(computed(() => props.loading));\n const isControlled = ref(props.modelValue !== false);\n const input = ref();\n const core = ref();\n const switchKls = computed(() => [ns.b(), ns.m(switchSize.value), ns.is(\"disabled\", switchDisabled.value), ns.is(\"checked\", checked.value)]);\n const labelLeftKls = computed(() => [ns.e(\"label\"), ns.em(\"label\", \"left\"), ns.is(\"active\", !checked.value)]);\n const labelRightKls = computed(() => [ns.e(\"label\"), ns.em(\"label\", \"right\"), ns.is(\"active\", checked.value)]);\n const coreStyle = computed(() => ({\n width: addUnit(props.width)\n }));\n watch(() => props.modelValue, () => {\n isControlled.value = true;\n });\n const actualValue = computed(() => {\n return isControlled.value ? props.modelValue : false;\n });\n const checked = computed(() => actualValue.value === props.activeValue);\n if (![props.activeValue, props.inactiveValue].includes(actualValue.value)) {\n emit(UPDATE_MODEL_EVENT, props.inactiveValue);\n emit(CHANGE_EVENT, props.inactiveValue);\n emit(INPUT_EVENT, props.inactiveValue);\n }\n watch(checked, val => {\n var _a;\n input.value.checked = val;\n if (props.validateEvent) {\n (_a = formItem == null ? void 0 : formItem.validate) == null ? void 0 : _a.call(formItem, \"change\").catch(err => debugWarn(err));\n }\n });\n const handleChange = () => {\n const val = checked.value ? props.inactiveValue : props.activeValue;\n emit(UPDATE_MODEL_EVENT, val);\n emit(CHANGE_EVENT, val);\n emit(INPUT_EVENT, val);\n nextTick(() => {\n input.value.checked = checked.value;\n });\n };\n const switchValue = () => {\n if (switchDisabled.value) return;\n const {\n beforeChange\n } = props;\n if (!beforeChange) {\n handleChange();\n return;\n }\n const shouldChange = beforeChange();\n const isPromiseOrBool = [isPromise(shouldChange), isBoolean(shouldChange)].includes(true);\n if (!isPromiseOrBool) {\n throwError(COMPONENT_NAME, \"beforeChange must return type `Promise<boolean>` or `boolean`\");\n }\n if (isPromise(shouldChange)) {\n shouldChange.then(result => {\n if (result) {\n handleChange();\n }\n }).catch(e =>