Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 3c13393

Browse files
Hubert KosterHubert Koster
authored andcommitted
chore: refactoring code
1 parent 407c562 commit 3c13393

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/components/UserNavbarItem/__tests__/item.desktop.test.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ describe('User Navbar Desktop Item', () => {
7171
});
7272
describe('Search popup', () => {
7373
beforeEach(() => {
74-
render(<UserNavbarDesktopItem is_logged_in={false} authUrl={'https://www.example.com'} />);
74+
render(
75+
<React.Fragment>
76+
<UserNavbarDesktopItem is_logged_in={false} authUrl={'https://www.example.com'} />
77+
<input type='text' placeholder='search' className='navbar__search-input' />
78+
</React.Fragment>,
79+
);
7580
});
7681

7782
afterEach(() => {
@@ -85,6 +90,16 @@ describe('User Navbar Desktop Item', () => {
8590
expect(navigation.classList.contains('search-open'));
8691
});
8792

93+
it('should focus the input after using the hotkey command', async () => {
94+
await userEvent.keyboard('{Meta>}[KeyK]{/Meta}');
95+
96+
const navigation = screen.getByRole('navigation');
97+
expect(navigation.classList.contains('search-open'));
98+
99+
const input = screen.getByPlaceholderText('search');
100+
expect(input).toHaveFocus();
101+
});
102+
88103
it('should be able to close search on same hotkey command', async () => {
89104
await userEvent.keyboard('{Meta>}[KeyK]{/Meta}');
90105

src/components/UserNavbarItem/item.desktop.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,6 @@ const UserNavbarDesktopItem = ({ authUrl, is_logged_in }: IUserNavbarItemProps)
1111
const location = useLocation();
1212
const nav_ref = useRef(null);
1313

14-
// This is the only React way to access and focus the search input from the 3rd party library we use
15-
const focusSearchInput = () => {
16-
// For some reason, this library wraps the search in a new element after triggering the search the first time
17-
const focusInput = () =>
18-
nav_ref && nav_ref?.current?.nextElementSibling?.firstChild?.firstChild?.focus?.();
19-
const focusNestedInput = () =>
20-
nav_ref &&
21-
nav_ref?.current?.nextElementSibling?.firstChild?.firstChild?.firstChild?.focus?.();
22-
23-
focusInput() ?? focusNestedInput();
24-
};
25-
2614
const closeSearchHotkey = (event) => {
2715
const press_escape = event.key === 'Escape';
2816
const press_cmd_and_k = event.metaKey && event.key === 'k';
@@ -36,6 +24,15 @@ const UserNavbarDesktopItem = ({ authUrl, is_logged_in }: IUserNavbarItemProps)
3624
if (press_cmd_and_k) setToggleSearch(true);
3725
};
3826

27+
const focusSearchInput = () => {
28+
// Using vanilla JS to get the element since it's a 3rd party library. I cannot access through React.
29+
const search_input = document.querySelector('.navbar__search-input');
30+
if (search_input) {
31+
const focusInput = () => (search_input as HTMLElement).focus();
32+
focusInput();
33+
}
34+
};
35+
3936
useEffect(() => {
4037
setToggleSearch(false);
4138
}, [location]);

0 commit comments

Comments
 (0)