mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-15 01:41:20 +08:00
dd19cae2bc
* refactor(components): popper composables - Refactor popper composables * updates * updates for tooltip * Updates for popper. TODO: fix controlled tooltip animation * Fix controlled mode popper animation issue * Add new feature for customizing tooltip theme * Fix popover and popconfirm error * - Add Collection component for wrapping a collection of component - Add FocusTrap component for trap focus for popups - Add RovingFocus component for roving focus component type - Adjust dropdown component based on these newly added components - Add popper-trigger component for placing the trigger - TODO: Finish current dropdown component, and all component's tests plus documents * Refactor popper * Complete organizing popper * Almost finish dropdown * Update popper tests * update only-child test * Finish focus trap component test * Finish tooltip content test * Finish tooltip trigger tests * Finish tooltip tests * finish tests for Collection and RovingFocusGroup * Fix test cases for timeselect & select & popover * Fix popover, popconfirm, menu bug and test cases * Fix select-v2 test error caused by updating popper * Fix date-picker test issue for updating popper * fix test cases * Fix eslint * Rebase dev & fix tests * Remove unused code
176 lines
4.0 KiB
TypeScript
176 lines
4.0 KiB
TypeScript
import { computed, getCurrentInstance, watch, onMounted } from 'vue'
|
|
import { isFunction } from '@vue/shared'
|
|
import { isClient } from '@vueuse/core'
|
|
import { isBool } from '@element-plus/utils/util'
|
|
import { buildProp, definePropType } from '@element-plus/utils/props'
|
|
import type { RouteLocationNormalizedLoaded } from 'vue-router'
|
|
|
|
import type { Ref, ComponentPublicInstance, ExtractPropTypes } from 'vue'
|
|
|
|
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),
|
|
}
|
|
|
|
const useModelToggleEmits = [`update:${name}`]
|
|
|
|
const useModelToggle = ({
|
|
indicator,
|
|
shouldHideWhenRouteChanges,
|
|
shouldProceed,
|
|
onShow,
|
|
onHide,
|
|
}: ModelToggleParams) => {
|
|
const instance = getCurrentInstance()!
|
|
const props = instance.props as UseModelToggleProps & { disabled: boolean }
|
|
const { emit } = instance
|
|
|
|
const updateEventKey = `update:${name}`
|
|
|
|
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
|
|
}
|
|
|
|
indicator.value = true
|
|
if (isFunction(onShow)) {
|
|
onShow()
|
|
}
|
|
}
|
|
|
|
const doHide = () => {
|
|
if (indicator.value === false) {
|
|
return
|
|
}
|
|
|
|
indicator.value = false
|
|
|
|
if (isFunction(onHide)) {
|
|
onHide()
|
|
}
|
|
}
|
|
|
|
const show = () => {
|
|
if (
|
|
props.disabled === true ||
|
|
(isFunction(shouldProceed) && !shouldProceed())
|
|
)
|
|
return
|
|
|
|
const shouldEmit = hasUpdateHandler.value && isClient
|
|
|
|
if (shouldEmit) {
|
|
emit(updateEventKey, true)
|
|
}
|
|
|
|
if (isModelBindingAbsent.value || !shouldEmit) {
|
|
doShow()
|
|
}
|
|
}
|
|
|
|
const hide = () => {
|
|
if (props.disabled === true || !isClient) return
|
|
|
|
const shouldEmit = hasUpdateHandler.value && isClient
|
|
|
|
if (shouldEmit) {
|
|
emit(updateEventKey, false)
|
|
}
|
|
|
|
if (isModelBindingAbsent.value || !shouldEmit) {
|
|
doHide()
|
|
}
|
|
}
|
|
|
|
const onChange = (val: boolean) => {
|
|
if (!isBool(val)) return
|
|
if (props.disabled && val) {
|
|
if (hasUpdateHandler.value) {
|
|
emit(updateEventKey, false)
|
|
}
|
|
} else if (indicator.value !== val) {
|
|
if (val) {
|
|
doShow()
|
|
} else {
|
|
doHide()
|
|
}
|
|
}
|
|
}
|
|
|
|
const toggle = () => {
|
|
if (indicator.value) {
|
|
hide()
|
|
} else {
|
|
show()
|
|
}
|
|
}
|
|
|
|
watch(() => props[name], onChange as any)
|
|
|
|
if (
|
|
shouldHideWhenRouteChanges &&
|
|
instance.appContext.config.globalProperties.$route !== undefined
|
|
) {
|
|
watch(
|
|
() => ({
|
|
...(
|
|
instance.proxy as ComponentPublicInstance<{
|
|
$route: RouteLocationNormalizedLoaded
|
|
}>
|
|
).$route,
|
|
}),
|
|
() => {
|
|
if (shouldHideWhenRouteChanges.value && indicator.value) {
|
|
hide()
|
|
}
|
|
}
|
|
)
|
|
}
|
|
|
|
onMounted(() => {
|
|
onChange(props[name] as boolean)
|
|
})
|
|
|
|
return {
|
|
hide,
|
|
show,
|
|
toggle,
|
|
}
|
|
}
|
|
|
|
return {
|
|
useModelToggle,
|
|
useModelToggleProps,
|
|
useModelToggleEmits,
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|