{"version":3,"file":"switch2.mjs","names":[],"sources":["../../../../../../packages/components/switch/src/switch.vue"],"sourcesContent":["<template>\n  <div :class=\"switchKls\" @click.prevent=\"switchValue\">\n    <input\n      :id=\"inputId\"\n      ref=\"input\"\n      :class=\"ns.e('input')\"\n      type=\"checkbox\"\n      role=\"switch\"\n      :aria-checked=\"checked\"\n      :aria-disabled=\"switchDisabled\"\n      :aria-label=\"ariaLabel\"\n      :name=\"name\"\n      :true-value=\"activeValue\"\n      :false-value=\"inactiveValue\"\n      :disabled=\"switchDisabled\"\n      :tabindex=\"tabindex\"\n      @change=\"handleChange\"\n      @keydown.enter=\"switchValue\"\n    />\n    <span\n      v-if=\"!inlinePrompt && (inactiveIcon || inactiveText || $slots.inactive)\"\n      :class=\"labelLeftKls\"\n    >\n      <slot name=\"inactive\">\n        <el-icon v-if=\"inactiveIcon\">\n          <component :is=\"inactiveIcon\" />\n        </el-icon>\n        <span v-if=\"!inactiveIcon && inactiveText\" :aria-hidden=\"checked\">{{\n          inactiveText\n        }}</span>\n      </slot>\n    </span>\n    <span :class=\"ns.e('core')\" :style=\"coreStyle\">\n      <div v-if=\"inlinePrompt\" :class=\"ns.e('inner')\">\n        <div v-if=\"!checked\" :class=\"ns.e('inner-wrapper')\">\n          <slot name=\"inactive\">\n            <el-icon v-if=\"inactiveIcon\">\n              <component :is=\"inactiveIcon\" />\n            </el-icon>\n            <span v-if=\"!inactiveIcon && inactiveText\">{{ inactiveText }}</span>\n          </slot>\n        </div>\n        <div v-else :class=\"ns.e('inner-wrapper')\">\n          <slot name=\"active\">\n            <el-icon v-if=\"activeIcon\">\n              <component :is=\"activeIcon\" />\n            </el-icon>\n            <span v-if=\"!activeIcon && activeText\">{{ activeText }}</span>\n          </slot>\n        </div>\n      </div>\n      <div :class=\"ns.e('action')\">\n        <el-icon v-if=\"loading\" :class=\"ns.is('loading')\">\n          <loading />\n        </el-icon>\n        <slot v-else-if=\"checked\" name=\"active-action\">\n          <el-icon v-if=\"activeActionIcon\">\n            <component :is=\"activeActionIcon\" />\n          </el-icon>\n        </slot>\n        <slot v-else-if=\"!checked\" name=\"inactive-action\">\n          <el-icon v-if=\"inactiveActionIcon\">\n            <component :is=\"inactiveActionIcon\" />\n          </el-icon>\n        </slot>\n      </div>\n    </span>\n    <span\n      v-if=\"!inlinePrompt && (activeIcon || activeText || $slots.active)\"\n      :class=\"labelRightKls\"\n    >\n      <slot name=\"active\">\n        <el-icon v-if=\"activeIcon\">\n          <component :is=\"activeIcon\" />\n        </el-icon>\n        <span v-if=\"!activeIcon && activeText\" :aria-hidden=\"!checked\">{{\n          activeText\n        }}</span>\n      </slot>\n    </span>\n  </div>\n</template>\n\n<script lang=\"ts\" setup>\nimport { computed, nextTick, onMounted, ref, shallowRef, watch } from 'vue'\nimport {\n  addUnit,\n  debugWarn,\n  isBoolean,\n  isPromise,\n  throwError,\n} from '@element-plus/utils'\nimport ElIcon from '@element-plus/components/icon'\nimport {\n  useFormDisabled,\n  useFormItem,\n  useFormItemInputId,\n  useFormSize,\n} from '@element-plus/components/form'\nimport { Loading } from '@element-plus/icons-vue'\nimport {\n  CHANGE_EVENT,\n  INPUT_EVENT,\n  UPDATE_MODEL_EVENT,\n} from '@element-plus/constants'\nimport { useNamespace } from '@element-plus/hooks'\nimport { switchEmits } from './switch'\n\nimport type { CSSProperties } from 'vue'\nimport type { SwitchProps } from './switch'\n\nconst COMPONENT_NAME = 'ElSwitch'\ndefineOptions({\n  name: COMPONENT_NAME,\n})\n\nconst props = withDefaults(defineProps<SwitchProps>(), {\n  modelValue: false,\n  disabled: undefined,\n  activeText: '',\n  inactiveText: '',\n  activeValue: true,\n  inactiveValue: false,\n  name: '',\n  validateEvent: true,\n  width: '',\n})\nconst emit = defineEmits(switchEmits)\n\nconst { formItem } = useFormItem()\nconst switchSize = useFormSize()\nconst ns = useNamespace('switch')\n\nconst { inputId } = useFormItemInputId(props, {\n  formItemContext: formItem,\n})\n\nconst switchDisabled = useFormDisabled(\n  computed(() => {\n    if (props.loading) {\n      return true\n    }\n    return undefined\n  })\n)\nconst isControlled = ref(props.modelValue !== false)\nconst input = shallowRef<HTMLInputElement>()\n\nconst switchKls = computed(() => [\n  ns.b(),\n  ns.m(switchSize.value),\n  ns.is('disabled', switchDisabled.value),\n  ns.is('checked', checked.value),\n])\n\nconst labelLeftKls = computed(() => [\n  ns.e('label'),\n  ns.em('label', 'left'),\n  ns.is('active', !checked.value),\n])\n\nconst labelRightKls = computed(() => [\n  ns.e('label'),\n  ns.em('label', 'right'),\n  ns.is('active', checked.value),\n])\n\nconst coreStyle = computed<CSSProperties>(() => ({\n  width: addUnit(props.width),\n}))\n\nwatch(\n  () => props.modelValue,\n  () => {\n    isControlled.value = true\n  }\n)\n\nconst actualValue = computed(() => {\n  return isControlled.value ? props.modelValue : false\n})\n\nconst checked = computed(() => actualValue.value === props.activeValue)\n\nif (![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\nwatch(checked, (val) => {\n  input.value!.checked = val\n\n  if (props.validateEvent) {\n    formItem?.validate?.('change').catch((err) => debugWarn(err))\n  }\n})\n\nconst 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\nconst switchValue = () => {\n  if (switchDisabled.value) return\n\n  const { beforeChange } = props\n  if (!beforeChange) {\n    handleChange()\n    return\n  }\n\n  const shouldChange = beforeChange()\n\n  const isPromiseOrBool = [\n    isPromise(shouldChange),\n    isBoolean(shouldChange),\n  ].includes(true)\n  if (!isPromiseOrBool) {\n    throwError(\n      COMPONENT_NAME,\n      'beforeChange must return type `Promise<boolean>` or `boolean`'\n    )\n  }\n\n  if (isPromise(shouldChange)) {\n    shouldChange\n      .then((result) => {\n        if (result) {\n          handleChange()\n        }\n      })\n      .catch((e) => {\n        debugWarn(COMPONENT_NAME, `some error occurred: ${e}`)\n      })\n  } else if (shouldChange) {\n    handleChange()\n  }\n}\n\nconst focus = (): void => {\n  input.value?.focus?.()\n}\n\nonMounted(() => {\n  input.value!.checked = checked.value\n})\n\ndefineExpose({\n  /**\n   *  @description manual focus to the switch component\n   **/\n  focus,\n  /**\n   * @description whether Switch is checked\n   */\n  checked,\n})\n</script>\n"],"mappings":""}