From 60e96310962c192c530ce6bf56190a29a1b6faa7 Mon Sep 17 00:00:00 2001 From: hxh2010 Date: Fri, 3 Jan 2025 11:02:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20[tooltip]onVisibleChange=E6=96=B0?= =?UTF-8?q?=E5=A2=9Epayload=E5=8F=82=E6=95=B0=20fix=20#1044?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/popper/index.tsx | 41 +++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/components/popper/index.tsx b/components/popper/index.tsx index 42c34e47..9cb52b1a 100644 --- a/components/popper/index.tsx +++ b/components/popper/index.tsx @@ -41,7 +41,7 @@ export const Triggers = tuple('hover', 'focus', 'click', 'contextMenu') export type TriggerType = typeof Triggers[number] -export type Reason = +export type IReason = | TriggerType | 'scroll' | 'clickOutside' @@ -88,7 +88,7 @@ export type PopperProps = { strategy?: 'fixed' | 'absolute' clickToClose?: boolean onTrigger?: (trigger: TriggerType) => void - onVisibleChange?: (visible: boolean, reason?: Reason) => void + onVisibleChange?: (visible: boolean, reason?: IReason, payload?: IPayload) => void getTriggerElement?: (locatorNode: HTMLElement) => HTMLElement getPopupContainer?: (locatorNode: HTMLElement) => HTMLElement onTransitionEnd?: (e: React.TransitionEvent) => void @@ -179,8 +179,16 @@ const getElement = (element: RenderType) => { export type SubPopup = { dom: HTMLElement | null - triggerOpen: (v: boolean, reason?: Reason) => void + triggerOpen: (nextOpen: boolean, triggerType?: IReason, delay?: number, payload?: IPayload) => void visible: boolean + subPopupRefs: React.MutableRefObject> +} + +export type IPayload = { + popperDom?: HTMLElement | false + referenceDom?: HTMLElement | false + event?: MouseEvent + subPopupRefs?: SubPopup['subPopupRefs'] } export const Popper = forwardRef((props, ref) => { @@ -289,7 +297,7 @@ export const Popper = forwardRef((props, ref) => { delayRef.current = null } } - const changeVisible = (nextOpen: boolean, triggerType: Reason = 'unknown') => { + const changeVisible = (nextOpen: boolean, triggerType: IReason = 'unknown', payload: IPayload) => { if (visibleInner !== nextOpen) { if (nextOpen && triggerTypeArray.includes(triggerType)) { onTrigger?.(triggerType as TriggerType) @@ -297,25 +305,30 @@ export const Popper = forwardRef((props, ref) => { if (typeof visible === 'undefined') { setVisibleInner(nextOpen) } - onVisibleChangeRef.current?.(nextOpen, triggerType) + onVisibleChangeRef.current?.(nextOpen, triggerType, payload) } if (!nextOpen && Object.keys(subPopupRefs.current || {}).length) { Object.values(subPopupRefs.current).forEach((d: SubPopup) => { if (typeof d?.triggerOpen === 'function' && d?.visible) { - d?.triggerOpen(false, 'parentHidden') + d?.triggerOpen(false, 'parentHidden', 0) } }) } } - const triggerOpen = (nextOpen: boolean, triggerType: Reason = 'unknown', delay = 0) => { + const triggerOpen: SubPopup['triggerOpen'] = ( + nextOpen: boolean, + triggerType = 'unknown', + delay = 0, + payload = {}, + ) => { clearDelay() if (!disabled) { if (delay === 0) { - changeVisible(nextOpen, triggerType) + changeVisible(nextOpen, triggerType, payload) } else { if (visibleInner !== nextOpen) { delayRef.current = setTimeout(() => { - changeVisible(nextOpen, triggerType) + changeVisible(nextOpen, triggerType, payload) }, delay * 1000) } } @@ -340,7 +353,7 @@ export const Popper = forwardRef((props, ref) => { return false } - const onTriggerInner = (nextOpen: boolean, triggerType: Reason, delay: undefined | number = undefined) => { + const onTriggerInner = (nextOpen: boolean, triggerType: IReason, delay: undefined | number = undefined) => { triggerOpen(nextOpen, triggerType, delay) } @@ -475,7 +488,12 @@ export const Popper = forwardRef((props, ref) => { : false const isTarget = isPopper || isReference if (!isTarget && !isSubPopper(e)) { - triggerOpen(false, 'clickOutside', 0) + triggerOpen(false, 'clickOutside', 0, { + popperDom: popperRefDom.current || false, + referenceDom: domReference || false, + event: e, + subPopupRefs, + }) } if (clickToClose && isReference && trigger !== 'focus' && trigger !== 'click') { @@ -512,6 +530,7 @@ export const Popper = forwardRef((props, ref) => { dom: popperRefDom.current, triggerOpen, visible: visibleInner, + subPopupRefs, } })