File tree Expand file tree Collapse file tree 4 files changed +72
-0
lines changed Expand file tree Collapse file tree 4 files changed +72
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ export * from './useCheckbox/useCheckbox';
7
7
export { useCustomHeightFromChildrens } from './useCustomHeightFromChildrens/useCustomHeightFromChildren' ;
8
8
export * from './useClickOutside' ;
9
9
export * from './useKeyPressed/useEscPressed' ;
10
+ export * from './useEscPressedV2' ;
10
11
export * from './useKeyPressed/useKeyPressed' ;
11
12
export * from './useScrollBlock/useScrollBlock' ;
12
13
export * from './useRoveFocus/useRoveFocus' ;
Original file line number Diff line number Diff line change
1
+ export * from './useEscPressedV2' ;
Original file line number Diff line number Diff line change
1
+ import { fireEvent } from '@testing-library/react' ;
2
+ import { renderHook } from '@testing-library/react-hooks' ;
3
+
4
+ import { useEscPressedV2 } from './useEscPressedV2' ;
5
+
6
+ describe ( 'useEscPressedV2' , ( ) => {
7
+ const mockRef = { current : document . createElement ( 'div' ) } ;
8
+
9
+ it ( 'does not call onEscPress when ref is not set' , ( ) => {
10
+ const mockEscPress = jest . fn ( ) ;
11
+ renderHook ( ( ) => useEscPressedV2 ( { ref : { current : null } , onEscPress : mockEscPress } ) ) ;
12
+ fireEvent . keyDown ( mockRef . current , { key : 'Escape' } ) ;
13
+
14
+ expect ( mockEscPress ) . not . toHaveBeenCalled ( ) ;
15
+ } ) ;
16
+
17
+ it ( 'calls onEscPress when Escape is pressed' , ( ) => {
18
+ const mockEscPress = jest . fn ( ) ;
19
+ renderHook ( ( ) => useEscPressedV2 ( { ref : mockRef , onEscPress : mockEscPress } ) ) ;
20
+ fireEvent . keyDown ( mockRef . current , { key : 'Escape' } ) ;
21
+
22
+ expect ( mockEscPress ) . toHaveBeenCalled ( ) ;
23
+ } ) ;
24
+
25
+ it ( 'does not call onEscPress when another key is pressed' , ( ) => {
26
+ const mockEscPress = jest . fn ( ) ;
27
+ renderHook ( ( ) => useEscPressedV2 ( { ref : mockRef , onEscPress : mockEscPress } ) ) ;
28
+ fireEvent . keyDown ( mockRef . current , { key : 'A' } ) ;
29
+
30
+ expect ( mockEscPress ) . not . toHaveBeenCalled ( ) ;
31
+ } ) ;
32
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import React from 'react' ;
2
+
3
+ type UseEscPressedV2ParamsType = {
4
+ ref : React . RefObject < HTMLElement > ;
5
+ onEscPress : ( event : KeyboardEvent ) => void ;
6
+ disablePreventDefault ?: boolean ;
7
+ disableStopPropagation ?: boolean ;
8
+ } ;
9
+
10
+ type UseEscPressedV2ReturnType = void ;
11
+
12
+ export const useEscPressedV2 = ( {
13
+ ref,
14
+ onEscPress,
15
+ disablePreventDefault = false ,
16
+ disableStopPropagation = false ,
17
+ } : UseEscPressedV2ParamsType ) : UseEscPressedV2ReturnType => {
18
+ React . useEffect ( ( ) => {
19
+ if ( ! ref . current ) {
20
+ return ;
21
+ }
22
+ const handleKeyDown = ( event : KeyboardEvent ) => {
23
+ if ( event . key === 'Escape' ) {
24
+ if ( ! disablePreventDefault ) {
25
+ event . preventDefault ( ) ;
26
+ }
27
+ if ( ! disableStopPropagation ) {
28
+ event . stopPropagation ( ) ;
29
+ }
30
+ onEscPress ( event ) ;
31
+ }
32
+ } ;
33
+ ref . current . addEventListener ( 'keydown' , handleKeyDown ) ;
34
+ return ( ) => {
35
+ ref . current ?. removeEventListener ( 'keydown' , handleKeyDown ) ;
36
+ } ;
37
+ } , [ onEscPress ] ) ;
38
+ } ;
You can’t perform that action at this time.
0 commit comments