Skip to content

Commit 8efa3fc

Browse files
committed
feat(useClickAway): 支持传入 options 作为 addEventListner 的第三个参数
1 parent c09513f commit 8efa3fc

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* title: Support event listener options
3+
* desc: For scenarios where click is blocked from bubbling, you can use event listener options to change the capture phase.
4+
*
5+
* title.zh-CN: 支持事件监听选项
6+
* desc.zh-CN: 针对点击阻止冒泡的场景,可以使用事件监听选项选择捕获阶段。
7+
*/
8+
9+
import React, { useState, useRef } from 'react';
10+
import { useClickAway } from 'ahooks';
11+
12+
export default () => {
13+
const [counter, setCounter] = useState(0);
14+
15+
const ref = useRef<HTMLButtonElement>(null);
16+
17+
useClickAway(() => setCounter((s) => s + 1), ref, 'click', { capture: true });
18+
19+
return (
20+
<div>
21+
<button ref={ref} type='button'>
22+
box1
23+
</button>
24+
<button type='button' style={{ marginLeft: 16 }} onClick={(e) => e.stopPropagation()}>
25+
box2
26+
</button>
27+
<p>counter: {counter}</p>
28+
</div>
29+
);
30+
};

packages/hooks/src/useClickAway/index.en-US.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Listen for click events outside the target element.
3333

3434
<code src="./demo/demo6.tsx"/>
3535

36+
### Support event listener options
37+
38+
<code src="./demo/demo7.tsx"/>
39+
3640
## API
3741

3842
```typescript
@@ -42,7 +46,8 @@ type DocumentEventKey = keyof DocumentEventMap;
4246
useClickAway<T extends Event = Event>(
4347
onClickAway: (event: T) => void,
4448
target: Target | Target[],
45-
eventName?: DocumentEventKey | DocumentEventKey[]
49+
eventName?: DocumentEventKey | DocumentEventKey[],
50+
options?: boolean | AddEventListenerOptions
4651
);
4752
```
4853

@@ -53,3 +58,4 @@ useClickAway<T extends Event = Event>(
5358
| onClickAway | Trigger Function | `(event: T) => void` | - |
5459
| target | DOM elements or Ref, support array | `Target` \| `Target[]` | - |
5560
| eventName | Set the event to be listened, support array | `DocumentEventKey` \| `DocumentEventKey[]` | `click` |
61+
| options | Event listener options | `boolean` \| `AddEventListenerOptions` | `false` |

packages/hooks/src/useClickAway/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ export default function useClickAway<T extends Event = Event>(
1010
onClickAway: (event: T) => void,
1111
target: BasicTarget | BasicTarget[],
1212
eventName: DocumentEventKey | DocumentEventKey[] = 'click',
13+
options?: boolean | AddEventListenerOptions,
1314
) {
1415
const onClickAwayRef = useLatest(onClickAway);
16+
const optionsRef = useLatest(options);
1517

1618
useEffectWithTarget(
1719
() => {
@@ -32,10 +34,14 @@ export default function useClickAway<T extends Event = Event>(
3234

3335
const eventNames = Array.isArray(eventName) ? eventName : [eventName];
3436

35-
eventNames.forEach((event) => documentOrShadow.addEventListener(event, handler));
37+
eventNames.forEach((event) =>
38+
documentOrShadow.addEventListener(event, handler, optionsRef.current),
39+
);
3640

3741
return () => {
38-
eventNames.forEach((event) => documentOrShadow.removeEventListener(event, handler));
42+
eventNames.forEach((event) =>
43+
documentOrShadow.removeEventListener(event, handler, optionsRef.current),
44+
);
3945
};
4046
},
4147
Array.isArray(eventName) ? eventName : [eventName],

packages/hooks/src/useClickAway/index.zh-CN.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ nav:
3333

3434
<code src="./demo/demo6.tsx" />
3535

36+
### 支持事件监听选项
37+
38+
<code src="./demo/demo7.tsx" />
39+
3640
## API
3741

3842
```typescript
@@ -42,7 +46,8 @@ type DocumentEventKey = keyof DocumentEventMap;
4246
useClickAway<T extends Event = Event>(
4347
onClickAway: (event: T) => void,
4448
target: Target | Target[],
45-
eventName?: DocumentEventKey | DocumentEventKey[]
49+
eventName?: DocumentEventKey | DocumentEventKey[],
50+
options?: boolean | AddEventListenerOptions
4651
);
4752
```
4853

@@ -53,3 +58,4 @@ useClickAway<T extends Event = Event>(
5358
| onClickAway | 触发函数 | `(event: T) => void` | - |
5459
| target | DOM 节点或者 Ref,支持数组 | `Target` \| `Target[]` | - |
5560
| eventName | 指定需要监听的事件,支持数组 | `DocumentEventKey` \| `DocumentEventKey[]` | `click` |
61+
| options | 事件监听选项 | `boolean` \| `AddEventListenerOptions` | `false` |

0 commit comments

Comments
 (0)