antd-tiny-vue/components/_util/wave/index.tsx

61 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-03-18 20:48:10 +08:00
import type { VNode } from 'vue'
import { cloneVNode, computed, defineComponent, shallowRef } from 'vue'
import { booleanType, classNames, filterEmpty, isVisible } from '@v-c/utils'
import { useEventListener } from '@vueuse/core'
import { useProviderConfigState } from '../../config-provider/context'
import useStyle from './style'
import useWave from './use-wave'
2023-03-18 18:17:20 +08:00
const Wave = defineComponent({
name: 'Wave',
2023-03-18 20:48:10 +08:00
props: {
disabled: booleanType()
},
setup(props, { slots }) {
const { getPrefixCls } = useProviderConfigState()
const containerRef = shallowRef()
// ============================== Style ===============================
const prefixCls = computed(() => getPrefixCls('wave'))
const [, hashId] = useStyle(prefixCls)
const showWave = useWave(
containerRef,
2023-03-18 21:16:41 +08:00
computed(() => classNames(prefixCls.value, hashId.value))
2023-03-18 20:48:10 +08:00
)
const onClick = (e: MouseEvent) => {
const node = containerRef.value
const { disabled } = props
if (!node || node.nodeType !== 1 || disabled) {
return
}
// Fix radio button click twice
if (
(e.target as HTMLElement).tagName === 'INPUT' ||
!isVisible(e.target as HTMLElement) ||
// No need wave
!node.getAttribute ||
node.getAttribute('disabled') ||
(node as HTMLInputElement).disabled ||
node.className.includes('disabled') ||
node.className.includes('-leave')
) {
return
}
showWave()
}
useEventListener(containerRef, 'click', onClick, true)
2023-03-18 18:17:20 +08:00
return () => {
2023-03-18 20:48:10 +08:00
const children = slots.default?.()
const child = filterEmpty(children)[0]
if (!child) return null
return cloneVNode(child as VNode, { ref: containerRef })
2023-03-18 18:17:20 +08:00
}
}
})
export default Wave