2022-03-25 15:35:56 +08:00
|
|
|
import { computed, getCurrentInstance, onMounted, watch } from 'vue'
|
2021-07-03 22:12:39 +08:00
|
|
|
import { isFunction } from '@vue/shared'
|
2021-12-12 23:28:03 +08:00
|
|
|
import { isClient } from '@vueuse/core'
|
2022-03-25 15:35:56 +08:00
|
|
|
import { buildProp, definePropType, isBoolean } from '@element-plus/utils'
|
2021-11-29 15:58:44 +08:00
|
|
|
import type { RouteLocationNormalizedLoaded } from 'vue-router'
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-03-25 15:35:56 +08:00
|
|
|
import type { ComponentPublicInstance, ExtractPropTypes, Ref } from 'vue'
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
const _prop = buildProp({
|
|
|
|
type: definePropType<boolean | null>(Boolean),
|
|
|
|
default: null,
|
|
|
|
} as const)
|
|
|
|
const _event = buildProp({
|
|
|
|
type: definePropType<(val: boolean) => void>(Function),
|
|
|
|
} as const)
|
|
|
|
|
|
|
|
type _UseModelToggleProps<T extends string> = {
|
|
|
|
[K in T]: typeof _prop
|
|
|
|
} & {
|
|
|
|
[K in `onUpdate:${T}`]: typeof _event
|
|
|
|
}
|
|
|
|
|
|
|
|
export const createModelToggleComposable = <T extends string>(name: T) => {
|
2022-01-04 09:15:15 +08:00
|
|
|
const useModelToggleProps = {
|
2022-02-21 14:28:22 +08:00
|
|
|
[name]: _prop,
|
|
|
|
[`onUpdate:${name}`]: _event,
|
|
|
|
} as _UseModelToggleProps<T>
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
const useModelToggleEmits = [`update:${name}`]
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
const useModelToggle = ({
|
|
|
|
indicator,
|
2022-05-30 11:51:25 +08:00
|
|
|
toggleReason,
|
2022-01-04 09:15:15 +08:00
|
|
|
shouldHideWhenRouteChanges,
|
|
|
|
shouldProceed,
|
|
|
|
onShow,
|
|
|
|
onHide,
|
|
|
|
}: ModelToggleParams) => {
|
|
|
|
const instance = getCurrentInstance()!
|
2022-02-21 14:28:22 +08:00
|
|
|
const props = instance.props as _UseModelToggleProps<T> & {
|
|
|
|
disabled: boolean
|
|
|
|
}
|
2022-01-04 09:15:15 +08:00
|
|
|
const { emit } = instance
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
const updateEventKey = `update:${name}`
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
const hasUpdateHandler = computed(() =>
|
|
|
|
isFunction(props[`onUpdate:${name}`])
|
|
|
|
)
|
|
|
|
// when it matches the default value we say this is absent
|
|
|
|
// though this could be mistakenly passed from the user but we need to rule out that
|
|
|
|
// condition
|
|
|
|
const isModelBindingAbsent = computed(() => props[name] === null)
|
|
|
|
|
2022-05-30 11:51:25 +08:00
|
|
|
const doShow = (event?: Event) => {
|
2022-01-04 09:15:15 +08:00
|
|
|
if (indicator.value === true) {
|
|
|
|
return
|
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
indicator.value = true
|
2022-05-30 11:51:25 +08:00
|
|
|
if (toggleReason) {
|
|
|
|
toggleReason.value = event
|
|
|
|
}
|
2022-01-04 09:15:15 +08:00
|
|
|
if (isFunction(onShow)) {
|
2022-05-30 11:51:25 +08:00
|
|
|
onShow(event)
|
2022-01-04 09:15:15 +08:00
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
}
|
|
|
|
|
2022-05-30 11:51:25 +08:00
|
|
|
const doHide = (event?: Event) => {
|
2022-01-04 09:15:15 +08:00
|
|
|
if (indicator.value === false) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
indicator.value = false
|
2022-05-30 11:51:25 +08:00
|
|
|
if (toggleReason) {
|
|
|
|
toggleReason.value = event
|
|
|
|
}
|
2022-01-04 09:15:15 +08:00
|
|
|
if (isFunction(onHide)) {
|
2022-05-30 11:51:25 +08:00
|
|
|
onHide(event)
|
2022-01-04 09:15:15 +08:00
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
}
|
|
|
|
|
2022-05-30 11:51:25 +08:00
|
|
|
const show = (event?: Event) => {
|
2022-01-04 09:15:15 +08:00
|
|
|
if (
|
|
|
|
props.disabled === true ||
|
|
|
|
(isFunction(shouldProceed) && !shouldProceed())
|
|
|
|
)
|
|
|
|
return
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
const shouldEmit = hasUpdateHandler.value && isClient
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
if (shouldEmit) {
|
|
|
|
emit(updateEventKey, true)
|
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
if (isModelBindingAbsent.value || !shouldEmit) {
|
2022-05-30 11:51:25 +08:00
|
|
|
doShow(event)
|
2022-01-04 09:15:15 +08:00
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
}
|
|
|
|
|
2022-05-30 11:51:25 +08:00
|
|
|
const hide = (event?: Event) => {
|
2022-01-04 09:15:15 +08:00
|
|
|
if (props.disabled === true || !isClient) return
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
const shouldEmit = hasUpdateHandler.value && isClient
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
if (shouldEmit) {
|
|
|
|
emit(updateEventKey, false)
|
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
if (isModelBindingAbsent.value || !shouldEmit) {
|
2022-05-30 11:51:25 +08:00
|
|
|
doHide(event)
|
2022-01-04 09:15:15 +08:00
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
}
|
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
const onChange = (val: boolean) => {
|
2022-02-09 16:59:08 +08:00
|
|
|
if (!isBoolean(val)) return
|
2022-01-04 09:15:15 +08:00
|
|
|
if (props.disabled && val) {
|
|
|
|
if (hasUpdateHandler.value) {
|
|
|
|
emit(updateEventKey, false)
|
|
|
|
}
|
|
|
|
} else if (indicator.value !== val) {
|
|
|
|
if (val) {
|
|
|
|
doShow()
|
|
|
|
} else {
|
|
|
|
doHide()
|
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
}
|
2022-01-04 09:15:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const toggle = () => {
|
|
|
|
if (indicator.value) {
|
|
|
|
hide()
|
2021-07-03 22:12:39 +08:00
|
|
|
} else {
|
2022-01-04 09:15:15 +08:00
|
|
|
show()
|
2021-07-03 22:12:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
watch(() => props[name], onChange as any)
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
if (
|
|
|
|
shouldHideWhenRouteChanges &&
|
|
|
|
instance.appContext.config.globalProperties.$route !== undefined
|
|
|
|
) {
|
|
|
|
watch(
|
|
|
|
() => ({
|
|
|
|
...(
|
|
|
|
instance.proxy as ComponentPublicInstance<{
|
|
|
|
$route: RouteLocationNormalizedLoaded
|
|
|
|
}>
|
|
|
|
).$route,
|
|
|
|
}),
|
|
|
|
() => {
|
|
|
|
if (shouldHideWhenRouteChanges.value && indicator.value) {
|
|
|
|
hide()
|
|
|
|
}
|
2021-09-04 19:29:28 +08:00
|
|
|
}
|
2022-01-04 09:15:15 +08:00
|
|
|
)
|
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
onMounted(() => {
|
|
|
|
onChange(props[name] as boolean)
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
hide,
|
|
|
|
show,
|
|
|
|
toggle,
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
|
|
|
|
return {
|
2022-01-04 09:15:15 +08:00
|
|
|
useModelToggle,
|
|
|
|
useModelToggleProps,
|
|
|
|
useModelToggleEmits,
|
2021-07-03 22:12:39 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-04 09:15:15 +08:00
|
|
|
|
|
|
|
const { useModelToggle, useModelToggleProps, useModelToggleEmits } =
|
|
|
|
createModelToggleComposable('modelValue')
|
|
|
|
|
|
|
|
export { useModelToggle, useModelToggleEmits, useModelToggleProps }
|
|
|
|
|
|
|
|
export type UseModelToggleProps = ExtractPropTypes<typeof useModelToggleProps>
|
|
|
|
|
|
|
|
export type ModelToggleParams = {
|
|
|
|
indicator: Ref<boolean>
|
2022-05-30 11:51:25 +08:00
|
|
|
toggleReason?: Ref<Event | undefined>
|
2022-01-04 09:15:15 +08:00
|
|
|
shouldHideWhenRouteChanges?: Ref<boolean>
|
|
|
|
shouldProceed?: () => boolean
|
2022-05-30 11:51:25 +08:00
|
|
|
onShow?: (event?: Event) => void
|
|
|
|
onHide?: (event?: Event) => void
|
2022-01-04 09:15:15 +08:00
|
|
|
}
|