2021-07-03 22:12:39 +08:00
|
|
|
import { computed, getCurrentInstance, watch, onMounted } from 'vue'
|
|
|
|
import { isFunction } from '@vue/shared'
|
2021-12-12 23:28:03 +08:00
|
|
|
import { isClient } from '@vueuse/core'
|
2022-02-11 11:03:15 +08:00
|
|
|
import { isBoolean, buildProp, definePropType } 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
|
|
|
|
2021-11-29 15:58:44 +08:00
|
|
|
import type { Ref, ComponentPublicInstance, ExtractPropTypes } from 'vue'
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
export const createModelToggleComposable = (name: string) => {
|
|
|
|
const useModelToggleProps = {
|
|
|
|
[name]: buildProp({
|
|
|
|
type: definePropType<boolean | null>(Boolean),
|
|
|
|
default: null,
|
|
|
|
} as const),
|
|
|
|
[`onUpdate:${name}`]: buildProp({
|
|
|
|
type: definePropType<(val: boolean) => void>(Function),
|
|
|
|
} as const),
|
|
|
|
}
|
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,
|
|
|
|
shouldHideWhenRouteChanges,
|
|
|
|
shouldProceed,
|
|
|
|
onShow,
|
|
|
|
onHide,
|
|
|
|
}: ModelToggleParams) => {
|
|
|
|
const instance = getCurrentInstance()!
|
|
|
|
const props = instance.props as UseModelToggleProps & { disabled: boolean }
|
|
|
|
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)
|
|
|
|
|
|
|
|
const doShow = () => {
|
|
|
|
if (indicator.value === true) {
|
|
|
|
return
|
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
indicator.value = true
|
|
|
|
if (isFunction(onShow)) {
|
|
|
|
onShow()
|
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
}
|
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
const doHide = () => {
|
|
|
|
if (indicator.value === false) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
indicator.value = false
|
2021-07-03 22:12:39 +08:00
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
if (isFunction(onHide)) {
|
|
|
|
onHide()
|
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
}
|
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
const show = () => {
|
|
|
|
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) {
|
|
|
|
doShow()
|
|
|
|
}
|
2021-07-03 22:12:39 +08:00
|
|
|
}
|
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
const hide = () => {
|
|
|
|
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) {
|
|
|
|
doHide()
|
|
|
|
}
|
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>
|
|
|
|
shouldHideWhenRouteChanges?: Ref<boolean>
|
|
|
|
shouldProceed?: () => boolean
|
|
|
|
onShow?: () => void
|
|
|
|
onHide?: () => void
|
|
|
|
}
|