Skip to content

feat: support for array as event parameter in useEventListener #2598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/hooks/src/useEventListener/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ describe('useEventListener', () => {
expect(state).toBe(1);
});

it('test on event list listener', async () => {
let state: number = 0;
const onClick = () => {
state++;
};
const onKeydown = () => {
state++;
};
const { rerender, unmount } = renderHook(
() => (
useEventListener('click', onClick, { target: () => container }),
useEventListener('keydown', onKeydown, { target: () => container })
),
);

document.body.click();
document.body.dispatchEvent(new KeyboardEvent('keydown'));
expect(state).toBe(0);
rerender();
container.click();
container.dispatchEvent(new KeyboardEvent('keydown'));
expect(state).toBe(2);
unmount();
document.body.click();
document.body.dispatchEvent(new KeyboardEvent('keydown'));
expect(state).toBe(2);
});

it('test "enable" parameter', () => {
let state = 0;
let enable = true;
Expand Down
10 changes: 5 additions & 5 deletions packages/hooks/src/useEventListener/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ useEventListener(

### Property

| Property | Description | type | default |
| --------- | ---------------------- | --------------------- | ------- |
| eventName | Event name | `string` | - |
| handler | Callback function | `(ev: Event) => void` | - |
| options | More options(optional) | `Options` | - |
| Property | Description | type | default |
| --------- | ---------------------- | ---------------------- | ------- |
| eventName | Event name | `string` \| `string[]` | - |
| handler | Callback function | `(ev: Event) => void` | - |
| options | More options(optional) | `Options` | - |

### Options

Expand Down
24 changes: 15 additions & 9 deletions packages/hooks/src/useEventListener/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ function useEventListener<K extends keyof WindowEventMap>(
options?: Options<Window>,
): void;
function useEventListener(
eventName: string,
eventName: string | string[],
handler: (event: Event) => void,
options?: Options<Window>,
): void;
function useEventListener(eventName: string, handler: noop, options: Options): void;
function useEventListener(eventName: string | string[], handler: noop, options: Options): void;

function useEventListener(eventName: string, handler: noop, options: Options = {}) {
function useEventListener(eventName: string | string[], handler: noop, options: Options = {}) {
const { enable = true } = options;

const handlerRef = useLatest(handler);
Expand All @@ -62,15 +62,21 @@ function useEventListener(eventName: string, handler: noop, options: Options = {
return handlerRef.current(event);
};

targetElement.addEventListener(eventName, eventListener, {
capture: options.capture,
once: options.once,
passive: options.passive,
const eventNameArray = Array.isArray(eventName) ? eventName : [eventName];

eventNameArray.forEach((eventName) => {
targetElement.addEventListener(eventName, eventListener, {
capture: options.capture,
once: options.once,
passive: options.passive,
});
});

return () => {
targetElement.removeEventListener(eventName, eventListener, {
capture: options.capture,
eventNameArray.forEach((eventName) => {
targetElement.removeEventListener(eventName, eventListener, {
capture: options.capture,
});
});
};
},
Expand Down
10 changes: 5 additions & 5 deletions packages/hooks/src/useEventListener/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ useEventListener(

### Params

| 参数 | 说明 | 类型 | 默认值 |
| --------- | ---------- | --------------------- | ------ |
| eventName | 事件名称 | `string` | - |
| handler | 处理函数 | `(ev: Event) => void` | - |
| options | 设置(可选) | `Options` | - |
| 参数 | 说明 | 类型 | 默认值 |
| --------- | ---------- | ---------------------- | ------ |
| eventName | 事件名称 | `string` \| `string[]` | - |
| handler | 处理函数 | `(ev: Event) => void` | - |
| options | 设置(可选) | `Options` | - |

### Options

Expand Down
Loading