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 line
8.9 KiB

{"version":3,"file":"index.js","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 inst