-
-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Search suggestion selectable (#4610)
- Loading branch information
Showing
3 changed files
with
96 additions
and
10 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
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,44 @@ | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import { useRef } from 'react'; | ||
import { useOnClickOutside } from './useOnClickOutside'; | ||
|
||
function TestComponent(props: { outsideClickHandler: () => void }) { | ||
const divRef = useRef(null); | ||
useOnClickOutside([divRef], props.outsideClickHandler); | ||
|
||
return ( | ||
<div data-testid="wrapper"> | ||
<div data-testid="inside" ref={divRef}> | ||
Inside | ||
</div> | ||
<div data-testid="outside">Outside</div> | ||
</div> | ||
); | ||
} | ||
|
||
test('should not call the callback when clicking inside', () => { | ||
let mockCallbackCallCount = 0; | ||
const mockCallback = () => mockCallbackCallCount++; | ||
|
||
render(<TestComponent outsideClickHandler={mockCallback} />); | ||
|
||
const insideDiv = screen.getByTestId('inside'); | ||
|
||
// Simulate a click inside the div | ||
fireEvent.click(insideDiv); | ||
|
||
expect(mockCallbackCallCount).toBe(0); | ||
}); | ||
|
||
test('should call the callback when clicking outside', () => { | ||
let mockCallbackCallCount = 0; | ||
const mockCallback = () => mockCallbackCallCount++; | ||
|
||
render(<TestComponent outsideClickHandler={mockCallback} />); | ||
|
||
const outsideDiv = screen.getByTestId('outside'); | ||
|
||
fireEvent.click(outsideDiv); | ||
|
||
expect(mockCallbackCallCount).toBe(1); | ||
}); |
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 @@ | ||
import { useEffect } from 'react'; | ||
|
||
/** | ||
* Hook to handle outside clicks for a given list of refs. | ||
* | ||
* @param {Array<React.RefObject>} refs - List of refs to the target elements. | ||
* @param {Function} callback - Callback to execute on outside click. | ||
*/ | ||
export const useOnClickOutside = ( | ||
refs: Array<React.RefObject<HTMLElement>>, | ||
callback: Function | ||
) => { | ||
useEffect(() => { | ||
const handleClickOutside = (event: MouseEvent) => { | ||
// Check if event target is outside all provided refs | ||
if ( | ||
!refs.some( | ||
ref => | ||
ref.current && | ||
ref.current.contains(event.target as Node) | ||
) | ||
) { | ||
callback(); | ||
} | ||
}; | ||
|
||
document.addEventListener('click', handleClickOutside); | ||
|
||
return () => { | ||
document.removeEventListener('click', handleClickOutside); | ||
}; | ||
}, [refs, callback]); | ||
}; |