Skip to content

Commit ff99216

Browse files
committed
💩 update
1 parent 0663e02 commit ff99216

File tree

10 files changed

+116
-46
lines changed

10 files changed

+116
-46
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [react-custom-events-hooks](https://github.com/cool-hooks/react-custom-events-hooks)
22

3-
[![NPM version](http://img.shields.io/npm/v/react-custom-events-hooks?style=flat-square)](https://www.npmjs.com/package/react-custom-events-hooks)
4-
[![NPM downloads](http://img.shields.io/npm/dm/react-custom-events-hooks?style=flat-square)](https://www.npmjs.com/package/react-custom-events-hooks)
3+
[![NPM version](https://img.shields.io/npm/v/react-custom-events-hooks?style=flat-square)](https://www.npmjs.com/package/react-custom-events-hooks)
4+
[![NPM downloads](https://img.shields.io/npm/dm/react-custom-events-hooks?style=flat-square)](https://www.npmjs.com/package/react-custom-events-hooks)
55
[![NPM license](https://img.shields.io/npm/l/react-custom-events-hooks?style=flat-square)](https://www.npmjs.com/package/react-custom-events-hooks)
66
[![Codecov](https://img.shields.io/codecov/c/github/cool-hooks/react-custom-events-hooks?style=flat-square)](https://codecov.io/gh/cool-hooks/react-custom-events-hooks)
77
[![Travis](https://img.shields.io/travis/cool-hooks/react-custom-events-hooks/master?style=flat-square)](https://travis-ci.org/cool-hooks/react-custom-events-hooks)

__tests__/useCustomEvent.test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,30 @@ describe('useCustomEvent', () => {
88
useCustomEvent({ eventName: 'myAwesomeCustomEvent' })
99
);
1010

11-
// expect(result.sum(2, 3)).toBe(5);
11+
result.current('hello');
1212
});
1313

1414
it('should', () => {
15+
const onSignal = jest.fn();
16+
1517
const { result } = renderHook(() =>
1618
useCustomEvent({
1719
eventName: 'myAwesomeCustomEvent',
18-
onSignal: (e) => null,
20+
defaultValue: {
21+
sender: 'dummy-sender',
22+
},
23+
onSignal,
1924
})
2025
);
2126

22-
// expect(result.sum(2, 3)).toBe(5);
27+
result.current({
28+
title: 'hello',
29+
message: 'world',
30+
});
31+
32+
expect(onSignal).toHaveBeenCalledWith({
33+
title: 'hello',
34+
message: 'world',
35+
});
2336
});
2437
});

__tests__/useEmitter.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
3+
import { useEmitter } from '../src';
4+
5+
describe('useEmitter', () => {
6+
it('should', () => {
7+
const { result } = renderHook(() => useEmitter('myAwesomeCustomEvent'));
8+
9+
result.current({
10+
title: 'hello',
11+
});
12+
});
13+
14+
it('should', () => {
15+
const { result } = renderHook(() => null);
16+
});
17+
});

__tests__/useListener.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
3+
import { useCustomEvent, useListener } from '../src';
4+
5+
describe('useListener', () => {
6+
it('should', () => {
7+
const { result } = renderHook(() =>
8+
useCustomEvent({ eventName: 'myAwesomeCustomEvent' })
9+
);
10+
11+
result.current('hello');
12+
});
13+
14+
it('should', () => {
15+
const onSignal = jest.fn();
16+
17+
const { result } = renderHook(() =>
18+
useCustomEvent({
19+
eventName: 'myAwesomeCustomEvent',
20+
defaultValue: {
21+
sender: 'dummy-sender',
22+
},
23+
onSignal,
24+
})
25+
);
26+
27+
result.current({
28+
title: 'hello',
29+
message: 'world',
30+
});
31+
32+
expect(onSignal).toHaveBeenCalledWith({
33+
title: 'hello',
34+
message: 'world',
35+
});
36+
});
37+
});
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import { useListener } from './useListener';
22
import { useEmitter } from './useEmitter';
33

4-
import type { Element, Options } from './types';
4+
import type { Element, Options } from '../types';
55

6-
interface Params {
6+
interface Params<T> {
77
readonly eventName: string;
8-
onSignal?: (e: CustomEvent) => void;
8+
onSignal?: (e: CustomEvent<T>) => void;
99
readonly element?: Element;
1010
readonly options?: Options;
1111
}
1212

13-
export const useCustomEvent = ({
13+
export const useCustomEvent = <T>({
1414
eventName,
1515
onSignal,
1616
element = window,
1717
options = {},
18-
}: Params) => {
18+
}: Params<T>) => {
1919
const handleSignal = onSignal || (() => null);
2020

21-
useListener(eventName, handleSignal, element, options);
21+
useListener<T>(eventName, handleSignal, element, options);
2222

23-
return useEmitter(eventName, element);
23+
return useEmitter<T>(eventName, element);
2424
};

src/hooks/useEmitter.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Element } from '../types';
2+
3+
export const useEmitter = <T>(eventName: string, element: Element = window) => {
4+
const callEvent = (data: T) => {
5+
const event = new CustomEvent(eventName, { detail: data });
6+
7+
element.dispatchEvent(event);
8+
};
9+
10+
return callEvent;
11+
};

src/hooks/useListener.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useEffect } from 'react';
2+
3+
import type { Element, Options } from '../types';
4+
5+
export const useListener = <T>(
6+
eventName: string,
7+
onSignal: (e: CustomEvent<T>) => void,
8+
element: Element = window,
9+
options: Options = {}
10+
) => {
11+
useEffect(() => {
12+
if (typeof onSignal === 'function') {
13+
const handleSignal = (e: Event) => {
14+
onSignal(e as CustomEvent);
15+
};
16+
17+
element.addEventListener(eventName, handleSignal, options);
18+
19+
return () =>
20+
element.removeEventListener(eventName, handleSignal, options);
21+
}
22+
}, [element, eventName, onSignal, options]);
23+
};

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { useEmitter } from './useEmitter';
2-
export { useListener } from './useListener';
3-
export { useCustomEvent } from './useCustomEvent';
1+
export { useEmitter } from './hooks/useEmitter';
2+
export { useListener } from './hooks/useListener';
3+
export { useCustomEvent } from './hooks/useCustomEvent';

src/useEmitter.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/useListener.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)