Skip to content

Commit

Permalink
Add debounce for search input
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Bartosik committed Sep 22, 2021
1 parent 9cf206b commit ef4bee3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
4 changes: 0 additions & 4 deletions src/components/Search/Search.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,4 @@ describe('Search component', () => {
expect(screen.getByRole('textbox')).toHaveValue(searchPhrase);
});

// it('calls setSearchQuery prop 6 times', () => {
// userEvent.type(screen.getByRole('textbox'), searchPhrase);
// expect(mockSearchQueryHandler).toHaveBeenCalledTimes(searchPhrase.length);
// });
});
19 changes: 15 additions & 4 deletions src/components/Search/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import { useRecoilState } from 'recoil';
import { useSetRecoilState } from 'recoil';
import { AiOutlineSearch } from 'react-icons/ai';
import { useState, useEffect } from 'react';

import { InputWrapper } from './Search.styles';
import { searchQueryState } from 'store';

const Search = ({ placeholder }: { placeholder: string }) => {
const [searchQuery, setSearchQuery] = useRecoilState(searchQueryState);
const setSearchQuery = useSetRecoilState(searchQueryState);
const [input, setInput] = useState('');

useEffect(() => {
const id = setTimeout(() => {
setSearchQuery(input);
}, 500);
return () => {
clearTimeout(id);
};
}, [input, setSearchQuery]);
return (
<InputWrapper>
<AiOutlineSearch />
<input
value={searchQuery}
value={input}
type="text"
placeholder={placeholder}
onChange={e => setSearchQuery(e.target.value)}
onChange={e => setInput(e.target.value)}
aria-label={placeholder}
/>
</InputWrapper>
Expand Down

1 comment on commit ef4bee3

@vercel
Copy link

@vercel vercel bot commented on ef4bee3 Sep 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.