Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: keyboard navigation in search (#4651)
- Loading branch information
Showing
7 changed files
with
194 additions
and
31 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
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
7 changes: 7 additions & 0 deletions
7
frontend/src/component/common/Search/SearchSuggestions/onEnter.ts
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,7 @@ | ||
export const onEnter = (callback: () => void) => { | ||
return (event: React.KeyboardEvent<HTMLSpanElement>): void => { | ||
if (event.key === 'Enter' || event.keyCode === 13) { | ||
callback(); | ||
} | ||
}; | ||
}; |
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,52 @@ | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import { useRef } from 'react'; | ||
import { useOnBlur } from './useOnBlur'; | ||
|
||
function TestComponent(props: { onBlurHandler: () => void }) { | ||
const divRef = useRef(null); | ||
useOnBlur(divRef, props.onBlurHandler); | ||
|
||
return ( | ||
<div data-testid="wrapper"> | ||
<div tabIndex={0} data-testid="inside" ref={divRef}> | ||
Inside | ||
</div> | ||
<div tabIndex={0} data-testid="outside"> | ||
Outside | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
test('should not call the callback when blurring within the same container', async () => { | ||
let mockCallbackCallCount = 0; | ||
const mockCallback = () => mockCallbackCallCount++; | ||
|
||
render(<TestComponent onBlurHandler={mockCallback} />); | ||
|
||
const insideDiv = screen.getByTestId('inside'); | ||
|
||
insideDiv.focus(); | ||
insideDiv.blur(); | ||
|
||
await waitFor(() => { | ||
expect(mockCallbackCallCount).toBe(0); | ||
}); | ||
}); | ||
|
||
test('should call the callback when blurring outside of the container', async () => { | ||
let mockCallbackCallCount = 0; | ||
const mockCallback = () => mockCallbackCallCount++; | ||
|
||
render(<TestComponent onBlurHandler={mockCallback} />); | ||
|
||
const insideDiv = screen.getByTestId('inside'); | ||
const outsideDiv = screen.getByTestId('outside'); | ||
|
||
insideDiv.focus(); | ||
outsideDiv.focus(); | ||
|
||
await waitFor(() => { | ||
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,31 @@ | ||
import { useEffect } from 'react'; | ||
|
||
export const useOnBlur = ( | ||
containerRef: React.RefObject<HTMLElement>, | ||
callback: () => void | ||
): void => { | ||
useEffect(() => { | ||
const handleBlur = (event: FocusEvent) => { | ||
// setTimeout is used because activeElement might not immediately be the new focused element after a blur event | ||
setTimeout(() => { | ||
if ( | ||
containerRef.current && | ||
!containerRef.current.contains(document.activeElement) | ||
) { | ||
callback(); | ||
} | ||
}, 0); | ||
}; | ||
|
||
const containerElement = containerRef.current; | ||
if (containerElement) { | ||
containerElement.addEventListener('blur', handleBlur, true); | ||
} | ||
|
||
return () => { | ||
if (containerElement) { | ||
containerElement.removeEventListener('blur', handleBlur, true); | ||
} | ||
}; | ||
}, [containerRef, callback]); | ||
}; |