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.

1 lines
12 KiB

{"ast":null,"code":"import { getCurrentInstance, computed, watch, onMounted } from 'vue';\nimport { buildProp, definePropType } from '../../utils/vue/props/runtime.mjs';\nimport { isBoolean } from '../../utils/types.mjs';\nimport { isFunction } from '@vue/shared';\nimport { isClient } from '@vueuse/core';\nconst _prop = buildProp({\n type: definePropType(Boolean),\n default: null\n});\nconst _event = buildProp({\n type: definePropType(Function)\n});\nconst createModelToggleComposable = name => {\n const updateEventKey = `update:${name}`;\n const updateEventKeyRaw = `onUpdate:${name}`;\n const useModelToggleEmits2 = [updateEventKey];\n const useModelToggleProps2 = {\n [name]: _prop,\n [updateEventKeyRaw]: _event\n };\n const useModelToggle2 = ({\n indicator,\n toggleReason,\n shouldHideWhenRouteChanges,\n shouldProceed,\n onShow,\n onHide\n }) => {\n const instance = getCurrentInstance();\n const {\n emit\n } = instance;\n const props = instance.props;\n const hasUpdateHandler = computed(() => isFunction(props[updateEventKeyRaw]));\n const isModelBindingAbsent = computed(() => props[name] === null);\n const doShow = event => {\n if (indicator.value === true) {\n return;\n }\n indicator.value = true;\n if (toggleReason) {\n toggleReason.value = event;\n }\n if (isFunction(onShow)) {\n onShow(event);\n }\n };\n const doHide = event => {\n if (indicator.value === false) {\n return;\n }\n indicator.value = false;\n if (toggleReason) {\n toggleReason.value = event;\n }\n if (isFunction(onHide)) {\n onHide(event);\n }\n };\n const show = event => {\n if (props.disabled === true || isFunction(shouldProceed) && !shouldProceed()) return;\n const shouldEmit = hasUpdateHandler.value && isClient;\n if (shouldEmit) {\n emit(updateEventKey, true);\n }\n if (isModelBindingAbsent.value || !shouldEmit) {\n doShow(event);\n }\n };\n const hide = event => {\n if (props.disabled === true || !isClient) return;\n const shouldEmit = hasUpdateHandler.value && isClient;\n if (shouldEmit) {\n emit(updateEventKey, false);\n }\n if (isModelBindingAbsent.value || !shouldEmit) {\n doHide(event);\n }\n };\n const onChange = val => {\n if (!isBoolean(val)) return;\n if (props.disabled && val) {\n if (hasUpdateHandler.value) {\n emit(updateEventKey, false);\n }\n } else if (indicator.value !== val) {\n if (val) {\n doShow();\n } else {\n doHide();\n }\n }\n };\n const toggle = () => {\n if (indicator.value) {\n hide();\n } else {\n show();\n }\n };\n watch(() => props[name], onChange);\n if (shouldHideWhenRouteChanges && instance.appContext.config.globalProperties.$route !== void 0) {\n watch(() => ({\n ...instance.proxy.$route\n }), () => {\n if (shouldHideWhenRouteChanges.value && indicator.value) {\n hide();\n }\n });\n }\n onMounted(() => {\n onChange(props[name]);\n });\n return {\n hide,\n show,\n toggle,\n hasUpdateHandler\n };\n };\n return {\n useModelToggle: useModelToggle2,\n useModelToggleProps: useModelToggleProps2,\n useModelToggleEmits: useModelToggleEmits2\n };\n};\nconst {\n useModelToggle,\n useModelToggleProps,\n useModelToggleEmits\n} = createModelToggleComposable(\"modelValue\");\nexport { createModelToggleComposable, useModelToggle, useModelToggleEmits, useModelToggleProps };","map":{"version":3,"names":["_prop","buildProp","type","definePropType","Boolean","default","_event","Function","createModelToggleComposable","name","updateEventKey","updateEventKeyRaw","useModelToggleEmits2","useModelToggleProps2","useModelToggle2","indicator","toggleReason","shouldHideWhenRouteChanges","shouldProceed","onShow","onHide","instance","getCurrentInstance","emit","props","hasUpdateHandler","computed","isFunction","isModelBindingAbsent","doShow","event","value","doHide","show","disabled","shouldEmit","isClient","hide","onChange","val","isBoolean","toggle","watch","appContext","config","globalProperties","$route","proxy","onMounted","useModelToggle","useModelToggleProps","useModelToggleEmits"],"sources":["../../../../../packages/hooks/use-model-toggle/index.ts"],"sourcesContent":["import { computed, getCurrentInstance, onMounted, watch } from 'vue'\nimport {\n buildProp,\n definePropType,\n isBoolean,\n isClient,\n isFunction,\n} from '@element-plus/utils'\nimport type { ExtractPropType } from '@element-plus/utils'\nimport type { RouteLocationNormalizedLoaded } from 'vue-router'\n\nimport type { ComponentPublicInstance, ExtractPropTypes, Ref } from 'vue'\n\nconst _prop = buildProp({\n type: definePropType<boolean | null>(Boolean),\n default: null,\n} as const)\nconst _event = buildProp({\n type: definePropType<(val: boolean) => void>(Function),\n} as const)\n\nexport type UseModelTogglePropsRaw<T extends string> = {\n [K in T]: typeof _prop\n} & {\n [K in `onUpdate:${T}`]: typeof _event\n}\n\nexport type UseModelTogglePropsGeneric<T extends string> = {\n [K in T]: ExtractPropType<typeof _prop>\n} & {\n [K in `onUpdate:${T}`]: ExtractPropType<typeof _event>\n}\n\nexport const createModelToggleComposable = <T extends string>(name: T) => {\n const updateEventKey = `update:${name}` as const\n const updateEventKeyRaw = `onUpdate:${name}` as const\n const useModelToggleEmits = [updateEventKey]\n\n const useModelToggleProps = {\n [name]: _prop,\n [updateEventKeyRaw]: _event,\n } as UseModelTogglePropsRaw<T>\n\n const useModelToggle = ({\n indicator,\n toggleReason,\n shouldHideWhenRouteChanges,\n shouldProceed,\n onShow,\n onHide,\n }: ModelToggleParams) => {\n const instance = getCurrentInstance()!\n const { emit } = instance\n const props = instance.props as UseModelTogglePropsGeneric<T> & {\n disabled: boolean\n }\n const hasUpdateHandler = computed(() =>\n isFunction(props[updateEventKeyRaw])\n )\n // when it matches the default value we say this is absent\n // though this could be mistakenly passed from the user but we need to rule out that\n // condition\n const isModelBindingAbsent = computed(() => props[name] === null)\n\n const doShow = (event?: Event) => {\n if (indicator.value === true) {\n return\n }\n\n indicator.value = true\n if (toggleReason) {\n toggleReason.value = event\n }\n if (isFunction(onShow)) {\n onShow(event)\n }\n }\n\n const doHide = (event?: Event) => {\n if (indicator.value === false) {\n return\n }\n\n indicator.value = false\n if (toggleReason) {\n toggleReason.value = event\n }\n if (isFunction(onHide)) {\n onHide(event)\n }\n }\n\n const show = (event?: Event) => {\n if (\n props.disabled === true ||\n (isFunction(shouldProceed) && !shouldProceed())\n )\n return\n\n const shouldEmit = hasUpdateHandler.value && isClient\n\n if (shouldEmit) {\n emit(updateEventKey, true)\n }\n\n if (isModelBindingAbsent.value || !shouldEmit) {\n doShow(event)\n }\n }\n\n const hide = (event?: Event) => {\n if (props.disabled === true || !isClient) return\n\n const shouldEmit = hasUpdateHandler.value && isClient\n\n if (shouldEmit) {\n emit(updateEventKey, false)\n }\n\n if (isModelBindingAbsent.value || !shouldEmit) {\n doHide(event)\n }\n }\n\n const onChange = (val: boolean) => {\n if (!isBoolean(val)) return\n if (props.disabled && val) {\n if (hasUpdateHandler.value) {\n emit(updateEventKey, false)\n }\n } else if (indicator.value !== val) {\n if (val) {\n doShow()\n } else {\n doHide()\n }\n }\n }\n\n const toggle = () => {\n if (indicator.value) {\n hide()\n } else {\n show()\n }\n }\n\n watch(() => props[name], onChange)\n\n if (\n shouldHideWhenRouteChanges &&\n instance.appContext.config.globalProperties.$route !== undefined\n ) {\n watch(\n () => ({\n ...(\n instance.proxy as ComponentPublicInstance<{\n $route: RouteLocationNormalizedLoaded\n }>\n ).$route,\n }),\n () => {\n if (shouldHideWhenRouteChanges.value && indicator.value) {\n hide()\n }\n }\n )\n }\n\n onMounted(() => {\n onChange(props[name])\n })\n\n return {\n hide,\n show,\n toggle,\n hasUpdateHandler,\n }\n }\n\n return {\n useModelToggle,\n useModelToggleProps,\n useModelToggleEmits,\n }\n}\n\nconst { useModelToggle, useModelToggleProps, useModelToggleEmits } =\n createModelToggleComposable('modelValue')\n\nexport { useModelToggle, useModelToggleEmits, useModelToggleProps }\n\nexport type UseModelToggleProps = ExtractPropTypes<typeof useModelToggleProps>\n\nexport type ModelToggleParams = {\n indicator: Ref<boolean>\n toggleReason?: Ref<Event | undefined>\n shouldHideWhenRouteChanges?: Ref<boolean>\n shouldProceed?: () => boolean\n onShow?: (event?: Event) => void\n onHide?: (event?: Event) => void\n}\n"],"mappings":";;;;;AAQA,MAAMA,KAAK,GAAGC,SAAS,CAAC;EACtBC,IAAI,EAAEC,cAAc,CAACC,OAAO,CAAC;EAC7BC,OAAO,EAAE;AACX,CAAC,CAAC;AACF,MAAMC,MAAM,GAAGL,SAAS,CAAC;EACvBC,IAAI,EAAEC,c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