|
1 | | -import React, { useMemo, useState } from 'react'; |
| 1 | +import type { DElementSelector } from '../../hooks/element'; |
| 2 | + |
| 3 | +import { isUndefined } from 'lodash'; |
| 4 | +import React, { useEffect, useMemo, useState } from 'react'; |
2 | 5 |
|
3 | 6 | import { useAsync } from '../../hooks'; |
| 7 | +import { useElement } from '../../hooks/element'; |
4 | 8 |
|
5 | 9 | export type DTriggerType = 'hover' | 'focus' | 'click'; |
6 | 10 |
|
7 | 11 | export interface DTriggerProps { |
8 | 12 | dTrigger?: DTriggerType | DTriggerType[]; |
9 | 13 | dMouseEnterDelay?: number; |
10 | 14 | dMouseLeaveDelay?: number; |
11 | | - children: React.ReactNode; |
| 15 | + dTriggerNode?: DElementSelector; |
| 16 | + children?: React.ReactNode; |
12 | 17 | onTrigger?: (state?: boolean) => void; |
13 | 18 | } |
14 | 19 |
|
15 | 20 | export function DTrigger(props: DTriggerProps) { |
16 | | - const { dTrigger, dMouseEnterDelay = 150, dMouseLeaveDelay = 200, children, onTrigger } = props; |
| 21 | + const { dTrigger, dMouseEnterDelay = 150, dMouseLeaveDelay = 200, dTriggerNode, children, onTrigger } = props; |
17 | 22 |
|
18 | 23 | const [currentData] = useState<{ tid: number | null }>({ |
19 | 24 | tid: null, |
20 | 25 | }); |
21 | 26 |
|
22 | 27 | const asyncCapture = useAsync(); |
23 | 28 |
|
24 | | - const child = useMemo(() => { |
25 | | - const _child = React.Children.only(children) as React.ReactElement<React.HTMLAttributes<HTMLElement>>; |
26 | | - return React.cloneElement<React.HTMLAttributes<HTMLElement>>(_child, { |
27 | | - ..._child.props, |
28 | | - onMouseEnter: (e) => { |
29 | | - _child.props.onMouseEnter?.(e); |
30 | | - |
31 | | - if (dTrigger === 'hover') { |
32 | | - currentData.tid && asyncCapture.clearTimeout(currentData.tid); |
33 | | - currentData.tid = asyncCapture.setTimeout(() => { |
34 | | - currentData.tid = null; |
35 | | - onTrigger?.(true); |
36 | | - }, dMouseEnterDelay); |
37 | | - } |
38 | | - }, |
39 | | - onMouseLeave: (e) => { |
40 | | - _child.props.onMouseLeave?.(e); |
| 29 | + const triggerEl = useElement(dTriggerNode ?? null); |
41 | 30 |
|
| 31 | + //#region DidUpdate |
| 32 | + useEffect(() => { |
| 33 | + if (!isUndefined(dTriggerNode)) { |
| 34 | + const [asyncGroup, asyncId] = asyncCapture.createGroup(); |
| 35 | + if (triggerEl.current) { |
42 | 36 | if (dTrigger === 'hover') { |
43 | | - currentData.tid && asyncCapture.clearTimeout(currentData.tid); |
44 | | - currentData.tid = asyncCapture.setTimeout(() => { |
45 | | - currentData.tid = null; |
46 | | - onTrigger?.(false); |
47 | | - }, dMouseLeaveDelay); |
| 37 | + asyncGroup.fromEvent(triggerEl.current, 'mouseenter').subscribe({ |
| 38 | + next: () => { |
| 39 | + currentData.tid && asyncCapture.clearTimeout(currentData.tid); |
| 40 | + currentData.tid = asyncCapture.setTimeout(() => { |
| 41 | + currentData.tid = null; |
| 42 | + onTrigger?.(true); |
| 43 | + }, dMouseEnterDelay); |
| 44 | + }, |
| 45 | + }); |
| 46 | + asyncGroup.fromEvent(triggerEl.current, 'mouseleave').subscribe({ |
| 47 | + next: () => { |
| 48 | + currentData.tid && asyncCapture.clearTimeout(currentData.tid); |
| 49 | + currentData.tid = asyncCapture.setTimeout(() => { |
| 50 | + currentData.tid = null; |
| 51 | + onTrigger?.(false); |
| 52 | + }, dMouseLeaveDelay); |
| 53 | + }, |
| 54 | + }); |
48 | 55 | } |
49 | | - }, |
50 | | - onFocus: (e) => { |
51 | | - _child.props.onFocus?.(e); |
52 | 56 |
|
53 | 57 | if (dTrigger === 'focus') { |
54 | | - currentData.tid && asyncCapture.clearTimeout(currentData.tid); |
55 | | - onTrigger?.(true); |
| 58 | + asyncGroup.fromEvent(triggerEl.current, 'focus').subscribe({ |
| 59 | + next: () => { |
| 60 | + currentData.tid && asyncCapture.clearTimeout(currentData.tid); |
| 61 | + onTrigger?.(true); |
| 62 | + }, |
| 63 | + }); |
| 64 | + asyncGroup.fromEvent(triggerEl.current, 'blur').subscribe({ |
| 65 | + next: () => { |
| 66 | + currentData.tid = asyncCapture.setTimeout(() => onTrigger?.(false), 20); |
| 67 | + }, |
| 68 | + }); |
56 | 69 | } |
57 | | - }, |
58 | | - onBlur: (e) => { |
59 | | - _child.props.onBlur?.(e); |
60 | | - |
61 | | - if (dTrigger === 'focus') { |
62 | | - currentData.tid = asyncCapture.setTimeout(() => onTrigger?.(false), 20); |
63 | | - } |
64 | | - }, |
65 | | - onClick: (e) => { |
66 | | - _child.props.onClick?.(e); |
67 | 70 |
|
68 | 71 | if (dTrigger === 'click') { |
69 | | - currentData.tid && asyncCapture.clearTimeout(currentData.tid); |
70 | | - onTrigger?.(); |
| 72 | + asyncGroup.fromEvent(triggerEl.current, 'click').subscribe({ |
| 73 | + next: () => { |
| 74 | + currentData.tid && asyncCapture.clearTimeout(currentData.tid); |
| 75 | + onTrigger?.(); |
| 76 | + }, |
| 77 | + }); |
71 | 78 | } |
72 | | - }, |
73 | | - }); |
74 | | - }, [asyncCapture, children, currentData, dMouseEnterDelay, dMouseLeaveDelay, dTrigger, onTrigger]); |
| 79 | + } |
| 80 | + |
| 81 | + return () => { |
| 82 | + asyncCapture.deleteGroup(asyncId); |
| 83 | + }; |
| 84 | + } |
| 85 | + }, [asyncCapture, currentData, dMouseEnterDelay, dMouseLeaveDelay, dTrigger, dTriggerNode, onTrigger, triggerEl]); |
| 86 | + //#endregion |
| 87 | + |
| 88 | + const child = useMemo(() => { |
| 89 | + if (isUndefined(dTriggerNode)) { |
| 90 | + const _child = React.Children.only(children) as React.ReactElement<React.HTMLAttributes<HTMLElement>>; |
| 91 | + let childProps: React.HTMLAttributes<HTMLElement> = {}; |
| 92 | + |
| 93 | + if (dTrigger === 'hover') { |
| 94 | + childProps = { |
| 95 | + onMouseEnter: (e) => { |
| 96 | + _child.props.onMouseEnter?.(e); |
| 97 | + |
| 98 | + currentData.tid && asyncCapture.clearTimeout(currentData.tid); |
| 99 | + currentData.tid = asyncCapture.setTimeout(() => { |
| 100 | + currentData.tid = null; |
| 101 | + onTrigger?.(true); |
| 102 | + }, dMouseEnterDelay); |
| 103 | + }, |
| 104 | + onMouseLeave: (e) => { |
| 105 | + _child.props.onMouseLeave?.(e); |
| 106 | + |
| 107 | + currentData.tid && asyncCapture.clearTimeout(currentData.tid); |
| 108 | + currentData.tid = asyncCapture.setTimeout(() => { |
| 109 | + currentData.tid = null; |
| 110 | + onTrigger?.(false); |
| 111 | + }, dMouseLeaveDelay); |
| 112 | + }, |
| 113 | + }; |
| 114 | + } |
| 115 | + if (dTrigger === 'focus') { |
| 116 | + childProps = { |
| 117 | + onFocus: (e) => { |
| 118 | + _child.props.onFocus?.(e); |
| 119 | + |
| 120 | + currentData.tid && asyncCapture.clearTimeout(currentData.tid); |
| 121 | + onTrigger?.(true); |
| 122 | + }, |
| 123 | + onBlur: (e) => { |
| 124 | + _child.props.onBlur?.(e); |
| 125 | + |
| 126 | + currentData.tid = asyncCapture.setTimeout(() => onTrigger?.(false), 20); |
| 127 | + }, |
| 128 | + }; |
| 129 | + } |
| 130 | + if (dTrigger === 'click') { |
| 131 | + childProps = { |
| 132 | + onClick: (e) => { |
| 133 | + _child.props.onClick?.(e); |
| 134 | + |
| 135 | + currentData.tid && asyncCapture.clearTimeout(currentData.tid); |
| 136 | + onTrigger?.(); |
| 137 | + }, |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + return React.cloneElement<React.HTMLAttributes<HTMLElement>>(_child, { |
| 142 | + ..._child.props, |
| 143 | + ...childProps, |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + return null; |
| 148 | + }, [asyncCapture, children, currentData, dMouseEnterDelay, dMouseLeaveDelay, dTrigger, dTriggerNode, onTrigger]); |
75 | 149 |
|
76 | 150 | return child; |
77 | 151 | } |
0 commit comments