Skip to content

Commit 9abe2e9

Browse files
committed
Better types
1 parent 4949e7f commit 9abe2e9

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

__tests__/index.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,19 @@ describe('ClickAway Listener', () => {
3333
const fakeHandleClick2 = jest.fn();
3434
const { getByTestId } = render(
3535
<React.Fragment>
36-
<ClickAwayListener onClickAway={fakeHandleClick} data-testid="hello-world">
37-
Hello World
36+
<ClickAwayListener onClickAway={fakeHandleClick}>
37+
<div data-testid="hello-world">
38+
Hello World
39+
</div>
3840
</ClickAwayListener>
3941
<button data-testid="button-one">A button</button>
4042
<button data-testid="some-other-button-one">Some other button</button>
4143
<p data-testid="text-one">A text element</p>
4244

43-
<ClickAwayListener onClickAway={fakeHandleClick2} data-testid="foo-bar">
44-
Foo bar
45+
<ClickAwayListener onClickAway={fakeHandleClick2}>
46+
<div data-testid="foo-bar">
47+
Foo bar
48+
</div>
4549
</ClickAwayListener>
4650
<button data-testid="button-two">Foo bar button</button>
4751
<button data-testid="some-other-button-two">Foo bar other button</button>

src/index.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
import React, { useEffect, useRef } from 'react';
1+
import React, { useEffect, useRef, FunctionComponent } from 'react';
22

33
interface Props {
4-
children?: React.ReactNode;
54
onClickAway: Function;
6-
childrenProps?: { [key: string]: any }[];
75
}
86

9-
const ClickAwayListener = (props: Props) => {
10-
const {
11-
children,
12-
onClickAway,
13-
...childrenProps
14-
} = props;
7+
const ClickAwayListener: FunctionComponent<Props> = ({ onClickAway, children }) => {
158

169
let node = useRef<HTMLDivElement>(null);
1710

1811
useEffect(() => {
19-
const handleMouseDown = (event:any) => {
20-
if (node.current && node.current.contains(event.target)) {
12+
const handleMouseDown = (event: MouseEvent): void => {
13+
if (node.current && node.current.contains(event.target as Node)) {
2114
return;
2215
}
2316

@@ -32,7 +25,7 @@ const ClickAwayListener = (props: Props) => {
3225
});
3326

3427
return (
35-
<div ref={node} {...childrenProps}>
28+
<div ref={node}>
3629
{children}
3730
</div>
3831
);

0 commit comments

Comments
 (0)