-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
a72dcee
commit 38ee5d5
Showing
4 changed files
with
125 additions
and
1 deletion.
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
75 changes: 75 additions & 0 deletions
75
packages/hooks/src/use-detect-outside-click/index.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,75 @@ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import { useDetectOutsideClick } from './'; | ||
import React, { useRef } from 'react'; | ||
|
||
/** | ||
* `Mock` component. | ||
*/ | ||
|
||
const Mock = ({ initiallyActive }: { initiallyActive?: boolean }) => { | ||
const ref = useRef<HTMLDivElement>(null); | ||
const [active, setActive] = useDetectOutsideClick(ref, initiallyActive); | ||
|
||
return ( | ||
<> | ||
<div | ||
onClick={() => setActive(true)} | ||
ref={ref} | ||
> | ||
{active ? 'active' : 'inactive'} | ||
</div> | ||
|
||
<div>{'outside'}</div> | ||
</> | ||
); | ||
}; | ||
|
||
/** | ||
* Test `useDetectOutsideClick` hook. | ||
*/ | ||
|
||
describe(`'useDetectOutsideClick' hook`, () => { | ||
it('should lose focus', () => { | ||
render(<Mock initiallyActive />); | ||
|
||
expect(screen.queryByText('active')).toBeTruthy(); | ||
|
||
fireEvent.click(screen.getByText('outside')); | ||
|
||
expect(screen.queryByText('inactive')).toBeTruthy(); | ||
}); | ||
|
||
it('should gain focus', () => { | ||
render(<Mock />); | ||
|
||
expect(screen.queryByText('inactive')).toBeTruthy(); | ||
|
||
fireEvent.click(screen.getByText('inactive')); | ||
|
||
expect(screen.queryByText('active')).toBeTruthy(); | ||
}); | ||
|
||
it('should keep focused', () => { | ||
render(<Mock initiallyActive />); | ||
|
||
expect(screen.queryByText('active')).toBeTruthy(); | ||
|
||
fireEvent.click(screen.getByText('active')); | ||
|
||
expect(screen.queryByText('active')).toBeTruthy(); | ||
}); | ||
|
||
it('should keep unfocused', () => { | ||
render(<Mock />); | ||
|
||
expect(screen.queryByText('inactive')).toBeTruthy(); | ||
|
||
fireEvent.click(screen.getByText('outside')); | ||
|
||
expect(screen.queryByText('inactive')).toBeTruthy(); | ||
}); | ||
}); |
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,48 @@ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
import { | ||
Dispatch, | ||
RefObject, | ||
SetStateAction, | ||
useEffect, | ||
useState | ||
} from 'react'; | ||
|
||
/** | ||
* `Result` Props. | ||
*/ | ||
|
||
type Result = [boolean, Dispatch<SetStateAction<boolean>>]; | ||
|
||
/** | ||
* Export `useDetectOutsideClick` hook. | ||
*/ | ||
|
||
export function useDetectOutsideClick( | ||
ref: RefObject<HTMLElement> | null | undefined, | ||
initialState = false | ||
): Result { | ||
const [active, setActive] = useState(initialState); | ||
|
||
useEffect(() => { | ||
const handleClickOutside = ({ target }: MouseEvent) => { | ||
if ( | ||
ref?.current && | ||
target instanceof Element && | ||
!ref?.current?.contains(target) | ||
) { | ||
setActive(false); | ||
} | ||
}; | ||
|
||
document.addEventListener('click', handleClickOutside, true); | ||
|
||
return () => { | ||
document.removeEventListener('click', handleClickOutside, true); | ||
}; | ||
}, [ref]); | ||
|
||
return [active, setActive]; | ||
} |
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