-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
1d2a025
commit 2a6dd8a
Showing
10 changed files
with
348 additions
and
143 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,95 @@ | ||
export const TodoFilter = () => ( | ||
<form className="field has-addons"> | ||
<p className="control"> | ||
<span className="select"> | ||
<select data-cy="statusSelect"> | ||
<option value="all">All</option> | ||
<option value="active">Active</option> | ||
<option value="completed">Completed</option> | ||
</select> | ||
</span> | ||
</p> | ||
|
||
<p className="control is-expanded has-icons-left has-icons-right"> | ||
<input | ||
data-cy="searchInput" | ||
type="text" | ||
className="input" | ||
placeholder="Search..." | ||
/> | ||
<span className="icon is-left"> | ||
<i className="fas fa-magnifying-glass" /> | ||
</span> | ||
|
||
<span className="icon is-right" style={{ pointerEvents: 'all' }}> | ||
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} | ||
<button data-cy="clearSearchButton" type="button" className="delete" /> | ||
</span> | ||
</p> | ||
</form> | ||
); | ||
import { debounce } from 'lodash'; | ||
import { ChangeEvent, useCallback, useEffect, useRef, useState } from 'react'; | ||
import { FilterType } from '../../types/Filter'; | ||
|
||
type Props = { | ||
setQuery: (value: string) => void; | ||
query: string; | ||
handleChangeSelect: (value: FilterType) => void; | ||
filter: FilterType; | ||
setFilter: (value: FilterType) => void; | ||
}; | ||
export const TodoFilter: React.FC<Props> = ({ | ||
setQuery, | ||
query, | ||
handleChangeSelect, | ||
filter, | ||
setFilter, | ||
}) => { | ||
const [inputValue, setInputValue] = useState(query); | ||
|
||
const debouncedSetQuery = useCallback( | ||
debounce((value: string) => { | ||
setQuery(value); | ||
}, 500), | ||
[], | ||
); | ||
|
||
const handleChange = (event: ChangeEvent<HTMLSelectElement>) => { | ||
const value = event.target.value as FilterType; | ||
|
||
handleChangeSelect(value); | ||
}; | ||
|
||
const handleChangeInput = (event: ChangeEvent<HTMLInputElement>) => { | ||
const value = event.target.value; | ||
|
||
setInputValue(value); | ||
debouncedSetQuery(value); | ||
}; | ||
|
||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
const clearInput = () => { | ||
setInputValue(''); | ||
setQuery(''); | ||
setFilter('all'); | ||
}; | ||
|
||
useEffect(() => { | ||
if (inputRef.current) { | ||
inputRef.current.focus(); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<form className="field has-addons"> | ||
<p className="control"> | ||
<span className="select"> | ||
<select data-cy="statusSelect" onChange={handleChange} value={filter}> | ||
<option value="all">All</option> | ||
<option value="active">Active</option> | ||
<option value="completed">Completed</option> | ||
</select> | ||
</span> | ||
</p> | ||
|
||
<p className="control is-expanded has-icons-left has-icons-right"> | ||
<input | ||
ref={inputRef} | ||
onChange={handleChangeInput} | ||
value={inputValue} | ||
data-cy="searchInput" | ||
type="text" | ||
className="input" | ||
placeholder="Search..." | ||
/> | ||
<span className="icon is-left"> | ||
<i className="fas fa-magnifying-glass" /> | ||
</span> | ||
|
||
{query && ( | ||
<span className="icon is-right" style={{ pointerEvents: 'all' }}> | ||
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} | ||
<button | ||
data-cy="clearSearchButton" | ||
type="button" | ||
className="delete" | ||
onClick={clearInput} | ||
/> | ||
</span> | ||
)} | ||
</p> | ||
</form> | ||
); | ||
}; |
Oops, something went wrong.