-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* eslint-disable jsx-a11y/click-events-have-key-events */ | ||
import React from 'react'; | ||
|
||
import {useOnClickOutside} from '../useOnClickOutside'; | ||
|
||
export const Demo = () => { | ||
const observerRef = React.useRef(null); | ||
const [status, setStatus] = React.useState<1 | 0>(0); | ||
|
||
const handleOutsideClick = () => { | ||
setStatus(0); | ||
}; | ||
|
||
const handleClick = () => { | ||
setStatus(1); | ||
}; | ||
|
||
useOnClickOutside({ | ||
observedRef: observerRef, | ||
enabled: true, | ||
handleCallback: handleOutsideClick, | ||
}); | ||
|
||
return ( | ||
<div> | ||
<h1>{status}</h1> | ||
<div ref={observerRef} onClick={handleClick}> | ||
{'Target'} | ||
</div> | ||
<div>{'Outside'}</div> | ||
</div> | ||
); | ||
}; |
20 changes: 20 additions & 0 deletions
20
src/components/utils/useOnClickOutside/__tests__/useOnClickOutside.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react'; | ||
|
||
import {render, screen} from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import {Demo} from './Demo'; | ||
|
||
test('Check useOnClickOutside correct work', async () => { | ||
render(<Demo />); | ||
|
||
expect(screen.getByRole('heading')).toHaveTextContent('0'); | ||
|
||
await userEvent.click(screen.getByText('Target')); | ||
|
||
expect(screen.getByRole('heading')).toHaveTextContent('1'); | ||
|
||
await userEvent.click(screen.getByText('Outside')); | ||
|
||
expect(screen.getByRole('heading')).toHaveTextContent('0'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export {useOnClickOutside} from './useOnClickOutside'; | ||
export type {UseOnClickOutsideType} from './useOnClickOutside'; |